Skip to content

Commit 18103bc

Browse files
committed
Remove custom_sampling_context
1 parent e850655 commit 18103bc

File tree

6 files changed

+39
-33
lines changed

6 files changed

+39
-33
lines changed

MIGRATION_GUIDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
1919
- Redis integration: In Redis pipeline spans there is no `span["data"]["redis.commands"]` that contains a dict `{"count": 3, "first_ten": ["cmd1", "cmd2", ...]}` but instead `span["data"]["redis.commands.count"]` (containing `3`) and `span["data"]["redis.commands.first_ten"]` (containing `["cmd1", "cmd2", ...]`).
2020
- clickhouse-driver integration: The query is now available under the `db.query.text` span attribute (only if `send_default_pii` is `True`).
2121
- `sentry_sdk.init` now returns `None` instead of a context manager.
22+
- The sampling context accessible in `traces_sampler` has changed. It now contains all span attributes. The original `sampling_context["transaction_context"]["name"]` and `sampling_context["transaction_context"]["op"]` are now accessible as `sampling_context["sentry_name"]` and `sampling_context["sentry_op"]`, respectively.
2223

2324
### Removed
2425

2526
- Spans no longer have a `description`. Use `name` instead.
2627
- Dropped support for Python 3.6.
28+
- The `custom_sampling_context` parameter of `start_transaction` has been removed.
2729
- The PyMongo integration no longer sets tags. The data is still accessible via span attributes.
2830
- The PyMongo integration doesn't set `operation_ids` anymore. The individual IDs (`operation_id`, `request_id`, `session_id`) are now accessible as separate span attributes.
2931
- `sentry_sdk.metrics` and associated metrics APIs have been removed as Sentry no longer accepts metrics data in this form. See https://sentry.zendesk.com/hc/en-us/articles/26369339769883-Upcoming-API-Changes-to-Metrics

sentry_sdk/api.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
ExcInfo,
4141
MeasurementUnit,
4242
LogLevelStr,
43-
SamplingContext,
4443
)
4544
from sentry_sdk.tracing import Span, TransactionKwargs
4645

@@ -239,12 +238,8 @@ def flush(
239238
return get_client().flush(timeout=timeout, callback=callback)
240239

241240

242-
def start_span(
243-
*,
244-
custom_sampling_context=None,
245-
**kwargs, # type: Any
246-
):
247-
# type: (...) -> POTelSpan
241+
def start_span(**kwargs):
242+
# type: (type.Any) -> POTelSpan
248243
"""
249244
Start and return a span.
250245
@@ -257,13 +252,11 @@ def start_span(
257252
of the `with` block. If not using context managers, call the `finish()`
258253
method.
259254
"""
260-
# TODO: Consider adding type hints to the method signature.
261-
return get_current_scope().start_span(custom_sampling_context, **kwargs)
255+
return get_current_scope().start_span(**kwargs)
262256

263257

264258
def start_transaction(
265259
transaction=None, # type: Optional[Transaction]
266-
custom_sampling_context=None, # type: Optional[SamplingContext]
267260
**kwargs, # type: Unpack[TransactionKwargs]
268261
):
269262
# type: (...) -> POTelSpan
@@ -295,14 +288,12 @@ def start_transaction(
295288
296289
:param transaction: The transaction to start. If omitted, we create and
297290
start a new transaction.
298-
:param custom_sampling_context: The transaction's custom sampling context.
299291
:param kwargs: Optional keyword arguments to be passed to the Transaction
300292
constructor. See :py:class:`sentry_sdk.tracing.Transaction` for
301293
available arguments.
302294
"""
303295
return start_span(
304296
span=transaction,
305-
custom_sampling_context=custom_sampling_context,
306297
**kwargs,
307298
)
308299

sentry_sdk/integrations/opentelemetry/consts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ class SentrySpanAttribute:
3030
NAME = "sentry.name"
3131
SOURCE = "sentry.source"
3232
CONTEXT = "sentry.context"
33+
CUSTOM_SAMPLED = "sentry.custom_sampled"
34+
PARENT_SAMPLED = "sentry.parent_sampled"

sentry_sdk/integrations/opentelemetry/sampler.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import cast
33

44
from opentelemetry import trace
5-
65
from opentelemetry.sdk.trace.sampling import Sampler, SamplingResult, Decision
76
from opentelemetry.trace.span import TraceState
87

@@ -12,6 +11,7 @@
1211
from sentry_sdk.integrations.opentelemetry.consts import (
1312
TRACESTATE_SAMPLED_KEY,
1413
TRACESTATE_SAMPLE_RATE_KEY,
14+
SentrySpanAttribute,
1515
)
1616

1717
from typing import TYPE_CHECKING
@@ -118,25 +118,21 @@ def should_sample(
118118
if not has_tracing_enabled(client.options):
119119
return dropped_result(parent_span_context, attributes)
120120

121-
sample_rate = None
121+
# Explicit sampled value provided at start_span
122+
if attributes.get(SentrySpanAttribute.CUSTOM_SAMPLED) is not None:
123+
sample_rate = float(attributes[SentrySpanAttribute.CUSTOM_SAMPLED])
124+
return sampled_result(parent_span_context, attributes, sample_rate)
122125

123-
# Check if sampled=True was passed to start_transaction
124-
# TODO-anton: Do we want to keep the start_transaction(sampled=True) thing?
126+
sample_rate = None
125127

126128
# Check if there is a traces_sampler
127129
# Traces_sampler is responsible to check parent sampled to have full transactions.
128130
has_traces_sampler = callable(client.options.get("traces_sampler"))
129131
if has_traces_sampler:
130-
# TODO-anton: Make proper sampling_context
131-
# TODO-neel-potel: Make proper sampling_context
132-
sampling_context = {
133-
"transaction_context": {
134-
"name": name,
135-
},
136-
"parent_sampled": get_parent_sampled(parent_span_context, trace_id),
137-
}
138-
139-
sample_rate = client.options["traces_sampler"](sampling_context)
132+
attributes[SentrySpanAttribute.PARENT_SAMPLED] = get_parent_sampled(
133+
parent_span_context, trace_id
134+
)
135+
sample_rate = client.options["traces_sampler"](attributes)
140136

141137
else:
142138
# Check if there is a parent with a sampling decision

sentry_sdk/integrations/opentelemetry/scope.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ def start_transaction(self, custom_sampling_context=None, **kwargs):
123123

124124
def start_span(self, custom_sampling_context=None, **kwargs):
125125
# type: (Optional[SamplingContext], Any) -> POTelSpan
126-
return POTelSpan(**kwargs, scope=self)
126+
return POTelSpan(
127+
**kwargs, custom_sampling_context=custom_sampling_context, scope=self
128+
)
127129

128130

129131
_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT)

sentry_sdk/tracing.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
from typing_extensions import TypedDict, Unpack
4343

44+
from opentelemetry.utils import types as OTelSpanAttributes
45+
4446
P = ParamSpec("P")
4547
R = TypeVar("R")
4648

@@ -683,9 +685,9 @@ def get_trace_context(self):
683685
rv["status"] = self.status
684686

685687
if self.containing_transaction:
686-
rv[
687-
"dynamic_sampling_context"
688-
] = self.containing_transaction.get_baggage().dynamic_sampling_context()
688+
rv["dynamic_sampling_context"] = (
689+
self.containing_transaction.get_baggage().dynamic_sampling_context()
690+
)
689691

690692
data = {}
691693

@@ -1200,10 +1202,12 @@ def __init__(
12001202
op=None, # type: Optional[str]
12011203
description=None, # type: Optional[str]
12021204
status=None, # type: Optional[str]
1205+
sampled=None, # type: Optional[bool]
12031206
start_timestamp=None, # type: Optional[Union[datetime, float]]
12041207
origin=None, # type: Optional[str]
12051208
name=None, # type: Optional[str]
12061209
source=TRANSACTION_SOURCE_CUSTOM, # type: str
1210+
attributes=None, # type: OTelSpanAttributes
12071211
otel_span=None, # type: Optional[OtelSpan]
12081212
**_, # type: dict[str, object]
12091213
):
@@ -1218,6 +1222,7 @@ def __init__(
12181222
if otel_span is not None:
12191223
self._otel_span = otel_span
12201224
else:
1225+
from sentry_sdk.integrations.opentelemetry.consts import SentrySpanAttribute
12211226
from sentry_sdk.integrations.opentelemetry.utils import (
12221227
convert_to_otel_timestamp,
12231228
)
@@ -1227,12 +1232,20 @@ def __init__(
12271232
start_timestamp = convert_to_otel_timestamp(start_timestamp)
12281233

12291234
span_name = name or description or op or ""
1230-
self._otel_span = tracer.start_span(span_name, start_time=start_timestamp)
1235+
1236+
# Prepopulate some attrs to be accessible in traces_sampler
1237+
attributes = attributes or {}
1238+
attributes[SentrySpanAttribute.NAME] = span_name
1239+
attributes[SentrySpanAttribute.OP] = op
1240+
if sampled is not None:
1241+
attributes[SentrySpanAttribute.CUSTOM_SAMPLED] = sampled
1242+
1243+
self._otel_span = tracer.start_span(
1244+
span_name, start_time=start_timestamp, attributes=attributes
1245+
)
12311246

12321247
self.origin = origin or DEFAULT_SPAN_ORIGIN
1233-
self.op = op
12341248
self.description = description
1235-
self.name = span_name
12361249
self.source = source
12371250

12381251
if status is not None:

0 commit comments

Comments
 (0)