-
Notifications
You must be signed in to change notification settings - Fork 589
ref: Add sampling to span first (11) #5617
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
Changes from all commits
946decc
f3ee55c
47ed910
5023c76
6445447
47e6211
1e7b694
80bfe5a
1f0ffc1
d773428
647fa79
49bdbe6
cdd8bd6
54f81af
941863e
4b14e8d
474f8e6
9996e29
e20d4fd
f2738ff
7874a54
63a9396
c974d3e
5d8c238
831adae
656ef2e
1dcf176
0a7eae8
6888c56
09e5cce
ae2fd52
f223574
05a4157
9e8e60e
777a246
9b1e2f3
e589c53
1487ea8
1006e7b
6c16dbf
cb37a07
ad6e7cc
ba29f0c
d6a42b2
5e20ad3
c70fae4
b995770
0235053
60217e1
b673a09
d6fa965
3602f86
9f59eb0
bd8e1c9
9b3df81
8614d52
cdee8bc
72f0968
dab1970
7daa720
b59f3cd
2f0dc01
dc81637
bc9f765
45372c1
c5fcb3e
51342fb
336b643
aba1b50
09b88f0
2ac24d3
a9b33a9
4ba4351
5d8c5f3
73e33ea
bd9e0a3
918609c
e850994
f816c0a
62deebf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| to_string, | ||
| try_convert, | ||
| is_sentry_url, | ||
| is_valid_sample_rate, | ||
| _is_external_source, | ||
| _is_in_project_root, | ||
| _module_in_list, | ||
|
|
@@ -41,6 +42,8 @@ | |
|
|
||
| from types import FrameType | ||
|
|
||
| from sentry_sdk._types import Attributes | ||
|
|
||
|
|
||
| SENTRY_TRACE_REGEX = re.compile( | ||
| "^[ \t]*" # whitespace | ||
|
|
@@ -1379,6 +1382,89 @@ def add_sentry_baggage_to_headers( | |
| ) | ||
|
|
||
|
|
||
| def _make_sampling_decision( | ||
| name: str, | ||
| attributes: "Optional[Attributes]", | ||
| scope: "sentry_sdk.Scope", | ||
| ) -> "tuple[bool, Optional[float], Optional[float], Optional[str]]": | ||
| """ | ||
| Decide whether a span should be sampled. | ||
|
|
||
| Returns a tuple with: | ||
| 1. the sampling decision | ||
| 2. the effective sample rate | ||
| 3. the sample rand | ||
| 4. the reason for not sampling the span, if unsampled | ||
| """ | ||
| client = sentry_sdk.get_client() | ||
|
|
||
| if not has_tracing_enabled(client.options): | ||
| return False, None, None, None | ||
sentrivana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| propagation_context = scope.get_active_propagation_context() | ||
|
|
||
| sample_rand = None | ||
| if propagation_context.baggage is not None: | ||
| sample_rand = propagation_context.baggage._sample_rand() | ||
| if sample_rand is None: | ||
| sample_rand = _generate_sample_rand(propagation_context.trace_id) | ||
|
|
||
| # If there's a traces_sampler, use that; otherwise use traces_sample_rate | ||
| traces_sampler_defined = callable(client.options.get("traces_sampler")) | ||
| if traces_sampler_defined: | ||
| sampling_context = { | ||
| "name": name, | ||
| "trace_id": propagation_context.trace_id, | ||
| "parent_span_id": propagation_context.parent_span_id, | ||
| "parent_sampled": propagation_context.parent_sampled, | ||
| "attributes": dict(attributes) if attributes else {}, | ||
| } | ||
|
|
||
| sample_rate = client.options["traces_sampler"](sampling_context) | ||
| else: | ||
| if propagation_context.parent_sampled is not None: | ||
| sample_rate = propagation_context.parent_sampled | ||
sentrivana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else: | ||
| sample_rate = client.options["traces_sample_rate"] | ||
|
|
||
| # Validate whether the sample_rate we got is actually valid. Since | ||
| # traces_sampler is user-provided, it could return anything. | ||
| if not is_valid_sample_rate(sample_rate, source="Tracing"): | ||
| logger.warning(f"[Tracing] Discarding {name} because of invalid sample rate.") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bigger refactor that would happen outside of this PR (if we do decide to do this), but I wonder if we should consider leveraging the If a user is trying to determine if/why spans were dropped, this would help with that effort.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think more info in log messages is always nice, so we can def do something like this in the future.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made PY-2133 to track this, might be a future maintenance day exploration for me 👀 |
||
| return False, None, None, "sample_rate" | ||
|
|
||
| sample_rate = float(sample_rate) | ||
| if not sample_rate: | ||
| if traces_sampler_defined: | ||
| reason = "traces_sampler returned 0 or False" | ||
| else: | ||
| reason = "traces_sample_rate is set to 0" | ||
|
|
||
| logger.debug(f"[Tracing] Discarding {name} because {reason}") | ||
| return False, 0.0, None, "sample_rate" | ||
|
|
||
| # Adjust sample rate if we're under backpressure | ||
| if client.monitor: | ||
| sample_rate /= 2**client.monitor.downsample_factor | ||
|
|
||
| if not sample_rate: | ||
| logger.debug(f"[Tracing] Discarding {name} because backpressure") | ||
| return False, 0.0, None, "backpressure" | ||
|
|
||
| sampled = sample_rand < sample_rate | ||
|
|
||
| if sampled: | ||
| logger.debug(f"[Tracing] Starting {name}") | ||
| outcome = None | ||
| else: | ||
| logger.debug( | ||
| f"[Tracing] Discarding {name} because it's not included in the random sample (sampling rate = {sample_rate})" | ||
| ) | ||
| outcome = "sample_rate" | ||
|
|
||
| return sampled, sample_rate, sample_rand, outcome | ||
|
|
||
|
|
||
| # Circular imports | ||
| from sentry_sdk.tracing import ( | ||
| BAGGAGE_HEADER_NAME, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.