From 65405096d71b51d769b389078904ad9c8b5afba8 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Tue, 11 Jul 2023 11:11:26 +0200 Subject: [PATCH] Add the sampling decision to the trace envelope header --- dynamic_sampling_context.go | 6 ++++++ dynamic_sampling_context_test.go | 15 ++++++++------- otel/propagator_test.go | 4 ++-- otel/span_processor_test.go | 2 +- tracing_test.go | 10 +++++----- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/dynamic_sampling_context.go b/dynamic_sampling_context.go index 15b37e316..365072607 100644 --- a/dynamic_sampling_context.go +++ b/dynamic_sampling_context.go @@ -82,6 +82,12 @@ func DynamicSamplingContextFromTransaction(span *Span) DynamicSamplingContext { entries["user_segment"] = userSegment } + if span.Sampled.Bool() { + entries["sampled"] = "true" + } else { + entries["sampled"] = "false" + } + return DynamicSamplingContext{ Entries: entries, Frozen: true, diff --git a/dynamic_sampling_context_test.go b/dynamic_sampling_context_test.go index ac791bb24..a6604ee18 100644 --- a/dynamic_sampling_context_test.go +++ b/dynamic_sampling_context_test.go @@ -82,7 +82,7 @@ func TestDynamicSamplingContextFromTransaction(t *testing.T) { input: func() *Span { ctx := NewTestContext(ClientOptions{ EnableTracing: true, - TracesSampleRate: 0.5, + TracesSampleRate: 1.0, Dsn: "http://public@example.com/sentry/1", Release: "1.0.0", Environment: "test", @@ -97,13 +97,14 @@ func TestDynamicSamplingContextFromTransaction(t *testing.T) { want: DynamicSamplingContext{ Frozen: true, Entries: map[string]string{ - "sample_rate": "0.5", + "sample_rate": "1", "trace_id": "d49d9bf66f13450b81f65bc51cf49c03", "public_key": "public", "release": "1.0.0", "environment": "test", "transaction": "name", "user_segment": "user_segment", + "sampled": "true", }, }, }, @@ -112,7 +113,7 @@ func TestDynamicSamplingContextFromTransaction(t *testing.T) { input: func() *Span { ctx := NewTestContext(ClientOptions{ EnableTracing: true, - TracesSampleRate: 0.5, + TracesSampleRate: 0.0, Dsn: "http://public@example.com/sentry/1", Release: "1.0.0", }) @@ -123,10 +124,10 @@ func TestDynamicSamplingContextFromTransaction(t *testing.T) { want: DynamicSamplingContext{ Frozen: true, Entries: map[string]string{ - "sample_rate": "0.5", - "trace_id": "d49d9bf66f13450b81f65bc51cf49c03", - "public_key": "public", - "release": "1.0.0", + "trace_id": "d49d9bf66f13450b81f65bc51cf49c03", + "public_key": "public", + "release": "1.0.0", + "sampled": "false", }, }, }, diff --git a/otel/propagator_test.go b/otel/propagator_test.go index fe7abb699..0941aa3bd 100644 --- a/otel/propagator_test.go +++ b/otel/propagator_test.go @@ -131,7 +131,7 @@ func testInjectUsesSetsValidTrace(t *testing.T, withChildSpan bool) { spanID: "6e0c63257de34c92", sampled: sentry.SampledTrue, }, - wantBaggage: stringPtr("sentry-environment=testing,sentry-release=1.2.3,sentry-transaction=sampled-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-sample_rate=1"), + wantBaggage: stringPtr("sentry-environment=testing,sentry-release=1.2.3,sentry-transaction=sampled-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-sample_rate=1,sentry-sampled=true"), wantSentryTrace: stringPtr("d4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-1"), }, { @@ -142,7 +142,7 @@ func testInjectUsesSetsValidTrace(t *testing.T, withChildSpan bool) { spanID: "6e0c63257de34c92", sampled: sentry.SampledFalse, }, - wantBaggage: stringPtr("sentry-environment=testing,sentry-release=1.2.3,sentry-transaction=not-sampled-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b"), + wantBaggage: stringPtr("sentry-environment=testing,sentry-release=1.2.3,sentry-transaction=not-sampled-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b,sentry-sampled=false"), wantSentryTrace: stringPtr("d4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-0"), }, { diff --git a/otel/span_processor_test.go b/otel/span_processor_test.go index 6c6483331..4361442c0 100644 --- a/otel/span_processor_test.go +++ b/otel/span_processor_test.go @@ -124,7 +124,7 @@ func TestOnStartRootSpan(t *testing.T) { testutils.AssertBaggageStringsEqual( t, sentrySpan.ToBaggage(), - "sentry-transaction=spanName,sentry-environment=testing,sentry-public_key=abc,sentry-release=1.2.3,sentry-sample_rate=1,sentry-trace_id="+otelTraceId.String(), + "sentry-transaction=spanName,sentry-environment=testing,sentry-public_key=abc,sentry-release=1.2.3,sentry-sample_rate=1,sentry-sampled=true,sentry-trace_id="+otelTraceId.String(), ) } diff --git a/tracing_test.go b/tracing_test.go index 0e5102e30..3fbd651d8 100644 --- a/tracing_test.go +++ b/tracing_test.go @@ -817,9 +817,9 @@ func TestGetTransactionReturnsNilOnManuallyCreatedSpans(t *testing.T) { func TestToBaggage(t *testing.T) { ctx := NewTestContext(ClientOptions{ - EnableTracing: true, - SampleRate: 1.0, - Release: "test-release", + EnableTracing: true, + TracesSampleRate: 1.0, + Release: "test-release", }) transaction := StartTransaction(ctx, "transaction-name") transaction.TraceID = TraceIDFromHex("f1a4c5c9071eca1cdf04e4132527ed16") @@ -827,7 +827,7 @@ func TestToBaggage(t *testing.T) { assertBaggageStringsEqual( t, transaction.ToBaggage(), - "sentry-trace_id=f1a4c5c9071eca1cdf04e4132527ed16,sentry-release=test-release,sentry-transaction=transaction-name", + "sentry-trace_id=f1a4c5c9071eca1cdf04e4132527ed16,sentry-release=test-release,sentry-transaction=transaction-name,sentry-sample_rate=1,sentry-sampled=true", ) // Calling ToBaggage() on a child span should return the same result @@ -835,7 +835,7 @@ func TestToBaggage(t *testing.T) { assertBaggageStringsEqual( t, child.ToBaggage(), - "sentry-trace_id=f1a4c5c9071eca1cdf04e4132527ed16,sentry-release=test-release,sentry-transaction=transaction-name", + "sentry-trace_id=f1a4c5c9071eca1cdf04e4132527ed16,sentry-release=test-release,sentry-transaction=transaction-name,sentry-sample_rate=1,sentry-sampled=true", ) }