From 3702a4a89010688262bfb182bf57e3777c8d1f06 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Fri, 11 Oct 2024 14:44:41 +0200 Subject: [PATCH 1/3] Only add to trace state if key does not exist --- .../integrations/opentelemetry/sampler.py | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/sampler.py b/sentry_sdk/integrations/opentelemetry/sampler.py index 404957f028..7a2460e688 100644 --- a/sentry_sdk/integrations/opentelemetry/sampler.py +++ b/sentry_sdk/integrations/opentelemetry/sampler.py @@ -48,10 +48,13 @@ def get_parent_sampled(parent_context, trace_id): def dropped_result(span_context, attributes, sample_rate=None): # type: (SpanContext, Attributes, Optional[float]) -> SamplingResult - # note that trace_state.add will NOT overwrite existing entries - # so these will only be added the first time in a root span sampling decision - trace_state = span_context.trace_state.add(TRACESTATE_SAMPLED_KEY, "false") - if sample_rate: + # these will only be added the first time in a root span sampling decision + trace_state = span_context.trace_state + + if TRACESTATE_SAMPLED_KEY not in span_context.trace_state.keys(): + trace_state = span_context.trace_state.add(TRACESTATE_SAMPLED_KEY, "false") + + if sample_rate and TRACESTATE_SAMPLE_RATE_KEY not in trace_state.keys(): trace_state = trace_state.add(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate)) return SamplingResult( @@ -63,11 +66,13 @@ def dropped_result(span_context, attributes, sample_rate=None): def sampled_result(span_context, attributes, sample_rate): # type: (SpanContext, Attributes, float) -> SamplingResult - # note that trace_state.add will NOT overwrite existing entries - # so these will only be added the first time in a root span sampling decision - trace_state = span_context.trace_state.add(TRACESTATE_SAMPLED_KEY, "true").add( - TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate) - ) + # these will only be added the first time in a root span sampling decision + trace_state = span_context.trace_state + + if TRACESTATE_SAMPLED_KEY not in trace_state.keys(): + trace_state = span_context.trace_state.add(TRACESTATE_SAMPLED_KEY, "true") + if TRACESTATE_SAMPLE_RATE_KEY not in trace_state.keys(): + trace_state = trace_state.add(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate)) return SamplingResult( Decision.RECORD_AND_SAMPLE, From 5b19c7dd50e87660ba02a26ca584c25ade821773 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Fri, 11 Oct 2024 14:48:25 +0200 Subject: [PATCH 2/3] just a mapping apparently --- sentry_sdk/integrations/opentelemetry/sampler.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/sampler.py b/sentry_sdk/integrations/opentelemetry/sampler.py index 7a2460e688..b055936aa6 100644 --- a/sentry_sdk/integrations/opentelemetry/sampler.py +++ b/sentry_sdk/integrations/opentelemetry/sampler.py @@ -51,10 +51,10 @@ def dropped_result(span_context, attributes, sample_rate=None): # these will only be added the first time in a root span sampling decision trace_state = span_context.trace_state - if TRACESTATE_SAMPLED_KEY not in span_context.trace_state.keys(): + if TRACESTATE_SAMPLED_KEY not in span_context.trace_state: trace_state = span_context.trace_state.add(TRACESTATE_SAMPLED_KEY, "false") - if sample_rate and TRACESTATE_SAMPLE_RATE_KEY not in trace_state.keys(): + if sample_rate and TRACESTATE_SAMPLE_RATE_KEY not in trace_state: trace_state = trace_state.add(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate)) return SamplingResult( @@ -69,9 +69,9 @@ def sampled_result(span_context, attributes, sample_rate): # these will only be added the first time in a root span sampling decision trace_state = span_context.trace_state - if TRACESTATE_SAMPLED_KEY not in trace_state.keys(): + if TRACESTATE_SAMPLED_KEY not in trace_state: trace_state = span_context.trace_state.add(TRACESTATE_SAMPLED_KEY, "true") - if TRACESTATE_SAMPLE_RATE_KEY not in trace_state.keys(): + if TRACESTATE_SAMPLE_RATE_KEY not in trace_state: trace_state = trace_state.add(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate)) return SamplingResult( From 8d3abc4b77c0b21d7c8c5ca1eecf8eca0cbf7705 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Fri, 11 Oct 2024 14:53:12 +0200 Subject: [PATCH 3/3] use correct trace state --- sentry_sdk/integrations/opentelemetry/sampler.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/sampler.py b/sentry_sdk/integrations/opentelemetry/sampler.py index b055936aa6..8ffad41b86 100644 --- a/sentry_sdk/integrations/opentelemetry/sampler.py +++ b/sentry_sdk/integrations/opentelemetry/sampler.py @@ -51,8 +51,8 @@ def dropped_result(span_context, attributes, sample_rate=None): # these will only be added the first time in a root span sampling decision trace_state = span_context.trace_state - if TRACESTATE_SAMPLED_KEY not in span_context.trace_state: - trace_state = span_context.trace_state.add(TRACESTATE_SAMPLED_KEY, "false") + if TRACESTATE_SAMPLED_KEY not in trace_state: + trace_state = trace_state.add(TRACESTATE_SAMPLED_KEY, "false") if sample_rate and TRACESTATE_SAMPLE_RATE_KEY not in trace_state: trace_state = trace_state.add(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate)) @@ -70,7 +70,7 @@ def sampled_result(span_context, attributes, sample_rate): trace_state = span_context.trace_state if TRACESTATE_SAMPLED_KEY not in trace_state: - trace_state = span_context.trace_state.add(TRACESTATE_SAMPLED_KEY, "true") + trace_state = trace_state.add(TRACESTATE_SAMPLED_KEY, "true") if TRACESTATE_SAMPLE_RATE_KEY not in trace_state: trace_state = trace_state.add(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate))