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

Fix sampling rate #180

Merged
merged 2 commits into from
Nov 8, 2024
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
13 changes: 11 additions & 2 deletions internal/extension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,17 @@ func (em *ExtensionManager) SendEndInvocationRequest(ctx context.Context, functi
req.Header.Set(string(DdSamplingPriority), samplingPriority)
}
} else {
req.Header.Set(string(DdTraceId), fmt.Sprint(functionExecutionSpan.Context().TraceID()))
req.Header.Set(string(DdSpanId), fmt.Sprint(functionExecutionSpan.Context().SpanID()))
spanContext := functionExecutionSpan.Context()
req.Header.Set(string(DdTraceId), fmt.Sprint(spanContext.TraceID()))
req.Header.Set(string(DdSpanId), fmt.Sprint(spanContext.SpanID()))

// Try to get sampling priority
// Check if the context implements SamplingPriority method
if pc, ok := spanContext.(interface{ SamplingPriority() (int, bool) }); ok && pc != nil {
if priority, ok := pc.SamplingPriority(); ok {
req.Header.Set(string(DdSamplingPriority), fmt.Sprint(priority))
}
}
}

resp, err := em.httpClient.Do(req)
Expand Down
28 changes: 28 additions & 0 deletions internal/extension/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,34 @@ func TestExtensionEndInvocationError(t *testing.T) {
assert.Contains(t, logOutput, "could not send end invocation payload to the extension")
}

type mockSpanContext struct {
ddtrace.SpanContext
}

func (m mockSpanContext) TraceID() uint64 { return 123 }
func (m mockSpanContext) SpanID() uint64 { return 456 }
func (m mockSpanContext) SamplingPriority() (int, bool) { return -1, true }

type mockSpan struct{ ddtrace.Span }

func (m mockSpan) Context() ddtrace.SpanContext { return mockSpanContext{} }

func TestExtensionEndInvocationSamplingPriority(t *testing.T) {
headers := http.Header{}
em := &ExtensionManager{httpClient: capturingClient{hdr: headers}}
span := &mockSpan{}

// When priority in context, use that value
ctx := context.WithValue(context.Background(), DdTraceId, "123")
ctx = context.WithValue(ctx, DdSamplingPriority, "2")
em.SendEndInvocationRequest(ctx, span, ddtrace.FinishConfig{})
assert.Equal(t, "2", headers.Get("X-Datadog-Sampling-Priority"))

// When no context, get priority from span
em.SendEndInvocationRequest(context.Background(), span, ddtrace.FinishConfig{})
assert.Equal(t, "-1", headers.Get("X-Datadog-Sampling-Priority"))
}

type capturingClient struct {
hdr http.Header
}
Expand Down
Loading