Skip to content

Commit

Permalink
Add changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
asreehari-splunk committed Sep 10, 2024
1 parent 0350c2e commit 3f66f2c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
27 changes: 27 additions & 0 deletions .chloggen/sapmexporter-prioritize-token-in-context.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: 'enhancement'

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: 'Prioritize token in context when accesstokenpassthrough is enabled'

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

# (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: []
28 changes: 14 additions & 14 deletions exporter/sapmexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ func (se *sapmExporter) pushTraceData(ctx context.Context, td ptrace.Traces) err
return nil
}

var accessToken string
accessToken = se.accessToken(ctx, rss.At(0))
accessToken := se.retrieveAccessToken(ctx, rss.At(0))

batches, err := jaeger.ProtoFromTraces(td)
if err != nil {
Expand Down Expand Up @@ -125,25 +124,26 @@ func (se *sapmExporter) pushTraceData(ctx context.Context, td ptrace.Traces) err
return nil
}

func (se *sapmExporter) accessToken(ctx context.Context, md ptrace.ResourceSpans) string {
func (se *sapmExporter) retrieveAccessToken(ctx context.Context, md ptrace.ResourceSpans) string {
if !se.config.AccessTokenPassthrough {
// Nothing to do if token is pass through not configured or resource is nil.
return ""
}

if ctxAccessToken, ok := ctx.Value("X-SF-TOKEN").(string); ok {
return ctxAccessToken
} else {
return se.retrieveAccessToken(md)
var token string
switch {
case ctx.Value("X-SF-TOKEN") != nil:
if ctxAccessToken, ok := ctx.Value("X-SF-TOKEN").(string); ok {
token = ctxAccessToken
}
default:
attrs := md.Resource().Attributes()
if accessToken, ok := attrs.Get(splunk.SFxAccessTokenLabel); ok {
token = accessToken.Str()
}
}
}

func (se *sapmExporter) retrieveAccessToken(md ptrace.ResourceSpans) string {
attrs := md.Resource().Attributes()
if accessToken, ok := attrs.Get(splunk.SFxAccessTokenLabel); ok {
return accessToken.Str()
}
return ""
return token
}

// filterToken filters the access token from the batch processor to avoid leaking credentials to the backend.
Expand Down
15 changes: 11 additions & 4 deletions exporter/sapmexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
)

type contextKey string

func (c contextKey) String() string {
return string(c)
}

var testMetadata = contextKey("X-SF-TOKEN")

func TestCreateTracesExporter(t *testing.T) {
cfg := &Config{
Endpoint: "test-endpoint",
Expand Down Expand Up @@ -272,11 +280,10 @@ func TestSAPMClientTokenAccess(t *testing.T) {

trace, testTraceErr := buildTestTrace()
require.NoError(t, testTraceErr)
var ctx context.Context

ctx := context.Background()
if tt.inContext {
ctx = context.WithValue(context.Background(), "X-SF-TOKEN", "SplunkAccessToken")
} else {
ctx = context.Background()
ctx = context.WithValue(ctx, testMetadata.String(), "SplunkAccessToken")
}
err = se.pushTraceData(ctx, trace)
require.NoError(t, err)
Expand Down

0 comments on commit 3f66f2c

Please sign in to comment.