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 ParentBased sampler for implicit parent spans #1394

Merged
merged 6 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
([#1373](https://github.com/open-telemetry/opentelemetry-python/pull/1373))
- Rename Meter class to Accumulator in Metrics SDK
([#1372](https://github.com/open-telemetry/opentelemetry-python/pull/1372))
- Fix `ParentBased` sampler for implicit parent spans. Fix also `trace_state`
erasure for dropped spans or spans sampled by the `TraceIdRatioBased` sampler.
([#1394](https://github.com/open-telemetry/opentelemetry-python/pull/1394))

## Version 0.15b0

Expand Down
28 changes: 13 additions & 15 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def should_sample(
trace_state: "TraceState" = None,
) -> "SamplingResult":
if self._decision is Decision.DROP:
return SamplingResult(self._decision)
attributes = None
return SamplingResult(self._decision, attributes, trace_state)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to pass in the trace_state for dropped spans?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes as otherwise the trace breaks when the span is propagated further. E.g. flask instrumentation extracts span a span where the sampling flag in the trace parent is false. The handler in the flask app makes an outgoing HTTP request with the requests library. The requests instrumentation has the flask span as parent so the created span will be dropped. The dropped span however is used for injecting/propagation, so it needs to include the trace_state of the parent.
w3c_tracecontext tests also rely on that the trace_state gets propagated.


def get_description(self) -> str:
Expand Down Expand Up @@ -209,8 +209,8 @@ def should_sample(
if trace_id & self.TRACE_ID_LIMIT < self.bound:
decision = Decision.RECORD_AND_SAMPLE
if decision is Decision.DROP:
return SamplingResult(decision)
return SamplingResult(decision, attributes)
attributes = None
return SamplingResult(decision, attributes, trace_state)

def get_description(self) -> str:
return "TraceIdRatioBased{{{}}}".format(self._rate)
Expand Down Expand Up @@ -238,18 +238,16 @@ def should_sample(
links: Sequence["Link"] = None,
trace_state: "TraceState" = None,
) -> "SamplingResult":
if parent_context is not None:
parent_span_context = get_current_span(
parent_context
).get_span_context()
# only drop if parent exists and is not a root span
if (
parent_span_context is not None
and parent_span_context.is_valid
and not parent_span_context.trace_flags.sampled
):
return SamplingResult(Decision.DROP)
return SamplingResult(Decision.RECORD_AND_SAMPLE, attributes)
parent_span_context = get_current_span(
parent_context
).get_span_context()
# respect the sampling flag of the parent if present
if parent_span_context is not None and parent_span_context.is_valid:
decision = Decision.RECORD_AND_SAMPLE
if not parent_span_context.trace_flags.sampled:
decision = Decision.DROP
attributes = None
return SamplingResult(decision, attributes, trace_state)

return self._delegate.should_sample(
parent_context=parent_context,
Expand Down
Loading