Skip to content

Commit

Permalink
removed use of context in client calls from scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
shalper2 committed Jan 27, 2025
1 parent 2f6b034 commit 56b82cb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 66 deletions.
30 changes: 15 additions & 15 deletions receiver/splunkenterprisereceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type splunkEntClient struct {
}

// The splunkEntClient is made up of a number of splunkClients defined for each configured endpoint
type splunkClientMap map[any]splunkClient
type splunkClientMap map[string]splunkClient

// The client does not carry the endpoint that is configured with it and golang does not support mixed
// type arrays so this struct contains the pair: the client configured for the endpoint and the endpoint
Expand Down Expand Up @@ -92,12 +92,8 @@ func newSplunkEntClient(ctx context.Context, cfg *Config, h component.Host, s co
}

// For running ad hoc searches only
func (c *splunkEntClient) createRequest(ctx context.Context, sr *searchResponse) (req *http.Request, err error) {
// get endpoint type from the context
eptType := ctx.Value(endpointType("type"))
if eptType == nil {
return nil, errCtxMissingEndpointType
}
func (c *splunkEntClient) createRequest(eptType string, sr *searchResponse) (req *http.Request, err error) {
ctx := context.WithValue(context.Background(), endpointType("type"), eptType)

// Running searches via Splunk's REST API is a two step process: First you submit the job to run
// this returns a jobid which is then used in the second part to retrieve the search results
Expand Down Expand Up @@ -137,14 +133,9 @@ func (c *splunkEntClient) createRequest(ctx context.Context, sr *searchResponse)
}

// forms an *http.Request for use with Splunk built-in API's (like introspection).
func (c *splunkEntClient) createAPIRequest(ctx context.Context, apiEndpoint string) (req *http.Request, err error) {
func (c *splunkEntClient) createAPIRequest(eptType string, apiEndpoint string) (req *http.Request, err error) {
var u string

// get endpoint type from the context
eptType := ctx.Value(endpointType("type"))
if eptType == nil {
return nil, errCtxMissingEndpointType
}
ctx := context.WithValue(context.Background(), endpointType("type"), eptType)

if e, ok := c.clients[eptType]; ok {
u = e.endpoint.String() + apiEndpoint
Expand All @@ -167,7 +158,16 @@ func (c *splunkEntClient) makeRequest(req *http.Request) (*http.Response, error)
if eptType == nil {
return nil, errCtxMissingEndpointType
}
if sc, ok := c.clients[eptType]; ok {

var endpointType string
switch t := eptType.(type) {
case string:
endpointType = t
default:
endpointType = fmt.Sprintf("%v", eptType)
}

if sc, ok := c.clients[endpointType]; ok {
res, err := sc.client.Do(req)
if err != nil {
return nil, err
Expand Down
8 changes: 2 additions & 6 deletions receiver/splunkenterprisereceiver/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,9 @@ func TestClientCreateRequest(t *testing.T) {
},
}

ctx := context.Background()
ctx = context.WithValue(ctx, endpointType("type"), typeIdx)
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
req, err := test.client.createRequest(ctx, test.sr)
req, err := test.client.createRequest(typeIdx, test.sr)
require.NoError(t, err)
// have to test specific parts since individual fields are pointers
require.Equal(t, test.expected.URL, req.URL)
Expand Down Expand Up @@ -165,9 +163,7 @@ func TestAPIRequestCreate(t *testing.T) {

require.NoError(t, err)

ctx := context.Background()
ctx = context.WithValue(ctx, endpointType("type"), typeIdx)
req, err := client.createAPIRequest(ctx, "/test/endpoint")
req, err := client.createAPIRequest(typeIdx, "/test/endpoint")
require.NoError(t, err)

// build the expected request
Expand Down
Loading

0 comments on commit 56b82cb

Please sign in to comment.