Skip to content

[receiver/splunkenterprise] 36330 add build info as attribute #37508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/36330-bi-as-attrs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'breaking'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: splunkenterprisereceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "added new attributes to the receiver and modified config"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [36330]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
1 change: 1 addition & 0 deletions receiver/splunkenterprisereceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The following settings are optional:

* `collection_interval` (default: 10m): The time between scrape attempts.
* `timeout` (default: 60s): The time the scrape function will wait for a response before returning empty.
* `build_version_info` (default: false): Elect to run an additional scrape which will retrieve build and version info for the configured endpoints and attach this as attributes to the selected metrics. A value of false will report an empty string as the attribute value but will speed up the receiver slightly.

Example:

Expand Down
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
1 change: 1 addition & 0 deletions receiver/splunkenterprisereceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {
IdxEndpoint confighttp.ClientConfig `mapstructure:"indexer"`
SHEndpoint confighttp.ClientConfig `mapstructure:"search_head"`
CMEndpoint confighttp.ClientConfig `mapstructure:"cluster_master"`
VersionInfo bool `mapstructure:"build_version_info"`
}

func (cfg *Config) Validate() (errors error) {
Expand Down
Loading
Loading