Skip to content

Commit

Permalink
Splunkent client refactor (open-telemetry#27205)
Browse files Browse the repository at this point in the history
Refactored parts of the Splunk Enterprise receiver to better leverage
the pre-existing otel SDK. This PR also updates the README to be a more
informative document.

[27026](open-telemetry#27026)

Unit testing is included and updated to accommodate the new refactor.
  • Loading branch information
shalper2 authored and jmsnll committed Nov 12, 2023
1 parent 6959b16 commit f6ece6a
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 213 deletions.
28 changes: 28 additions & 0 deletions .chloggen/splunkent-client-refactor-with-sdk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Use this changelog template to create an entry for release notes.

# 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: "Users can now use auth settings and basicauth extension to connect to their Splunk enterprise deployments"

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

# (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]

39 changes: 36 additions & 3 deletions receiver/splunkenterprisereceiver/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
# 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.
The Splunk Enterprise Receiver is a pull based tool which enables the ingestion of performance metrics describing the operational status of a user's Splunk Enterprise deployment to an appropriate observability tool.
It is designed to leverage several different data sources to gather these metrics including the [introspection api endpoint](https://docs.splunk.com/Documentation/Splunk/9.1.1/RESTREF/RESTintrospect) and serializing
results from ad-hoc searches. Because of this, care must be taken by users when enabling metrics as running searches can effect your Splunk Enterprise Deployment and introspection may fail to report for Splunk
Cloud deployments. The primary purpose of this receiver is to empower those tasked with the maintenance and care of a Splunk Enterprise deployment to leverage opentelemetry and their observability toolset in their
jobs.

## Configuration

The following settings are required, omitting them will either cause your receiver to fail to compile or result in 4/5xx return codes during scraping.

* `basicauth` (from [basicauthextension](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/basicauthextension)): A configured stanza for the basicauthextension.
* `auth` (no default): String name referencing your auth extension.
* `endpoint` (no default): your Splunk Enterprise host's endpoint.

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.

Example:

```yaml
extensions:
basicauth/client:
client_auth:
username: admin
password: securityFirst

receivers:
splunkenterprise:
auth: basicauth/client
endpoint: "https://localhost:8089"
timeout: 45s
```
For a full list of settings exposed by this receiver please look [here](./config.go) with a detailed configuration [here](./testdata/config.yaml).
46 changes: 12 additions & 34 deletions receiver/splunkenterprisereceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,31 @@ package splunkenterprisereceiver // import "github.com/open-telemetry/openteleme

import (
"context"
"crypto/tls"
"encoding/base64"
"fmt"
"net/http"
"net/url"
"strings"

"go.opentelemetry.io/collector/component"
)

type splunkEntClient struct {
endpoint *url.URL
client *http.Client
basicAuth string
client *http.Client
endpoint *url.URL
}

func newSplunkEntClient(cfg *Config) splunkEntClient {
// tls party
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
func newSplunkEntClient(cfg *Config, h component.Host, s component.TelemetrySettings) (*splunkEntClient, error) {
client, err := cfg.HTTPClientSettings.ToClient(h, s)
if err != nil {
return nil, err
}

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,
}
return &splunkEntClient{
client: client,
endpoint: endpoint,
}, nil
}

// For running ad hoc searches only
Expand All @@ -59,10 +49,6 @@ func (c *splunkEntClient) createRequest(ctx context.Context, sr *searchResponse)
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)
Expand All @@ -73,10 +59,6 @@ func (c *splunkEntClient) createRequest(ctx context.Context, sr *searchResponse)
return nil, err
}

// Required headers
req.Header.Add("Authorization", c.basicAuth)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

return req, nil
}

Expand All @@ -88,10 +70,6 @@ func (c *splunkEntClient) createAPIRequest(ctx context.Context, apiEndpoint stri
return nil, err
}

// Required headers
req.Header.Add("Authorization", c.basicAuth)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

return req, nil
}

Expand Down
94 changes: 64 additions & 30 deletions receiver/splunkenterprisereceiver/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package splunkenterprisereceiver // import "github.com/open-telemetry/openteleme

import (
"context"
"encoding/base64"
"fmt"
"net/http"
"net/url"
Expand All @@ -14,58 +13,87 @@ import (
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/configauth"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/extension/auth"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

// mockHost allows us to create a test host with a no op extension that can be used to satisfy the SDK without having to parse from an
// actual config.yaml.
type mockHost struct {
component.Host
extensions map[component.ID]component.Component
}

func (m *mockHost) GetExtensions() map[component.ID]component.Component {
return m.extensions
}

func TestClientCreation(t *testing.T) {
// create a client from an example config
client := newSplunkEntClient(&Config{
Username: "admin",
Password: "securityFirst",
MaxSearchWaitTime: 11 * time.Second,
cfg := &Config{
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "https://localhost:8089",
Auth: &configauth.Authentication{
AuthenticatorID: component.NewID("basicauth/client"),
},
},
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 10 * time.Second,
InitialDelay: 1 * time.Second,
Timeout: 11 * time.Second,
},
})
}

testEndpoint, _ := url.Parse("https://localhost:8089")
host := &mockHost{
extensions: map[component.ID]component.Component{
component.NewID("basicauth/client"): auth.NewClient(),
},
}
// create a client from an example config
client, err := newSplunkEntClient(cfg, host, componenttest.NewNopTelemetrySettings())
require.NoError(t, err)

authString := fmt.Sprintf("%s:%s", "admin", "securityFirst")
auth64 := base64.StdEncoding.EncodeToString([]byte(authString))
testBasicAuth := fmt.Sprintf("Basic %s", auth64)
testEndpoint, _ := url.Parse("https://localhost:8089")

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,
cfg := &Config{
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "https://localhost:8089",
Auth: &configauth.Authentication{
AuthenticatorID: component.NewID("basicauth/client"),
},
},
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 10 * time.Second,
InitialDelay: 1 * time.Second,
Timeout: 11 * time.Second,
},
})
}

host := &mockHost{
extensions: map[component.ID]component.Component{
component.NewID("basicauth/client"): auth.NewClient(),
},
}
// create a client from an example config
client, err := newSplunkEntClient(cfg, host, componenttest.NewNopTelemetrySettings())

require.NoError(t, err)

testJobID := "123"

tests := []struct {
desc string
sr *searchResponse
client splunkEntClient
client *splunkEntClient
expected *http.Request
}{
{
Expand All @@ -81,8 +109,6 @@ func TestClientCreateRequest(t *testing.T) {
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
}(),
},
Expand All @@ -99,8 +125,6 @@ func TestClientCreateRequest(t *testing.T) {
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
}(),
},
Expand All @@ -122,27 +146,37 @@ func TestClientCreateRequest(t *testing.T) {

// 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,
cfg := &Config{
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "https://localhost:8089",
Auth: &configauth.Authentication{
AuthenticatorID: component.NewID("basicauth/client"),
},
},
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 10 * time.Second,
InitialDelay: 1 * time.Second,
Timeout: 11 * time.Second,
},
})
}

host := &mockHost{
extensions: map[component.ID]component.Component{
component.NewID("basicauth/client"): auth.NewClient(),
},
}
// create a client from an example config
client, err := newSplunkEntClient(cfg, host, componenttest.NewNopTelemetrySettings())

require.NoError(t, err)

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

// build the expected request
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)
Expand Down
22 changes: 5 additions & 17 deletions receiver/splunkenterprisereceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"net/url"
"strings"
"time"

"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/receiver/scraperhelper"
Expand All @@ -17,22 +16,15 @@ import (
)

var (
errBadOrMissingEndpoint = errors.New("Missing a valid endpoint")
errMissingUsername = errors.New("Missing valid username")
errMissingPassword = errors.New("Missing valid password")
errBadScheme = errors.New("Endpoint scheme must be either http or https")
errBadOrMissingEndpoint = errors.New("missing a valid endpoint")
errBadScheme = errors.New("endpoint scheme must be either http or https")
errMissingAuthExtension = errors.New("auth extension missing from config")
)

type Config struct {
confighttp.HTTPClientSettings `mapstructure:",squash"`
scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
metadata.MetricsBuilderConfig `mapstructure:",squash"`
// Username and password with associated with an account with
// permission to access the Splunk deployments REST api
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
// default is 60s
MaxSearchWaitTime time.Duration `mapstructure:"max_search_wait_time"`
}

func (cfg *Config) Validate() (errors error) {
Expand All @@ -54,12 +46,8 @@ func (cfg *Config) Validate() (errors error) {
}
}

if cfg.Username == "" {
errors = multierr.Append(errors, errMissingUsername)
}

if cfg.Password == "" {
errors = multierr.Append(errors, errMissingPassword)
if cfg.HTTPClientSettings.Auth.AuthenticatorID.Name() == "" {
errors = multierr.Append(errors, errMissingAuthExtension)
}

return errors
Expand Down
Loading

0 comments on commit f6ece6a

Please sign in to comment.