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 54659f3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
15 changes: 5 additions & 10 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 @@ -139,8 +132,10 @@ func (se *sapmExporter) retrieveAccessToken(ctx context.Context, md ptrace.Resou
}

var token string
if ctxAccessToken, ok := ctx.Value(sapmAccessTokenContextKey).(string); ok {
token = ctxAccessToken
cl := client.FromContext(ctx)
ss := cl.Metadata.Get(splunk.SFxAccessTokenHeader)
if len(ss) > 0 {
token = ss[0]
} else {
attrs := md.Resource().Attributes()
if accessToken, ok := attrs.Get(splunk.SFxAccessTokenLabel); ok {
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 54659f3

Please sign in to comment.