Skip to content

Commit

Permalink
refactor: use client to retrieve token from context
Browse files Browse the repository at this point in the history
  • Loading branch information
asreehari-splunk committed Sep 11, 2024
1 parent 77ee86e commit a15f6fa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
27 changes: 10 additions & 17 deletions exporter/sapmexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/jaegertracing/jaeger/model"
sapmclient "github.com/signalfx/sapm-proto/client"
"go.opentelemetry.io/collector/client"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
Expand All @@ -23,14 +24,6 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
)

type contextKey string

const sapmAccessTokenContextKey contextKey = splunk.SFxAccessTokenHeader

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

// TODO: Find a place for this to be shared.
type baseTracesExporter struct {
component.Component
Expand Down Expand Up @@ -138,17 +131,17 @@ func (se *sapmExporter) retrieveAccessToken(ctx context.Context, md ptrace.Resou
return ""
}

var token string
if ctxAccessToken, ok := ctx.Value(sapmAccessTokenContextKey).(string); ok {
token = ctxAccessToken
} else {
attrs := md.Resource().Attributes()
if accessToken, ok := attrs.Get(splunk.SFxAccessTokenLabel); ok {
token = accessToken.Str()
}
cl := client.FromContext(ctx)
ss := cl.Metadata.Get(splunk.SFxAccessTokenHeader)
if len(ss) > 0 {
return ss[0]
}

return token
attrs := md.Resource().Attributes()
if accessToken, ok := attrs.Get(splunk.SFxAccessTokenLabel); ok {
return accessToken.Str()
}
return ""
}

// filterToken filters the access token from the batch processor to avoid leaking credentials to the backend.
Expand Down
8 changes: 7 additions & 1 deletion exporter/sapmexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
splunksapm "github.com/signalfx/sapm-proto/gen"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/client"
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/pdata/ptrace"

Expand Down Expand Up @@ -275,7 +276,12 @@ func TestSAPMClientTokenAccess(t *testing.T) {

ctx := context.Background()
if tt.inContext {
ctx = context.WithValue(ctx, sapmAccessTokenContextKey, "SplunkAccessToken")
ctx = client.NewContext(
ctx,
client.Info{Metadata: client.NewMetadata(
map[string][]string{splunk.SFxAccessTokenHeader: {"SplunkAccessToken"}},
)},
)
}
err = se.pushTraceData(ctx, trace)
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions exporter/sapmexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector v0.109.0 // indirect
go.opentelemetry.io/collector/client v1.15.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.109.0 // indirect
go.opentelemetry.io/collector/consumer/consumerprofiles v0.109.0 // indirect
go.opentelemetry.io/collector/consumer/consumertest v0.109.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions exporter/sapmexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a15f6fa

Please sign in to comment.