Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/sapm] Add priority to pick token from context #35123

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: []
15 changes: 10 additions & 5 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 Down Expand Up @@ -42,7 +43,6 @@ func (se *sapmExporter) Shutdown(context.Context) error {
}

func newSAPMExporter(cfg *Config, params exporter.Settings) (sapmExporter, error) {

client, err := sapmclient.New(cfg.clientOptions()...)
if err != nil {
return sapmExporter{}, err
Expand Down Expand Up @@ -71,7 +71,6 @@ func newSAPMTracesExporter(cfg *Config, set exporter.Settings) (exporter.Traces,
exporterhelper.WithRetry(cfg.BackOffConfig),
exporterhelper.WithTimeout(cfg.TimeoutSettings),
)

if err != nil {
return nil, err
}
Expand All @@ -95,8 +94,8 @@ func (se *sapmExporter) pushTraceData(ctx context.Context, td ptrace.Traces) err
return nil
}

// All metrics in the pmetric.Metrics will have the same access token because of the BatchPerResourceMetrics.
accessToken := se.retrieveAccessToken(rss.At(0))
accessToken := se.retrieveAccessToken(ctx, rss.At(0))

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

func (se *sapmExporter) retrieveAccessToken(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 ""
}

cl := client.FromContext(ctx)
ss := cl.Metadata.Get(splunk.SFxAccessTokenHeader)
if len(ss) > 0 {
return ss[0]
}

attrs := md.Resource().Attributes()
if accessToken, ok := attrs.Get(splunk.SFxAccessTokenLabel); ok {
return accessToken.Str()
Expand Down
74 changes: 74 additions & 0 deletions 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 @@ -215,6 +216,79 @@ func TestSAPMClientTokenUsageAndErrorMarshalling(t *testing.T) {
}
}

func TestSAPMClientTokenAccess(t *testing.T) {
tests := []struct {
name string
inContext bool
accessTokenPassthrough bool
}{
{
name: "Token in context with passthrough",
inContext: true,
accessTokenPassthrough: true,
},
{
name: "Token in attributes with passthrough",
inContext: false,
accessTokenPassthrough: true,
},
{
name: "Token in config wihout passthrough",
inContext: false,
accessTokenPassthrough: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tracesReceived := false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectedToken := "ClientAccessToken"
if tt.accessTokenPassthrough && tt.inContext {
expectedToken = "SplunkAccessToken"
} else if tt.accessTokenPassthrough && !tt.inContext {
expectedToken = "TraceAccessToken0"
}
assert.Contains(t, r.Header.Get("x-sf-token"), expectedToken)
status := 200
w.WriteHeader(status)
tracesReceived = true
}))
defer func() {
assert.True(t, tracesReceived, "Test server never received traces.")
}()
defer server.Close()

cfg := &Config{
Endpoint: server.URL,
AccessToken: "ClientAccessToken",
AccessTokenPassthroughConfig: splunk.AccessTokenPassthroughConfig{
AccessTokenPassthrough: tt.accessTokenPassthrough,
},
}
params := exportertest.NewNopSettings()

se, err := newSAPMExporter(cfg, params)
assert.NoError(t, err)
assert.NotNil(t, se, "failed to create trace exporter")

trace, testTraceErr := buildTestTrace()
require.NoError(t, testTraceErr)

ctx := context.Background()
if tt.inContext {
ctx = client.NewContext(
ctx,
client.Info{Metadata: client.NewMetadata(
map[string][]string{splunk.SFxAccessTokenHeader: {"SplunkAccessToken"}},
)},
)
}
err = se.pushTraceData(ctx, trace)
require.NoError(t, err)
})
}
}

func decompress(body io.Reader, compression string) ([]byte, error) {
switch compression {
case "":
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.

Loading