Skip to content

Commit 189e4a9

Browse files
ref(tracing): Move TRANSACTION_SOURCE_* constants to Enum (#3889)
Change the `TRANSACTION_SOURCE_*` constants defined in `tracing.py` to be enums, for better developer experience. Fixes GH-2696 --------- Co-authored-by: Anton Pirker <[email protected]>
1 parent eeedd11 commit 189e4a9

27 files changed

+99
-99
lines changed

CHANGELOG.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2328,7 +2328,7 @@ By: @mgaligniana (#1773)
23282328

23292329
import sentry_sdk
23302330
from sentry_sdk.integrations.arq import ArqIntegration
2331-
from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT
2331+
from sentry_sdk.tracing import TransactionSource
23322332

23332333
sentry_sdk.init(
23342334
dsn="...",
@@ -2348,7 +2348,7 @@ By: @mgaligniana (#1773)
23482348
await ctx['session'].aclose()
23492349

23502350
async def main():
2351-
with sentry_sdk.start_transaction(name="testing_arq_tasks", source=TRANSACTION_SOURCE_COMPONENT):
2351+
with sentry_sdk.start_transaction(name="testing_arq_tasks", source=TransactionSource.COMPONENT):
23522352
redis = await create_pool(RedisSettings())
23532353
for url in ('https://facebook.com', 'https://microsoft.com', 'https://github.com', "asdf"
23542354
):
@@ -2422,7 +2422,7 @@ By: @mgaligniana (#1773)
24222422

24232423
import sentry_sdk
24242424
from sentry_sdk.integrations.huey import HueyIntegration
2425-
from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT, Transaction
2425+
from sentry_sdk.tracing import TransactionSource, Transaction
24262426

24272427

24282428
def main():
@@ -2434,7 +2434,7 @@ By: @mgaligniana (#1773)
24342434
traces_sample_rate=1.0,
24352435
)
24362436

2437-
with sentry_sdk.start_transaction(name="testing_huey_tasks", source=TRANSACTION_SOURCE_COMPONENT):
2437+
with sentry_sdk.start_transaction(name="testing_huey_tasks", source=TransactionSource.COMPONENT):
24382438
r = add_numbers(1, 2)
24392439

24402440
if __name__ == "__main__":

sentry_sdk/integrations/aiohttp.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from sentry_sdk.tracing import (
2121
BAGGAGE_HEADER_NAME,
2222
SOURCE_FOR_STYLE,
23-
TRANSACTION_SOURCE_ROUTE,
23+
TransactionSource,
2424
)
2525
from sentry_sdk.tracing_utils import should_propagate_trace
2626
from sentry_sdk.utils import (
@@ -129,7 +129,7 @@ async def sentry_app_handle(self, request, *args, **kwargs):
129129
# If this transaction name makes it to the UI, AIOHTTP's
130130
# URL resolver did not find a route or died trying.
131131
name="generic AIOHTTP request",
132-
source=TRANSACTION_SOURCE_ROUTE,
132+
source=TransactionSource.ROUTE,
133133
origin=AioHttpIntegration.origin,
134134
)
135135
with sentry_sdk.start_transaction(

sentry_sdk/integrations/arq.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
66
from sentry_sdk.integrations.logging import ignore_logger
77
from sentry_sdk.scope import should_send_default_pii
8-
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_TASK
8+
from sentry_sdk.tracing import Transaction, TransactionSource
99
from sentry_sdk.utils import (
1010
capture_internal_exceptions,
1111
ensure_integration_enabled,
@@ -102,7 +102,7 @@ async def _sentry_run_job(self, job_id, score):
102102
name="unknown arq task",
103103
status="ok",
104104
op=OP.QUEUE_TASK_ARQ,
105-
source=TRANSACTION_SOURCE_TASK,
105+
source=TransactionSource.TASK,
106106
origin=ArqIntegration.origin,
107107
)
108108

sentry_sdk/integrations/asgi.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
from sentry_sdk.sessions import track_session
2626
from sentry_sdk.tracing import (
2727
SOURCE_FOR_STYLE,
28-
TRANSACTION_SOURCE_ROUTE,
29-
TRANSACTION_SOURCE_URL,
30-
TRANSACTION_SOURCE_COMPONENT,
31-
TRANSACTION_SOURCE_CUSTOM,
28+
TransactionSource,
3229
)
3330
from sentry_sdk.utils import (
3431
ContextVar,
@@ -273,9 +270,9 @@ def event_processor(self, event, hint, asgi_scope):
273270
already_set = event["transaction"] != _DEFAULT_TRANSACTION_NAME and event[
274271
"transaction_info"
275272
].get("source") in [
276-
TRANSACTION_SOURCE_COMPONENT,
277-
TRANSACTION_SOURCE_ROUTE,
278-
TRANSACTION_SOURCE_CUSTOM,
273+
TransactionSource.COMPONENT,
274+
TransactionSource.ROUTE,
275+
TransactionSource.CUSTOM,
279276
]
280277
if not already_set:
281278
name, source = self._get_transaction_name_and_source(
@@ -313,7 +310,7 @@ def _get_transaction_name_and_source(self, transaction_style, asgi_scope):
313310
name = transaction_from_function(endpoint) or ""
314311
else:
315312
name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
316-
source = TRANSACTION_SOURCE_URL
313+
source = TransactionSource.URL
317314

318315
elif transaction_style == "url":
319316
# FastAPI includes the route object in the scope to let Sentry extract the
@@ -325,11 +322,11 @@ def _get_transaction_name_and_source(self, transaction_style, asgi_scope):
325322
name = path
326323
else:
327324
name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
328-
source = TRANSACTION_SOURCE_URL
325+
source = TransactionSource.URL
329326

330327
if name is None:
331328
name = _DEFAULT_TRANSACTION_NAME
332-
source = TRANSACTION_SOURCE_ROUTE
329+
source = TransactionSource.ROUTE
333330
return name, source
334331

335332
return name, source

sentry_sdk/integrations/aws_lambda.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sentry_sdk.api import continue_trace
1111
from sentry_sdk.consts import OP
1212
from sentry_sdk.scope import should_send_default_pii
13-
from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT
13+
from sentry_sdk.tracing import TransactionSource
1414
from sentry_sdk.utils import (
1515
AnnotatedValue,
1616
capture_internal_exceptions,
@@ -153,7 +153,7 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
153153
headers,
154154
op=OP.FUNCTION_AWS,
155155
name=aws_context.function_name,
156-
source=TRANSACTION_SOURCE_COMPONENT,
156+
source=TransactionSource.COMPONENT,
157157
origin=AwsLambdaIntegration.origin,
158158
)
159159
with sentry_sdk.start_transaction(

sentry_sdk/integrations/celery/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
)
1515
from sentry_sdk.integrations.celery.utils import _now_seconds_since_epoch
1616
from sentry_sdk.integrations.logging import ignore_logger
17-
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, TRANSACTION_SOURCE_TASK
17+
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, TransactionSource
1818
from sentry_sdk.tracing_utils import Baggage
1919
from sentry_sdk.utils import (
2020
capture_internal_exceptions,
@@ -319,7 +319,7 @@ def _inner(*args, **kwargs):
319319
headers,
320320
op=OP.QUEUE_TASK_CELERY,
321321
name="unknown celery task",
322-
source=TRANSACTION_SOURCE_TASK,
322+
source=TransactionSource.TASK,
323323
origin=CeleryIntegration.origin,
324324
)
325325
transaction.name = task.name

sentry_sdk/integrations/chalice.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sentry_sdk
55
from sentry_sdk.integrations import Integration, DidNotEnable
66
from sentry_sdk.integrations.aws_lambda import _make_request_event_processor
7-
from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT
7+
from sentry_sdk.tracing import TransactionSource
88
from sentry_sdk.utils import (
99
capture_internal_exceptions,
1010
event_from_exception,
@@ -67,7 +67,7 @@ def wrapped_view_function(**function_args):
6767
configured_time = app.lambda_context.get_remaining_time_in_millis()
6868
scope.set_transaction_name(
6969
app.lambda_context.function_name,
70-
source=TRANSACTION_SOURCE_COMPONENT,
70+
source=TransactionSource.COMPONENT,
7171
)
7272

7373
scope.add_event_processor(

sentry_sdk/integrations/django/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from sentry_sdk.consts import OP, SPANDATA
99
from sentry_sdk.scope import add_global_event_processor, should_send_default_pii
1010
from sentry_sdk.serializer import add_global_repr_processor
11-
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TRANSACTION_SOURCE_URL
11+
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource
1212
from sentry_sdk.tracing_utils import add_query_source, record_sql_queries
1313
from sentry_sdk.utils import (
1414
AnnotatedValue,
@@ -398,7 +398,7 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
398398

399399
if transaction_name is None:
400400
transaction_name = request.path_info
401-
source = TRANSACTION_SOURCE_URL
401+
source = TransactionSource.URL
402402
else:
403403
source = SOURCE_FOR_STYLE[transaction_style]
404404

sentry_sdk/integrations/fastapi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sentry_sdk
66
from sentry_sdk.integrations import DidNotEnable
77
from sentry_sdk.scope import should_send_default_pii
8-
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE
8+
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource
99
from sentry_sdk.utils import (
1010
transaction_from_function,
1111
logger,
@@ -61,7 +61,7 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
6161

6262
if not name:
6363
name = _DEFAULT_TRANSACTION_NAME
64-
source = TRANSACTION_SOURCE_ROUTE
64+
source = TransactionSource.ROUTE
6565
else:
6666
source = SOURCE_FOR_STYLE[transaction_style]
6767

sentry_sdk/integrations/gcp.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sentry_sdk.integrations import Integration
1111
from sentry_sdk.integrations._wsgi_common import _filter_headers
1212
from sentry_sdk.scope import should_send_default_pii
13-
from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT
13+
from sentry_sdk.tracing import TransactionSource
1414
from sentry_sdk.utils import (
1515
AnnotatedValue,
1616
capture_internal_exceptions,
@@ -88,7 +88,7 @@ def sentry_func(functionhandler, gcp_event, *args, **kwargs):
8888
headers,
8989
op=OP.FUNCTION_GCP,
9090
name=environ.get("FUNCTION_NAME", ""),
91-
source=TRANSACTION_SOURCE_COMPONENT,
91+
source=TransactionSource.COMPONENT,
9292
origin=GcpIntegration.origin,
9393
)
9494
sampling_context = {

sentry_sdk/integrations/grpc/aio/server.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from sentry_sdk.consts import OP
33
from sentry_sdk.integrations import DidNotEnable
44
from sentry_sdk.integrations.grpc.consts import SPAN_ORIGIN
5-
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_CUSTOM
5+
from sentry_sdk.tracing import Transaction, TransactionSource
66
from sentry_sdk.utils import event_from_exception
77

88
from typing import TYPE_CHECKING
@@ -48,7 +48,7 @@ async def wrapped(request, context):
4848
dict(context.invocation_metadata()),
4949
op=OP.GRPC_SERVER,
5050
name=name,
51-
source=TRANSACTION_SOURCE_CUSTOM,
51+
source=TransactionSource.CUSTOM,
5252
origin=SPAN_ORIGIN,
5353
)
5454

sentry_sdk/integrations/grpc/server.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from sentry_sdk.consts import OP
33
from sentry_sdk.integrations import DidNotEnable
44
from sentry_sdk.integrations.grpc.consts import SPAN_ORIGIN
5-
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_CUSTOM
5+
from sentry_sdk.tracing import Transaction, TransactionSource
66

77
from typing import TYPE_CHECKING
88

@@ -42,7 +42,7 @@ def behavior(request, context):
4242
metadata,
4343
op=OP.GRPC_SERVER,
4444
name=name,
45-
source=TRANSACTION_SOURCE_CUSTOM,
45+
source=TransactionSource.CUSTOM,
4646
origin=SPAN_ORIGIN,
4747
)
4848

sentry_sdk/integrations/huey.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sentry_sdk.tracing import (
1010
BAGGAGE_HEADER_NAME,
1111
SENTRY_TRACE_HEADER_NAME,
12-
TRANSACTION_SOURCE_TASK,
12+
TransactionSource,
1313
)
1414
from sentry_sdk.utils import (
1515
capture_internal_exceptions,
@@ -159,7 +159,7 @@ def _sentry_execute(self, task, timestamp=None):
159159
sentry_headers or {},
160160
name=task.name,
161161
op=OP.QUEUE_TASK_HUEY,
162-
source=TRANSACTION_SOURCE_TASK,
162+
source=TransactionSource.TASK,
163163
origin=HueyIntegration.origin,
164164
)
165165
transaction.set_status(SPANSTATUS.OK)

sentry_sdk/integrations/litestar.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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 SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE
12+
from sentry_sdk.tracing import TransactionSource, SOURCE_FOR_STYLE
1313
from sentry_sdk.utils import (
1414
ensure_integration_enabled,
1515
event_from_exception,
@@ -249,7 +249,7 @@ def event_processor(event, _):
249249

250250
if not tx_name:
251251
tx_name = _DEFAULT_TRANSACTION_NAME
252-
tx_info = {"source": TRANSACTION_SOURCE_ROUTE}
252+
tx_info = {"source": TransactionSource.ROUTE}
253253

254254
event.update(
255255
{

sentry_sdk/integrations/ray.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sentry_sdk
55
from sentry_sdk.consts import OP, SPANSTATUS
66
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
7-
from sentry_sdk.tracing import TRANSACTION_SOURCE_TASK
7+
from sentry_sdk.tracing import TransactionSource
88
from sentry_sdk.utils import (
99
event_from_exception,
1010
logger,
@@ -63,7 +63,7 @@ def _f(*f_args, _tracing=None, **f_kwargs):
6363
op=OP.QUEUE_TASK_RAY,
6464
name=qualname_from_function(f),
6565
origin=RayIntegration.origin,
66-
source=TRANSACTION_SOURCE_TASK,
66+
source=TransactionSource.TASK,
6767
)
6868

6969
with sentry_sdk.start_transaction(transaction) as transaction:

sentry_sdk/integrations/rq.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from sentry_sdk.api import continue_trace
66
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
77
from sentry_sdk.integrations.logging import ignore_logger
8-
from sentry_sdk.tracing import TRANSACTION_SOURCE_TASK
8+
from sentry_sdk.tracing import TransactionSource
99
from sentry_sdk.utils import (
1010
capture_internal_exceptions,
1111
ensure_integration_enabled,
@@ -57,7 +57,7 @@ def sentry_patched_perform_job(self, job, *args, **kwargs):
5757
job.meta.get("_sentry_trace_headers") or {},
5858
op=OP.QUEUE_TASK_RQ,
5959
name="unknown RQ task",
60-
source=TRANSACTION_SOURCE_TASK,
60+
source=TransactionSource.TASK,
6161
origin=RqIntegration.origin,
6262
)
6363

sentry_sdk/integrations/sanic.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
1010
from sentry_sdk.integrations._wsgi_common import RequestExtractor, _filter_headers
1111
from sentry_sdk.integrations.logging import ignore_logger
12-
from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT, TRANSACTION_SOURCE_URL
12+
from sentry_sdk.tracing import TransactionSource
1313
from sentry_sdk.utils import (
1414
capture_internal_exceptions,
1515
ensure_integration_enabled,
@@ -192,7 +192,7 @@ async def _context_enter(request):
192192
op=OP.HTTP_SERVER,
193193
# Unless the request results in a 404 error, the name and source will get overwritten in _set_transaction
194194
name=request.path,
195-
source=TRANSACTION_SOURCE_URL,
195+
source=TransactionSource.URL,
196196
origin=SanicIntegration.origin,
197197
)
198198
request.ctx._sentry_transaction = sentry_sdk.start_transaction(
@@ -229,7 +229,7 @@ async def _set_transaction(request, route, **_):
229229
with capture_internal_exceptions():
230230
scope = sentry_sdk.get_current_scope()
231231
route_name = route.name.replace(request.app.name, "").strip(".")
232-
scope.set_transaction_name(route_name, source=TRANSACTION_SOURCE_COMPONENT)
232+
scope.set_transaction_name(route_name, source=TransactionSource.COMPONENT)
233233

234234

235235
def _sentry_error_handler_lookup(self, exception, *args, **kwargs):
@@ -304,11 +304,11 @@ def _legacy_router_get(self, *args):
304304
sanic_route = sanic_route[len(sanic_app_name) + 1 :]
305305

306306
scope.set_transaction_name(
307-
sanic_route, source=TRANSACTION_SOURCE_COMPONENT
307+
sanic_route, source=TransactionSource.COMPONENT
308308
)
309309
else:
310310
scope.set_transaction_name(
311-
rv[0].__name__, source=TRANSACTION_SOURCE_COMPONENT
311+
rv[0].__name__, source=TransactionSource.COMPONENT
312312
)
313313

314314
return rv

0 commit comments

Comments
 (0)