Skip to content

Commit

Permalink
[receiver/splunkenterprise] Add scraping of two initial metrics (#24800)
Browse files Browse the repository at this point in the history
**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
shalper2 authored Sep 13, 2023
1 parent faf9f20 commit a3eacd6
Show file tree
Hide file tree
Showing 23 changed files with 1,654 additions and 9 deletions.
20 changes: 20 additions & 0 deletions .chloggen/splunkent-client.yaml
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:
5 changes: 5 additions & 0 deletions receiver/splunkenterprisereceiver/README.md
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.
107 changes: 107 additions & 0 deletions receiver/splunkenterprisereceiver/client.go
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
}
151 changes: 151 additions & 0 deletions receiver/splunkenterprisereceiver/client_test.go
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)
}
62 changes: 62 additions & 0 deletions receiver/splunkenterprisereceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,65 @@
// SPDX-License-Identifier: Apache-2.0

package splunkenterprisereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/splunkenterprisereceiver"

import (
"errors"
"net/url"
"strings"
"time"

"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/splunkenterprisereceiver/internal/metadata"
)

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")
)

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) {
var targetURL *url.URL

if cfg.Endpoint == "" {
errors = multierr.Append(errors, errBadOrMissingEndpoint)
} else {
// we want to validate that the endpoint url supplied by user is at least
// a little bit valid
var err error
targetURL, err = url.Parse(cfg.Endpoint)
if err != nil {
errors = multierr.Append(errors, errBadOrMissingEndpoint)
}

if !strings.HasPrefix(targetURL.Scheme, "http") {
errors = multierr.Append(errors, errBadScheme)
}
}

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

if cfg.Password == "" {
errors = multierr.Append(errors, errMissingPassword)
}

return errors
}
Loading

0 comments on commit a3eacd6

Please sign in to comment.