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

Only add to trace state if key does not exist #3645

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Changes from 2 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
23 changes: 14 additions & 9 deletions sentry_sdk/integrations/opentelemetry/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
trace_state = span_context.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))

return SamplingResult(
Expand All @@ -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:
trace_state = span_context.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))

return SamplingResult(
Decision.RECORD_AND_SAMPLE,
Expand Down
Loading