Skip to content

Commit 759ded9

Browse files
authored
ref: Move consts, utils from integrations/opentelemetry/ (#4272)
Moving stuff out of `integrations/opentelemetry/` step by step since there is no OpenTelemetry integration anymore -- it's part of the core SDK. This moves `sentry_sdk/integrations/opentelemetry/{utils,consts}.py` -> `sentry_sdk/opentelemetry/{utils,consts}.py`. There's quite some stuff moving around in this PR in order to remove circular dependencies that have to do with `tracing.py`: - `get_span_status_from_http_code` from `tracing.py` is now in `tracing_utils.py` - various constants from `tracing.py` are now in `consts.py` Additionally, Sphinx was unhappy, so tweaked the way we type some things in `api.py` and `_init_implementation.py`. I'll possibly follow this up with creating a nice structure for the tracing files, maybe a common `tracing` directory with `tracing.py`, `tracing_utils.py` -> `utils.py`, and dedicated `consts.py`. Ref #3853
1 parent 97f1dc9 commit 759ded9

30 files changed

+189
-255
lines changed

Diff for: sentry_sdk/_init_implementation.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from typing import TYPE_CHECKING
22

33
import sentry_sdk
4+
from sentry_sdk.consts import ClientConstructor
45
from sentry_sdk.integrations.opentelemetry.scope import setup_scope_context_management
56

67
if TYPE_CHECKING:
78
from typing import Any, Optional
89

9-
import sentry_sdk.consts
10-
1110

1211
def _check_python_deprecations():
1312
# type: () -> None
@@ -36,7 +35,7 @@ def _init(*args, **kwargs):
3635
# Use `ClientConstructor` to define the argument types of `init` and
3736
# `ContextManager[Any]` to tell static analyzers about the return type.
3837

39-
class init(sentry_sdk.consts.ClientConstructor): # noqa: N801
38+
class init(ClientConstructor): # noqa: N801
4039
pass
4140

4241
else:

Diff for: sentry_sdk/api.py

+14-24
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,7 @@
3030
from typing import Union
3131
from typing import Generator
3232

33-
from sentry_sdk.client import BaseClient
34-
from sentry_sdk._types import (
35-
Event,
36-
Hint,
37-
Breadcrumb,
38-
BreadcrumbHint,
39-
ExcInfo,
40-
MeasurementUnit,
41-
LogLevelStr,
42-
)
43-
from sentry_sdk.tracing import Span
33+
import sentry_sdk
4434

4535
T = TypeVar("T")
4636
F = TypeVar("F", bound=Callable[..., Any])
@@ -102,7 +92,7 @@ def clientmethod(f):
10292

10393
@scopemethod
10494
def get_client():
105-
# type: () -> BaseClient
95+
# type: () -> sentry_sdk.client.BaseClient
10696
return Scope.get_client()
10797

10898

@@ -150,8 +140,8 @@ def last_event_id():
150140

151141
@scopemethod
152142
def capture_event(
153-
event, # type: Event
154-
hint=None, # type: Optional[Hint]
143+
event, # type: sentry_sdk._types.Event
144+
hint=None, # type: Optional[sentry_sdk._types.Hint]
155145
scope=None, # type: Optional[Any]
156146
**scope_kwargs, # type: Any
157147
):
@@ -162,7 +152,7 @@ def capture_event(
162152
@scopemethod
163153
def capture_message(
164154
message, # type: str
165-
level=None, # type: Optional[LogLevelStr]
155+
level=None, # type: Optional[sentry_sdk._types.LogLevelStr]
166156
scope=None, # type: Optional[Any]
167157
**scope_kwargs, # type: Any
168158
):
@@ -174,7 +164,7 @@ def capture_message(
174164

175165
@scopemethod
176166
def capture_exception(
177-
error=None, # type: Optional[Union[BaseException, ExcInfo]]
167+
error=None, # type: Optional[Union[BaseException, sentry_sdk._types.ExcInfo]]
178168
scope=None, # type: Optional[Any]
179169
**scope_kwargs, # type: Any
180170
):
@@ -184,8 +174,8 @@ def capture_exception(
184174

185175
@scopemethod
186176
def add_breadcrumb(
187-
crumb=None, # type: Optional[Breadcrumb]
188-
hint=None, # type: Optional[BreadcrumbHint]
177+
crumb=None, # type: Optional[sentry_sdk._types.Breadcrumb]
178+
hint=None, # type: Optional[sentry_sdk._types.BreadcrumbHint]
189179
**kwargs, # type: Any
190180
):
191181
# type: (...) -> None
@@ -224,7 +214,7 @@ def set_user(value):
224214

225215
@scopemethod
226216
def set_level(value):
227-
# type: (LogLevelStr) -> None
217+
# type: (sentry_sdk._types.LogLevelStr) -> None
228218
return get_isolation_scope().set_level(value)
229219

230220

@@ -238,7 +228,7 @@ def flush(
238228

239229

240230
def start_span(**kwargs):
241-
# type: (Any) -> Span
231+
# type: (Any) -> sentry_sdk.tracing.Span
242232
"""
243233
Start and return a span.
244234
@@ -255,10 +245,10 @@ def start_span(**kwargs):
255245

256246

257247
def start_transaction(
258-
transaction=None, # type: Optional[Span]
248+
transaction=None, # type: Optional[sentry_sdk.tracing.Span]
259249
**kwargs, # type: Any
260250
):
261-
# type: (...) -> Span
251+
# type: (...) -> sentry_sdk.tracing.Span
262252
"""
263253
.. deprecated:: 3.0.0
264254
This function is deprecated and will be removed in a future release.
@@ -298,14 +288,14 @@ def start_transaction(
298288

299289

300290
def set_measurement(name, value, unit=""):
301-
# type: (str, float, MeasurementUnit) -> None
291+
# type: (str, float, sentry_sdk._types.MeasurementUnit) -> None
302292
transaction = get_current_scope().root_span
303293
if transaction is not None:
304294
transaction.set_measurement(name, value, unit)
305295

306296

307297
def get_current_span(scope=None):
308-
# type: (Optional[Scope]) -> Optional[Span]
298+
# type: (Optional[Scope]) -> Optional[sentry_sdk.tracing.Span]
309299
"""
310300
Returns the currently active span if there is one running, otherwise `None`
311301
"""

Diff for: sentry_sdk/consts.py

+40
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,46 @@ class OP:
478478
SOCKET_DNS = "socket.dns"
479479

480480

481+
BAGGAGE_HEADER_NAME = "baggage"
482+
SENTRY_TRACE_HEADER_NAME = "sentry-trace"
483+
484+
DEFAULT_SPAN_ORIGIN = "manual"
485+
DEFAULT_SPAN_NAME = "<unlabeled span>"
486+
487+
488+
# Transaction source
489+
# see https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations
490+
class TransactionSource(str, Enum):
491+
COMPONENT = "component"
492+
CUSTOM = "custom"
493+
ROUTE = "route"
494+
TASK = "task"
495+
URL = "url"
496+
VIEW = "view"
497+
498+
def __str__(self):
499+
# type: () -> str
500+
return self.value
501+
502+
503+
# These are typically high cardinality and the server hates them
504+
LOW_QUALITY_TRANSACTION_SOURCES = [
505+
TransactionSource.URL,
506+
]
507+
508+
SOURCE_FOR_STYLE = {
509+
"endpoint": TransactionSource.COMPONENT,
510+
"function_name": TransactionSource.COMPONENT,
511+
"handler_name": TransactionSource.COMPONENT,
512+
"method_and_path_pattern": TransactionSource.ROUTE,
513+
"path": TransactionSource.URL,
514+
"route_name": TransactionSource.COMPONENT,
515+
"route_pattern": TransactionSource.ROUTE,
516+
"uri_template": TransactionSource.ROUTE,
517+
"url": TransactionSource.ROUTE,
518+
}
519+
520+
481521
# This type exists to trick mypy and PyCharm into thinking `init` and `Client`
482522
# take these arguments (even though they take opaque **kwargs)
483523
class ClientConstructor:

Diff for: sentry_sdk/integrations/aiohttp.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
from functools import wraps
44

55
import sentry_sdk
6-
from sentry_sdk.consts import OP, SPANSTATUS, SPANDATA
6+
from sentry_sdk.consts import (
7+
OP,
8+
SPANSTATUS,
9+
SPANDATA,
10+
BAGGAGE_HEADER_NAME,
11+
SOURCE_FOR_STYLE,
12+
TransactionSource,
13+
)
714
from sentry_sdk.integrations import (
815
_DEFAULT_FAILED_REQUEST_STATUS_CODES,
916
_check_minimum_version,
@@ -17,11 +24,6 @@
1724
_request_headers_to_span_attributes,
1825
request_body_within_bounds,
1926
)
20-
from sentry_sdk.tracing import (
21-
BAGGAGE_HEADER_NAME,
22-
SOURCE_FOR_STYLE,
23-
TransactionSource,
24-
)
2527
from sentry_sdk.tracing_utils import should_propagate_trace
2628
from sentry_sdk.utils import (
2729
capture_internal_exceptions,

Diff for: sentry_sdk/integrations/asgi.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from functools import partial
1111

1212
import sentry_sdk
13-
from sentry_sdk.consts import OP
13+
from sentry_sdk.consts import OP, SOURCE_FOR_STYLE, TransactionSource
1414

1515
from sentry_sdk.integrations._asgi_common import (
1616
_get_headers,
@@ -23,10 +23,6 @@
2323
_request_headers_to_span_attributes,
2424
)
2525
from sentry_sdk.sessions import track_session
26-
from sentry_sdk.tracing import (
27-
SOURCE_FOR_STYLE,
28-
TransactionSource,
29-
)
3026
from sentry_sdk.utils import (
3127
ContextVar,
3228
capture_internal_exceptions,

Diff for: sentry_sdk/integrations/bottle.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import functools
22

33
import sentry_sdk
4-
from sentry_sdk.tracing import SOURCE_FOR_STYLE
4+
from sentry_sdk.consts import SOURCE_FOR_STYLE
55
from sentry_sdk.utils import (
66
capture_internal_exceptions,
77
ensure_integration_enabled,

Diff for: sentry_sdk/integrations/celery/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import sentry_sdk
66
from sentry_sdk import isolation_scope
7-
from sentry_sdk.consts import OP, SPANSTATUS, SPANDATA
7+
from sentry_sdk.consts import OP, SPANSTATUS, SPANDATA, BAGGAGE_HEADER_NAME
88
from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
99
from sentry_sdk.integrations.celery.beat import (
1010
_patch_beat_apply_entry,
@@ -13,7 +13,7 @@
1313
)
1414
from sentry_sdk.integrations.celery.utils import _now_seconds_since_epoch
1515
from sentry_sdk.integrations.logging import ignore_logger
16-
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, TransactionSource
16+
from sentry_sdk.tracing import TransactionSource
1717
from sentry_sdk.tracing_utils import Baggage
1818
from sentry_sdk.utils import (
1919
capture_internal_exceptions,

Diff for: sentry_sdk/integrations/django/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
from importlib import import_module
77

88
import sentry_sdk
9-
from sentry_sdk.consts import OP, SPANDATA
9+
from sentry_sdk.consts import OP, SPANDATA, SOURCE_FOR_STYLE, TransactionSource
1010
from sentry_sdk.scope import add_global_event_processor, should_send_default_pii
1111
from sentry_sdk.serializer import add_global_repr_processor
12-
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource
1312
from sentry_sdk.tracing_utils import add_query_source, record_sql_queries
1413
from sentry_sdk.utils import (
1514
AnnotatedValue,

Diff for: sentry_sdk/integrations/falcon.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import sentry_sdk
2+
from sentry_sdk.consts import SOURCE_FOR_STYLE
23
from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
34
from sentry_sdk.integrations._wsgi_common import RequestExtractor
45
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
5-
from sentry_sdk.tracing import SOURCE_FOR_STYLE
66
from sentry_sdk.utils import (
77
capture_internal_exceptions,
88
ensure_integration_enabled,

Diff for: sentry_sdk/integrations/fastapi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from functools import wraps
44

55
import sentry_sdk
6+
from sentry_sdk.consts import SOURCE_FOR_STYLE, TransactionSource
67
from sentry_sdk.integrations import DidNotEnable
78
from sentry_sdk.scope import should_send_default_pii
8-
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource
99
from sentry_sdk.utils import (
1010
transaction_from_function,
1111
logger,

Diff for: sentry_sdk/integrations/flask.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import sentry_sdk
2+
from sentry_sdk.consts import SOURCE_FOR_STYLE
23
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
34
from sentry_sdk.integrations._wsgi_common import (
45
DEFAULT_HTTP_METHODS_TO_CAPTURE,
56
RequestExtractor,
67
)
78
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
89
from sentry_sdk.scope import should_send_default_pii
9-
from sentry_sdk.tracing import SOURCE_FOR_STYLE
1010
from sentry_sdk.utils import (
1111
capture_internal_exceptions,
1212
ensure_integration_enabled,

Diff for: sentry_sdk/integrations/httpx.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import sentry_sdk
2-
from sentry_sdk.consts import OP, SPANDATA
2+
from sentry_sdk.consts import OP, SPANDATA, BAGGAGE_HEADER_NAME
33
from sentry_sdk.integrations import Integration, DidNotEnable
4-
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME
54
from sentry_sdk.tracing_utils import Baggage, should_propagate_trace
65
from sentry_sdk.utils import (
76
SENSITIVE_DATA_SUBSTITUTE,

Diff for: sentry_sdk/integrations/huey.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
import sentry_sdk
55
from sentry_sdk.api import get_baggage, get_traceparent
6-
from sentry_sdk.consts import OP, SPANSTATUS
7-
from sentry_sdk.integrations import DidNotEnable, Integration
8-
from sentry_sdk.scope import should_send_default_pii
9-
from sentry_sdk.tracing import (
6+
from sentry_sdk.consts import (
7+
OP,
8+
SPANSTATUS,
109
BAGGAGE_HEADER_NAME,
1110
SENTRY_TRACE_HEADER_NAME,
1211
TransactionSource,
1312
)
13+
from sentry_sdk.integrations import DidNotEnable, Integration
14+
from sentry_sdk.scope import should_send_default_pii
1415
from sentry_sdk.utils import (
1516
capture_internal_exceptions,
1617
ensure_integration_enabled,

Diff for: sentry_sdk/integrations/litestar.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections.abc import Set
22
import sentry_sdk
3-
from sentry_sdk.consts import OP
3+
from sentry_sdk.consts import OP, TransactionSource, SOURCE_FOR_STYLE
44
from sentry_sdk.integrations import (
55
_DEFAULT_FAILED_REQUEST_STATUS_CODES,
66
DidNotEnable,
@@ -9,7 +9,6 @@
99
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
1010
from sentry_sdk.integrations.logging import ignore_logger
1111
from sentry_sdk.scope import should_send_default_pii
12-
from sentry_sdk.tracing import TransactionSource, SOURCE_FOR_STYLE
1312
from sentry_sdk.utils import (
1413
ensure_integration_enabled,
1514
event_from_exception,

Diff for: sentry_sdk/integrations/opentelemetry/scope.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
use_span,
1818
)
1919

20-
from sentry_sdk.integrations.opentelemetry.consts import (
20+
from sentry_sdk.opentelemetry.consts import (
2121
SENTRY_SCOPES_KEY,
2222
SENTRY_FORK_ISOLATION_SCOPE_KEY,
2323
SENTRY_USE_CURRENT_SCOPE_KEY,
2424
SENTRY_USE_ISOLATION_SCOPE_KEY,
2525
TRACESTATE_SAMPLED_KEY,
2626
)
27-
from sentry_sdk.integrations.opentelemetry.utils import trace_state_from_baggage
2827
from sentry_sdk.opentelemetry.contextvars_context import (
2928
SentryContextVarsRuntimeContext,
3029
)
30+
from sentry_sdk.opentelemetry.utils import trace_state_from_baggage
3131
from sentry_sdk.scope import Scope, ScopeType
3232
from sentry_sdk.tracing import Span
3333
from sentry_sdk._types import TYPE_CHECKING

Diff for: sentry_sdk/integrations/pyramid.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import weakref
55

66
import sentry_sdk
7+
from sentry_sdk.consts import SOURCE_FOR_STYLE
78
from sentry_sdk.integrations import Integration, DidNotEnable
89
from sentry_sdk.integrations._wsgi_common import RequestExtractor
910
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
1011
from sentry_sdk.scope import should_send_default_pii
11-
from sentry_sdk.tracing import SOURCE_FOR_STYLE
1212
from sentry_sdk.utils import (
1313
capture_internal_exceptions,
1414
ensure_integration_enabled,

Diff for: sentry_sdk/integrations/quart.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from functools import wraps
44

55
import sentry_sdk
6+
from sentry_sdk.consts import SOURCE_FOR_STYLE
67
from sentry_sdk.integrations import DidNotEnable, Integration
78
from sentry_sdk.integrations._wsgi_common import _filter_headers
89
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
910
from sentry_sdk.scope import should_send_default_pii
10-
from sentry_sdk.tracing import SOURCE_FOR_STYLE
1111
from sentry_sdk.utils import (
1212
capture_internal_exceptions,
1313
ensure_integration_enabled,

0 commit comments

Comments
 (0)