8000 Create spans on scope (#3442) · getsentry/sentry-python@74d62f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 74d62f9

Browse files
authored
Create spans on scope (#3442)
1 parent 67a5823 commit 74d62f9

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

MIGRATION_GUIDE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
99

1010
### Changed
1111

12+
- `sentry_sdk.start_span` now only takes keyword arguments.
13+
1214
### Removed
1315

1416
### Deprecated
1517

18+
- `sentry_sdk.start_transaction` is deprecated. Use `sentry_sdk.start_span` instead.
1619

1720
## Upgrading to 2.0
1821

sentry_sdk/api.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import inspect
22

3-
from sentry_sdk import tracing, tracing_utils, Client
3+
from sentry_sdk import tracing_utils, Client
44
from sentry_sdk._init_implementation import init
55
from sentry_sdk.tracing import POTelSpan, Transaction, trace
66
from sentry_sdk.crons import monitor
@@ -233,14 +233,26 @@ def flush(
233233

234234

235235
def start_span(
236+
*,
237+
span=None,
238+
custom_sampling_context=None,
236239
**kwargs, # type: Any
237240
):
238241
# type: (...) -> POTelSpan
239242
"""
240-
Alias for tracing.POTelSpan constructor. The method signature is the same.
243+
Start and return a span.
244+
245+
This is the entry point to manual tracing instrumentation.
246+
247+
A tree structure can be built by adding child spans to the span.
248+
To start a new child span within the span, call the `start_child()` method.
249+
250+
When used as a context manager, spans are automatically finished at the end
251+
of the `with` block. If not using context managers, call the `finish()`
252+
method.
241253
"""
242254
# TODO: Consider adding type hints to the method signature.
243-
return tracing.POTelSpan(**kwargs)
255+
return get_current_scope().start_span(span, custom_sampling_context, **kwargs)
244256

245257

246258
def start_transaction(
@@ -282,7 +294,11 @@ def start_transaction(
282294
constructor. See :py:class:`sentry_sdk.tracing.Transaction` for
283295
available arguments.
284296
"""
285-
return start_span(**kwargs)
297+
return start_span(
298+
span=transaction,
299+
custom_sampling_context=custom_sampling_context,
300+
**kwargs,
301+
)
286302

287303

288304
def set_measurement(name, value, unit=""):

sentry_sdk/scope.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
NoOpSpan,
2626
Span,
2727
Transaction,
28+
POTelSpan,
2829
)
2930
from sentry_sdk._types import TYPE_CHECKING
3031
from sentry_sdk.utils import (
@@ -963,6 +964,10 @@ def start_transaction(
963964
):
964965
# type: (Optional[Transaction], Optional[SamplingContext], Unpack[TransactionKwargs]) -> Union[Transaction, NoOpSpan]
965966
"""
967+
.. deprecated:: 3.0.0
968+
This function is deprecated and will be removed in a future release.
969+
Use :py:meth:`sentry_sdk.start_span` instead.
970+
966971
Start and return a transaction.
967972
968973
Start an existing transaction if given, otherwise create and start a new
@@ -993,19 +998,12 @@ def start_transaction(
993998
"""
994999
kwargs.setdefault("scope", self)
9951000

996-
client = self.get_client()
997-
9981001
try_autostart_continuous_profiler()
9991002

10001003
custom_sampling_context = custom_sampling_context or {}
10011004

1002-
# kwargs at this point has type TransactionKwargs, since we have removed
1003-
# the client and custom_sampling_context from it.
1004-
transaction_kwargs = kwargs # type: TransactionKwargs
1005-
10061005
# if we haven't been given a transaction, make one
1007-
if transaction is None:
1008-
transaction = Transaction(**transaction_kwargs)
1006+
transaction = transaction or POTelSpan(**kwargs)
10091007

10101008
# use traces_sample_rate, traces_sampler, and/or inheritance to make a
10111009
# sampling decision
@@ -1024,39 +1022,24 @@ def start_transaction(
10241022

10251023
transaction._profile = profile
10261024

1027-
# we don't bother to keep spans if we already know we're not going to
1028-
# send the transaction
1029-
max_spans = (client.options["_experiments"].get("max_spans")) or 1000
1030-
transaction.init_span_recorder(maxlen=max_spans)
1031-
10321025
return transaction
10331026

1034-
def start_span(self, **kwargs):
1035-
# type: (Any) -> Span
1027+
def start_span(self, span=None, custom_sampling_context=None, **kwargs):
1028+
# type: (Optional[Span], Optional[SamplingContext], Any) -> Span
10361029
"""
1037-
Start a span whose parent is the currently active span or transaction, if any.
1030+
Start a span whose parent is the currently active span, if any.
10381031
10391032
The return value is a :py:class:`sentry_sdk.tracing.Span` instance,
10401033
typically used as a context manager to start and stop timing in a `with`
10411034
block.
10421035
1043-
Only spans contained in a transaction are sent to Sentry. Most
1044-
integrations start a transaction at the appropriate time, for example
1045-
for every incoming HTTP request. Use
1046-
:py:meth:`sentry_sdk.start_transaction` to start a new transaction when
1047-
one is not already in progress.
1048-
10491036
For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Span`.
1050-
1051-
The instrumenter parameter is deprecated for user code, and it will
1052-
be removed in the next major version. Going forward, it should only
1053-
be used by the SDK itself.
10541037
"""
10551038
with new_scope():
10561039
kwargs.setdefault("scope", self)
10571040

10581041
# get current span or transaction
1059-
span = self.span or self.get_isolation_scope().span
1042+
span = span or self.span or self.get_isolation_scope().span
10601043

10611044
if span is None:
10621045
# New spans get the `trace_id` from the scope
@@ -1065,7 +1048,7 @@ def start_span(self, **kwargs):
10651048
if propagation_context is not None:
10661049
kwargs["trace_id"] = propagation_context.trace_id
10671050

1068-
span = Span(**kwargs)
1051+
span = POTelSpan(**kwargs)
10691052
else:
10701053
# Children take `trace_id`` from the parent span.
10711054
span = span.start_child(**kwargs)

sentry_sdk/tracing.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,10 @@ def containing_transaction(self):
13081308

13091309
def start_child(self, **kwargs):
13101310
# type: (str, **Any) -> POTelSpan
1311-
pass
1311+
kwargs.setdefault("sampled", self.sampled)
1312+
1313+
span = POTelSpan(**kwargs)
1314+
return span
13121315

13131316
@classmethod
13141317
def continue_from_environ(
@@ -1317,7 +1320,9 @@ def continue_from_environ(
13171320
**kwargs, # type: Any
13181321
):
13191322
# type: (...) -> POTelSpan
1320-
pass
1323+
# XXX actually propagate
1324+
span = POTelSpan(**kwargs)
1325+
return span
13211326

13221327
@classmethod
13231328
def continue_from_headers(
@@ -1326,7 +1331,9 @@ def continue_from_headers(
13261331
**kwargs, # type: Any
13271332
):
13281333
# type: (...) -> POTelSpan
1329-
pass
1334+
# XXX actually propagate
1335+
span = POTelSpan(**kwargs)
1336+
return span
13301337

13311338
def iter_headers(self):
13321339
# type: () -> Iterator[Tuple[str, str]]
@@ -1339,7 +1346,9 @@ def from_traceparent(
13391346
**kwargs, # type: Any
13401347
):
13411348
# type: (...) -> Optional[Transaction]
1342-
pass
1349+
# XXX actually propagate
1350+
span = POTelSpan(**kwargs)
1351+
return span
13431352

13441353
def to_traceparent(self):
13451354
# type: () -> str

0 commit comments

Comments
 (0)
0