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 b8c47dd
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 @@ -11,6 +11,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger v0.109.0
github.com/signalfx/sapm-proto v0.14.0
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/client v1.15.0
go.opentelemetry.io/collector/component v0.109.0
go.opentelemetry.io/collector/config/configopaque v1.15.0
go.opentelemetry.io/collector/config/configretry v1.15.0
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 b8c47dd

Please sign in to comment.