Skip to content

Commit 7afe4bb

Browse files
committed
naive impl
1 parent 2c29711 commit 7afe4bb

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

sentry_sdk/api.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import inspect
22
from contextlib import contextmanager
33

4-
from sentry_sdk import tracing_utils, Client
4+
from sentry_sdk import tracing, tracing_utils, Client
55
from sentry_sdk._types import TYPE_CHECKING
66
from sentry_sdk.consts import INSTRUMENTER
77
from sentry_sdk.scope import Scope, _ScopeManager, new_scope, isolation_scope
@@ -282,15 +282,20 @@ def flush(
282282
return Scope.get_client().flush(timeout=timeout, callback=callback)
283283

284284

285-
@scopemethod
286285
def start_span(
287286
**kwargs, # type: Any
288287
):
289288
# type: (...) -> Span
290-
return Scope.get_current_scope().start_span(**kwargs)
289+
return tracing.start_span(**kwargs)
290+
291+
292+
def start_inactive_span(
293+
**kwargs, # type: Any
294+
):
295+
# type: (...) -> Span
296+
return tracing.start_inactive_span(**kwargs)
291297

292298

293-
@scopemethod
294299
def start_transaction(
295300
transaction=None, # type: Optional[Transaction]
296301
instrumenter=INSTRUMENTER.SENTRY, # type: str
@@ -299,6 +304,10 @@ def start_transaction(
299304
):
300305
# type: (...) -> Union[Transaction, NoOpSpan]
301306
"""
307+
.. deprecated:: 3.0.0
308+
This function is deprecated and will be removed in a future release.
309+
Use :py:meth:`sentry_sdk.start_span` instead.
310+
302311
Start and return a transaction on the current scope.
303312
304313
Start an existing transaction if given, otherwise create and start a new
@@ -328,7 +337,7 @@ def start_transaction(
328337
constructor. See :py:class:`sentry_sdk.tracing.Transaction` for
329338
available arguments.
330339
"""
331-
return Scope.get_current_scope().start_transaction(
340+
return tracing.start_transaction(
332341
transaction, instrumenter, custom_sampling_context, **kwargs
333342
)
334343

sentry_sdk/tracing.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import random
33
from datetime import datetime, timedelta, timezone
44

5+
from opentelemetry import trace as otel_trace, context
6+
from opentelemetry.sdk.trace import ReadableSpan
7+
58
import sentry_sdk
69
from sentry_sdk.consts import INSTRUMENTER, SPANDATA
710
from sentry_sdk.profiler.continuous_profiler import get_profiler_id
@@ -198,6 +201,7 @@ class Span:
198201
:param start_timestamp: The timestamp when the span started. If omitted, the current time
199202
will be used.
200203
:param scope: The scope to use for this span. If not provided, we use the current scope.
204+
:param otel_span: The underlying OTel span this span is wrapping.
201205
"""
202206

203207
__slots__ = (
@@ -1156,6 +1160,35 @@ def _set_initial_sampling_decision(self, sampling_context):
11561160
pass
11571161

11581162

1163+
class OTelSpanWrapper:
1164+
"""
1165+
Light-weight OTel span wrapper.
1166+
1167+
Meant for providing some level of compatibility with the old span interface.
1168+
"""
1169+
1170+
def __init__(self, otel_span=None, **kwargs):
1171+
# type: (Optional[ReadableSpan]) -> None
1172+
self.description = kwargs.get("description")
1173+
self._otel_span = otel_span
1174+
1175+
def __enter__(self):
1176+
# type: () -> OTelSpanWrapper
1177+
# Creates a Context object with parent set as current span
1178+
ctx = otel_trace.set_span_in_context(self._otel_span)
1179+
# Set as the implicit current context
1180+
self._ctx_token = context.attach(ctx)
1181+
return self
1182+
1183+
def __exit__(self, ty, value, tb):
1184+
# type: (Optional[Any], Optional[Any], Optional[Any]) -> None
1185+
self._otel_span.end()
1186+
context.detach(self._ctx_token)
1187+
1188+
def start_child(self, **kwargs):
1189+
pass
1190+
1191+
11591192
if TYPE_CHECKING:
11601193

11611194
@overload
@@ -1198,6 +1231,21 @@ async def my_async_function():
11981231
return start_child_span_decorator
11991232

12001233

1234+
def start_span(**kwargs):
1235+
otel_span = otel_trace.get_tracer(__name__).start_span()
1236+
span = OTelSpanWrapper(otel_span=otel_span, **kwargs)
1237+
return span
1238+
1239+
1240+
def start_inactive_span(**kwargs):
1241+
pass
1242+
1243+
1244+
def start_transaction(**kwargs):
1245+
# XXX force_transaction?
1246+
pass
1247+
1248+
12011249
# Circular imports
12021250

12031251
from sentry_sdk.tracing_utils import (

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def get_file_text(file_name):
4141
install_requires=[
4242
"urllib3>=1.26.11",
4343
"certifi",
44+
"opentelemetry-distro>=0.35b0", # XXX check lower bound
4445
],
4546
extras_require={
4647
"aiohttp": ["aiohttp>=3.5"],

0 commit comments

Comments
 (0)