Skip to content

Commit 905b3fd

Browse files
authored
Add constants for sentry-trace and baggage headers (#1765)
* Introduced SENTRY_TRACE_HEADER_NAME variable * Introduced +BAGGAGE_HEADER_NAME variable
1 parent 1c886e6 commit 905b3fd

File tree

5 files changed

+49
-38
lines changed

5 files changed

+49
-38
lines changed

.vscode/settings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
22
"python.pythonPath": ".venv/bin/python",
3-
"python.formatting.provider": "black"
4-
}
3+
"python.formatting.provider": "black",
4+
"python.testing.unittestEnabled": false,
5+
"python.testing.pytestEnabled": true
6+
}

sentry_sdk/consts.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,31 @@
4444
DEFAULT_MAX_BREADCRUMBS = 100
4545

4646

47+
class OP:
48+
DB = "db"
49+
DB_REDIS = "db.redis"
50+
EVENT_DJANGO = "event.django"
51+
FUNCTION = "function"
52+
FUNCTION_AWS = "function.aws"
53+
FUNCTION_GCP = "function.gcp"
54+
HTTP_CLIENT = "http.client"
55+
HTTP_CLIENT_STREAM = "http.client.stream"
56+
HTTP_SERVER = "http.server"
57+
MIDDLEWARE_DJANGO = "middleware.django"
58+
MIDDLEWARE_STARLETTE = "middleware.starlette"
59+
MIDDLEWARE_STARLETTE_RECEIVE = "middleware.starlette.receive"
60+
MIDDLEWARE_STARLETTE_SEND = "middleware.starlette.send"
61+
QUEUE_SUBMIT_CELERY = "queue.submit.celery"
62+
QUEUE_TASK_CELERY = "queue.task.celery"
63+
QUEUE_TASK_RQ = "queue.task.rq"
64+
SUBPROCESS = "subprocess"
65+
SUBPROCESS_WAIT = "subprocess.wait"
66+
SUBPROCESS_COMMUNICATE = "subprocess.communicate"
67+
TEMPLATE_RENDER = "template.render"
68+
VIEW_RENDER = "view.render"
69+
WEBSOCKET_SERVER = "websocket.server"
70+
71+
4772
# This type exists to trick mypy and PyCharm into thinking `init` and `Client`
4873
# take these arguments (even though they take opaque **kwargs)
4974
class ClientConstructor(object):
@@ -106,28 +131,3 @@ def _get_default_options():
106131

107132

108133
VERSION = "1.11.1"
109-
110-
111-
class OP:
112-
DB = "db"
113-
DB_REDIS = "db.redis"
114-
EVENT_DJANGO = "event.django"
115-
FUNCTION = "function"
116-
FUNCTION_AWS = "function.aws"
117-
FUNCTION_GCP = "function.gcp"
118-
HTTP_CLIENT = "http.client"
119-
HTTP_CLIENT_STREAM = "http.client.stream"
120-
HTTP_SERVER = "http.server"
121-
MIDDLEWARE_DJANGO = "middleware.django"
122-
MIDDLEWARE_STARLETTE = "middleware.starlette"
123-
MIDDLEWARE_STARLETTE_RECEIVE = "middleware.starlette.receive"
124-
MIDDLEWARE_STARLETTE_SEND = "middleware.starlette.send"
125-
QUEUE_SUBMIT_CELERY = "queue.submit.celery"
126-
QUEUE_TASK_CELERY = "queue.task.celery"
127-
QUEUE_TASK_RQ = "queue.task.rq"
128-
SUBPROCESS = "subprocess"
129-
SUBPROCESS_WAIT = "subprocess.wait"
130-
SUBPROCESS_COMMUNICATE = "subprocess.communicate"
131-
TEMPLATE_RENDER = "template.render"
132-
VIEW_RENDER = "view.render"
133-
WEBSOCKET_SERVER = "websocket.server"

sentry_sdk/integrations/flask.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sentry_sdk.integrations._wsgi_common import RequestExtractor
77
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
88
from sentry_sdk.scope import Scope
9-
from sentry_sdk.tracing import SOURCE_FOR_STYLE
9+
from sentry_sdk.tracing import SENTRY_TRACE_HEADER_NAME, SOURCE_FOR_STYLE
1010
from sentry_sdk.utils import (
1111
capture_internal_exceptions,
1212
event_from_exception,
@@ -101,8 +101,11 @@ def _add_sentry_trace(sender, template, context, **extra):
101101
sentry_span = Hub.current.scope.span
102102
context["sentry_trace"] = (
103103
Markup(
104-
'<meta name="sentry-trace" content="%s" />'
105-
% (sentry_span.to_traceparent(),)
104+
'<meta name="%s" content="%s" />'
105+
% (
106+
SENTRY_TRACE_HEADER_NAME,
107+
sentry_span.to_traceparent(),
108+
)
106109
)
107110
if sentry_span
108111
else ""

sentry_sdk/integrations/stdlib.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ def sentry_patched_popen_init(self, *a, **kw):
187187
env = None
188188

189189
with hub.start_span(op=OP.SUBPROCESS, description=description) as span:
190-
191190
for k, v in hub.iter_trace_propagation_headers(span):
192191
if env is None:
193192
env = _init_argument(

sentry_sdk/tracing.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from datetime import datetime, timedelta
77

88
import sentry_sdk
9-
109
from sentry_sdk.utils import logger
1110
from sentry_sdk._types import MYPY
1211

@@ -24,6 +23,9 @@
2423
import sentry_sdk.profiler
2524
from sentry_sdk._types import Event, SamplingContext, MeasurementUnit
2625

26+
BAGGAGE_HEADER_NAME = "baggage"
27+
SENTRY_TRACE_HEADER_NAME = "sentry-trace"
28+
2729

2830
# Transaction source
2931
# see https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations
@@ -278,10 +280,12 @@ def continue_from_headers(
278280

279281
# TODO-neel move away from this kwargs stuff, it's confusing and opaque
280282
# make more explicit
281-
baggage = Baggage.from_incoming_header(headers.get("baggage"))
282-
kwargs.update({"baggage": baggage})
283+
baggage = Baggage.from_incoming_header(headers.get(BAGGAGE_HEADER_NAME))
284+
kwargs.update({BAGGAGE_HEADER_NAME: baggage})
283285

284-
sentrytrace_kwargs = extract_sentrytrace_data(headers.get("sentry-trace"))
286+
sentrytrace_kwargs = extract_sentrytrace_data(
287+
headers.get(SENTRY_TRACE_HEADER_NAME)
288+
)
285289

286290
if sentrytrace_kwargs is not None:
287291
kwargs.update(sentrytrace_kwargs)
@@ -308,7 +312,7 @@ def iter_headers(self):
308312
`sentry_tracestate` value, this will cause one to be generated and
309313
stored.
310314
"""
311-
yield "sentry-trace", self.to_traceparent()
315+
yield SENTRY_TRACE_HEADER_NAME, self.to_traceparent()
312316

313317
tracestate = self.to_tracestate() if has_tracestate_enabled(self) else None
314318
# `tracestate` will only be `None` if there's no client or no DSN
@@ -320,7 +324,7 @@ def iter_headers(self):
320324
if self.containing_transaction:
321325
baggage = self.containing_transaction.get_baggage().serialize()
322326
if baggage:
323-
yield "baggage", baggage
327+
yield BAGGAGE_HEADER_NAME, baggage
324328

325329
@classmethod
326330
def from_traceparent(
@@ -344,7 +348,9 @@ def from_traceparent(
344348
if not traceparent:
345349
return None
346350

347-
return cls.continue_from_headers({"sentry-trace": traceparent}, **kwargs)
351+
return cls.continue_from_headers(
352+
{SENTRY_TRACE_HEADER_NAME: traceparent}, **kwargs
353+
)
348354

349355
def to_traceparent(self):
350356
# type: () -> str
@@ -653,6 +659,7 @@ def finish(self, hub=None):
653659
# to a concrete decision.
654660
if self.sampled is None:
655661
logger.warning("Discarding transaction without sampling decision.")
662+
656663
return None
657664

658665
finished_spans = [

0 commit comments

Comments
 (0)