-
Notifications
You must be signed in to change notification settings - Fork 553
Potel Sampling #3501
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
Merged
Merged
Potel Sampling #3501
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7acf5ae
First version of Sentry potel sampler
antonpirker b9b42c6
Make traces_sampler work
antonpirker 352cfa8
One more test
antonpirker 2edc217
wording
antonpirker e859aef
Made name not static
antonpirker 1c6d1e7
Added tests for upstream sampling decision
antonpirker b7fe11f
Merge branch 'potel-base' into antonpirker/potel/sampling
antonpirker 6dc77b7
Use new continue_trace
antonpirker c7fd9b8
updated tests
antonpirker c2049c4
Fixed tests
antonpirker 613c5d1
Improved tests
antonpirker a8fbb9d
Cleanup
antonpirker d3a2c2f
Cleanup
antonpirker d2bd9e8
review feedback.
antonpirker e718736
Return update trace_state withing the SamplingResult
antonpirker 453ce29
Fixed trace_state
antonpirker daeb7de
Fix sample_rate code and some cleanup
antonpirker 1c95db2
typing
antonpirker 8acb109
Removed default sample_rate because it is set in _get_options
antonpirker 8b27cd9
Cleanup
antonpirker f29b782
Nicer tests
antonpirker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
from random import random | ||
|
||
from opentelemetry import trace | ||
|
||
from opentelemetry.sdk.trace.sampling import Sampler, SamplingResult, Decision | ||
from opentelemetry.trace.span import TraceState | ||
|
||
import sentry_sdk | ||
from sentry_sdk.integrations.opentelemetry.consts import SENTRY_TRACE_STATE_DROPPED | ||
from sentry_sdk.tracing_utils import has_tracing_enabled | ||
from sentry_sdk.utils import is_valid_sample_rate, logger | ||
|
||
from typing import TYPE_CHECKING, Optional, Sequence | ||
|
||
if TYPE_CHECKING: | ||
from opentelemetry.context import Context | ||
from opentelemetry.trace import Link, SpanKind | ||
from opentelemetry.trace.span import SpanContext | ||
from opentelemetry.util.types import Attributes | ||
|
||
|
||
def get_parent_sampled(parent_context, trace_id): | ||
# type: (Optional[SpanContext], int) -> Optional[bool] | ||
if parent_context is None: | ||
return None | ||
|
||
is_span_context_valid = parent_context is not None and parent_context.is_valid | ||
|
||
# Only inherit sample rate if `traceId` is the same | ||
if is_span_context_valid and parent_context.trace_id == trace_id: | ||
# this is getSamplingDecision in JS | ||
if parent_context.trace_flags.sampled: | ||
return True | ||
|
||
dropped = parent_context.trace_state.get(SENTRY_TRACE_STATE_DROPPED) == "true" | ||
if dropped: | ||
return False | ||
|
||
# TODO-anton: fall back to sampling decision in DSC (for this die DSC needs to be set in the trace_state) | ||
|
||
return None | ||
|
||
|
||
def dropped(parent_context=None): | ||
# type: (Optional[SpanContext]) -> SamplingResult | ||
trace_state = parent_context.trace_state if parent_context is not None else None | ||
updated_trace_context = trace_state or TraceState() | ||
updated_trace_context = updated_trace_context.update( | ||
SENTRY_TRACE_STATE_DROPPED, "true" | ||
) | ||
return SamplingResult( | ||
Decision.DROP, | ||
trace_state=updated_trace_context, | ||
) | ||
|
||
|
||
class SentrySampler(Sampler): | ||
def should_sample( | ||
self, | ||
parent_context, # type: Optional[Context] | ||
trace_id, # type: int | ||
name, # type: str | ||
kind=None, # type: Optional[SpanKind] | ||
attributes=None, # type: Attributes | ||
links=None, # type: Optional[Sequence[Link]] | ||
trace_state=None, # type: Optional[TraceState] | ||
): | ||
# type: (...) -> SamplingResult | ||
client = sentry_sdk.get_client() | ||
|
||
parent_span = trace.get_current_span(parent_context) | ||
parent_context = parent_span.get_span_context() if parent_span else None | ||
|
||
# No tracing enabled, thus no sampling | ||
if not has_tracing_enabled(client.options): | ||
return dropped(parent_context) | ||
|
||
sample_rate = None | ||
|
||
# Check if sampled=True was passed to start_transaction | ||
# TODO-anton: Do we want to keep the start_transaction(sampled=True) thing? | ||
|
||
# Check if there is a traces_sampler | ||
# Traces_sampler is responsible to check parent sampled to have full transactions. | ||
has_traces_sampler = callable(client.options.get("traces_sampler")) | ||
if has_traces_sampler: | ||
# TODO-anton: Make proper sampling_context | ||
sampling_context = { | ||
"transaction_context": { | ||
"name": name, | ||
}, | ||
"parent_sampled": get_parent_sampled(parent_context, trace_id), | ||
} | ||
|
||
sample_rate = client.options["traces_sampler"](sampling_context) | ||
|
||
else: | ||
# Check if there is a parent with a sampling decision | ||
parent_sampled = get_parent_sampled(parent_context, trace_id) | ||
if parent_sampled is not None: | ||
sample_rate = parent_sampled | ||
else: | ||
# Check if there is a traces_sample_rate | ||
sample_rate = client.options.get("traces_sample_rate") | ||
|
||
# If the sample rate is invalid, drop the span | ||
if not is_valid_sample_rate(sample_rate, source=self.__class__.__name__): | ||
logger.warning( | ||
f"[Tracing] Discarding {name} because of invalid sample rate." | ||
) | ||
return dropped(parent_context) | ||
|
||
# Roll the dice on sample rate | ||
sampled = random() < float(sample_rate) | ||
|
||
if sampled: | ||
return SamplingResult(Decision.RECORD_AND_SAMPLE) | ||
else: | ||
return dropped(parent_context) | ||
|
||
def get_description(self) -> str: | ||
return self.__class__.__name__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.