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 34bf50e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
27 changes: 21 additions & 6 deletions exporter/sapmexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ package sapmexporter // import "github.com/open-telemetry/opentelemetry-collecto
import (
"context"
"errors"
"fmt"

"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,12 +25,23 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
)

type contextKey string
type ContextSource struct {
Key string
}

func (ts *ContextSource) Get(ctx context.Context) (string, error) {
cl := client.FromContext(ctx)
ss := cl.Metadata.Get(ts.Key)

const sapmAccessTokenContextKey contextKey = splunk.SFxAccessTokenHeader
if len(ss) == 0 {
return "", nil
}

if len(ss) > 1 {
return "", fmt.Errorf("%d source keys found in the context, can't determine which one to use", len(ss))
}

func (k contextKey) String() string {
return string(k)
return ss[0], nil
}

// TODO: Find a place for this to be shared.
Expand Down Expand Up @@ -139,8 +152,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 34bf50e

Please sign in to comment.