-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receiver/splunkenterprise] Add scraping of two initial metrics (#24800)
**Description:** First pass at implementing the component. This PR is primarily focused on implementing the components architecture with less focus on the collection of actual Splunk performance data. This was done to keep the PR relatively short. Considerable work has however been done to implement the receiver logic and accompanying tests. **Link to tracking Issue:** [12667](#12667)
- Loading branch information
Showing
23 changed files
with
1,654 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
# If your change doesn't affect end users, such as a test fix or a tooling change, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: 'enhancement' | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: splunkentreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "adding component logic to splunkenterprise receiver" | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [12667] | ||
|
||
# (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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Splunk Enterprise Receiver | ||
--- | ||
|
||
The Splunk Enterprise Receiver is a pull based tool which enables the ingestion of key performance metrics (KPI's) describing the operational status of a user's Splunk Enterprise deployment to be | ||
added to their OpenTelemetry Pipeline. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package splunkenterprisereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/splunkenterprisereceiver" | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
type splunkEntClient struct { | ||
endpoint *url.URL | ||
client *http.Client | ||
basicAuth string | ||
} | ||
|
||
func newSplunkEntClient(cfg *Config) splunkEntClient { | ||
// tls party | ||
tr := &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
} | ||
|
||
client := &http.Client{Transport: tr} | ||
|
||
endpoint, _ := url.Parse(cfg.Endpoint) | ||
|
||
// build and encode our auth string. Do this work once to avoid rebuilding the | ||
// auth header every time we make a new request | ||
authString := fmt.Sprintf("%s:%s", cfg.Username, cfg.Password) | ||
auth64 := base64.StdEncoding.EncodeToString([]byte(authString)) | ||
basicAuth := fmt.Sprintf("Basic %s", auth64) | ||
|
||
return splunkEntClient{ | ||
client: client, | ||
endpoint: endpoint, | ||
basicAuth: basicAuth, | ||
} | ||
} | ||
|
||
// For running ad hoc searches only | ||
func (c *splunkEntClient) createRequest(ctx context.Context, sr *searchResponse) (*http.Request, error) { | ||
// 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 | ||
if sr.Jobid == nil { | ||
path := "/services/search/jobs/" | ||
url, _ := url.JoinPath(c.endpoint.String(), path) | ||
|
||
// reader for the response data | ||
data := strings.NewReader(sr.search) | ||
|
||
// return the build request, ready to be run by makeRequest | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Required headers | ||
req.Header.Add("Authorization", c.basicAuth) | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
return req, nil | ||
} | ||
path := fmt.Sprintf("/services/search/jobs/%s/results", *sr.Jobid) | ||
url, _ := url.JoinPath(c.endpoint.String(), path) | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Required headers | ||
req.Header.Add("Authorization", c.basicAuth) | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
return req, nil | ||
} | ||
|
||
func (c *splunkEntClient) createAPIRequest(ctx context.Context, apiEndpoint string) (*http.Request, error) { | ||
url := c.endpoint.String() + apiEndpoint | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Required headers | ||
req.Header.Add("Authorization", c.basicAuth) | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
return req, nil | ||
} | ||
|
||
// Construct and perform a request to the API. Returns the searchResponse passed into the | ||
// function as state | ||
func (c *splunkEntClient) makeRequest(req *http.Request) (*http.Response, error) { | ||
res, err := c.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package splunkenterprisereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/splunkenterprisereceiver" | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.opentelemetry.io/collector/receiver/scraperhelper" | ||
) | ||
|
||
func TestClientCreation(t *testing.T) { | ||
// create a client from an example config | ||
client := newSplunkEntClient(&Config{ | ||
Username: "admin", | ||
Password: "securityFirst", | ||
MaxSearchWaitTime: 11 * time.Second, | ||
HTTPClientSettings: confighttp.HTTPClientSettings{ | ||
Endpoint: "https://localhost:8089", | ||
}, | ||
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{ | ||
CollectionInterval: 10 * time.Second, | ||
InitialDelay: 1 * time.Second, | ||
}, | ||
}) | ||
|
||
testEndpoint, _ := url.Parse("https://localhost:8089") | ||
|
||
authString := fmt.Sprintf("%s:%s", "admin", "securityFirst") | ||
auth64 := base64.StdEncoding.EncodeToString([]byte(authString)) | ||
testBasicAuth := fmt.Sprintf("Basic %s", auth64) | ||
|
||
require.Equal(t, client.endpoint, testEndpoint) | ||
require.Equal(t, client.basicAuth, testBasicAuth) | ||
} | ||
|
||
// test functionality of createRequest which is used for building metrics out of | ||
// ad-hoc searches | ||
func TestClientCreateRequest(t *testing.T) { | ||
// create a client from an example config | ||
client := newSplunkEntClient(&Config{ | ||
Username: "admin", | ||
Password: "securityFirst", | ||
MaxSearchWaitTime: 11 * time.Second, | ||
HTTPClientSettings: confighttp.HTTPClientSettings{ | ||
Endpoint: "https://localhost:8089", | ||
}, | ||
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{ | ||
CollectionInterval: 10 * time.Second, | ||
InitialDelay: 1 * time.Second, | ||
}, | ||
}) | ||
|
||
testJobID := "123" | ||
|
||
tests := []struct { | ||
desc string | ||
sr *searchResponse | ||
client splunkEntClient | ||
expected *http.Request | ||
}{ | ||
{ | ||
desc: "First req, no jobid", | ||
sr: &searchResponse{ | ||
search: "example search", | ||
}, | ||
client: client, | ||
expected: func() *http.Request { | ||
method := "POST" | ||
path := "/services/search/jobs/" | ||
testEndpoint, _ := url.Parse("https://localhost:8089") | ||
url, _ := url.JoinPath(testEndpoint.String(), path) | ||
data := strings.NewReader("example search") | ||
req, _ := http.NewRequest(method, url, data) | ||
req.Header.Add("Authorization", client.basicAuth) | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
return req | ||
}(), | ||
}, | ||
{ | ||
desc: "Second req, jobID detected", | ||
sr: &searchResponse{ | ||
search: "example search", | ||
Jobid: &testJobID, | ||
}, | ||
client: client, | ||
expected: func() *http.Request { | ||
method := "GET" | ||
path := fmt.Sprintf("/services/search/jobs/%s/results", testJobID) | ||
testEndpoint, _ := url.Parse("https://localhost:8089") | ||
url, _ := url.JoinPath(testEndpoint.String(), path) | ||
req, _ := http.NewRequest(method, url, nil) | ||
req.Header.Add("Authorization", client.basicAuth) | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
return req | ||
}(), | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
for _, test := range tests { | ||
t.Run(test.desc, func(t *testing.T) { | ||
req, err := test.client.createRequest(ctx, test.sr) | ||
require.NoError(t, err) | ||
// have to test specific parts since individual fields are pointers | ||
require.Equal(t, test.expected.URL, req.URL) | ||
require.Equal(t, test.expected.Method, req.Method) | ||
require.Equal(t, test.expected.Header, req.Header) | ||
require.Equal(t, test.expected.Body, req.Body) | ||
}) | ||
} | ||
} | ||
|
||
// createAPIRequest creates a request for api calls i.e. to introspection endpoint | ||
func TestAPIRequestCreate(t *testing.T) { | ||
client := newSplunkEntClient(&Config{ | ||
Username: "admin", | ||
Password: "securityFirst", | ||
MaxSearchWaitTime: 11 * time.Second, | ||
HTTPClientSettings: confighttp.HTTPClientSettings{ | ||
Endpoint: "https://localhost:8089", | ||
}, | ||
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{ | ||
CollectionInterval: 10 * time.Second, | ||
InitialDelay: 1 * time.Second, | ||
}, | ||
}) | ||
|
||
ctx := context.Background() | ||
req, err := client.createAPIRequest(ctx, "/test/endpoint") | ||
require.NoError(t, err) | ||
|
||
expectedURL := client.endpoint.String() + "/test/endpoint" | ||
expected, _ := http.NewRequest(http.MethodGet, expectedURL, nil) | ||
expected.Header.Add("Authorization", client.basicAuth) | ||
expected.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
require.Equal(t, expected.URL, req.URL) | ||
require.Equal(t, expected.Method, req.Method) | ||
require.Equal(t, expected.Header, req.Header) | ||
require.Equal(t, expected.Body, req.Body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.