Skip to content

Commit

Permalink
Add constants for sentry-trace and baggage headers (#1765)
Browse files Browse the repository at this point in the history
* Introduced SENTRY_TRACE_HEADER_NAME variable
* Introduced +BAGGAGE_HEADER_NAME variable
  • Loading branch information
antonpirker committed Nov 30, 2022
1 parent 1c886e6 commit 905b3fd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
6 changes: 4 additions & 2 deletions .vscode/settings.json
@@ -1,4 +1,6 @@
{
"python.pythonPath": ".venv/bin/python",
"python.formatting.provider": "black"
}
"python.formatting.provider": "black",
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
50 changes: 25 additions & 25 deletions sentry_sdk/consts.py
Expand Up @@ -44,6 +44,31 @@
DEFAULT_MAX_BREADCRUMBS = 100


class OP:
DB = "db"
DB_REDIS = "db.redis"
EVENT_DJANGO = "event.django"
FUNCTION = "function"
FUNCTION_AWS = "function.aws"
FUNCTION_GCP = "function.gcp"
HTTP_CLIENT = "http.client"
HTTP_CLIENT_STREAM = "http.client.stream"
HTTP_SERVER = "http.server"
MIDDLEWARE_DJANGO = "middleware.django"
MIDDLEWARE_STARLETTE = "middleware.starlette"
MIDDLEWARE_STARLETTE_RECEIVE = "middleware.starlette.receive"
MIDDLEWARE_STARLETTE_SEND = "middleware.starlette.send"
QUEUE_SUBMIT_CELERY = "queue.submit.celery"
QUEUE_TASK_CELERY = "queue.task.celery"
QUEUE_TASK_RQ = "queue.task.rq"
SUBPROCESS = "subprocess"
SUBPROCESS_WAIT = "subprocess.wait"
SUBPROCESS_COMMUNICATE = "subprocess.communicate"
TEMPLATE_RENDER = "template.render"
VIEW_RENDER = "view.render"
WEBSOCKET_SERVER = "websocket.server"


# This type exists to trick mypy and PyCharm into thinking `init` and `Client`
# take these arguments (even though they take opaque **kwargs)
class ClientConstructor(object):
Expand Down Expand Up @@ -106,28 +131,3 @@ def _get_default_options():


VERSION = "1.11.1"


class OP:
DB = "db"
DB_REDIS = "db.redis"
EVENT_DJANGO = "event.django"
FUNCTION = "function"
FUNCTION_AWS = "function.aws"
FUNCTION_GCP = "function.gcp"
HTTP_CLIENT = "http.client"
HTTP_CLIENT_STREAM = "http.client.stream"
HTTP_SERVER = "http.server"
MIDDLEWARE_DJANGO = "middleware.django"
MIDDLEWARE_STARLETTE = "middleware.starlette"
MIDDLEWARE_STARLETTE_RECEIVE = "middleware.starlette.receive"
MIDDLEWARE_STARLETTE_SEND = "middleware.starlette.send"
QUEUE_SUBMIT_CELERY = "queue.submit.celery"
QUEUE_TASK_CELERY = "queue.task.celery"
QUEUE_TASK_RQ = "queue.task.rq"
SUBPROCESS = "subprocess"
SUBPROCESS_WAIT = "subprocess.wait"
SUBPROCESS_COMMUNICATE = "subprocess.communicate"
TEMPLATE_RENDER = "template.render"
VIEW_RENDER = "view.render"
WEBSOCKET_SERVER = "websocket.server"
9 changes: 6 additions & 3 deletions sentry_sdk/integrations/flask.py
Expand Up @@ -6,7 +6,7 @@
from sentry_sdk.integrations._wsgi_common import RequestExtractor
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
from sentry_sdk.scope import Scope
from sentry_sdk.tracing import SOURCE_FOR_STYLE
from sentry_sdk.tracing import SENTRY_TRACE_HEADER_NAME, SOURCE_FOR_STYLE
from sentry_sdk.utils import (
capture_internal_exceptions,
event_from_exception,
Expand Down Expand Up @@ -101,8 +101,11 @@ def _add_sentry_trace(sender, template, context, **extra):
sentry_span = Hub.current.scope.span
context["sentry_trace"] = (
Markup(
'<meta name="sentry-trace" content="%s" />'
% (sentry_span.to_traceparent(),)
'<meta name="%s" content="%s" />'
% (
SENTRY_TRACE_HEADER_NAME,
sentry_span.to_traceparent(),
)
)
if sentry_span
else ""
Expand Down
1 change: 0 additions & 1 deletion sentry_sdk/integrations/stdlib.py
Expand Up @@ -187,7 +187,6 @@ def sentry_patched_popen_init(self, *a, **kw):
env = None

with hub.start_span(op=OP.SUBPROCESS, description=description) as span:

for k, v in hub.iter_trace_propagation_headers(span):
if env is None:
env = _init_argument(
Expand Down
21 changes: 14 additions & 7 deletions sentry_sdk/tracing.py
Expand Up @@ -6,7 +6,6 @@
from datetime import datetime, timedelta

import sentry_sdk

from sentry_sdk.utils import logger
from sentry_sdk._types import MYPY

Expand All @@ -24,6 +23,9 @@
import sentry_sdk.profiler
from sentry_sdk._types import Event, SamplingContext, MeasurementUnit

BAGGAGE_HEADER_NAME = "baggage"
SENTRY_TRACE_HEADER_NAME = "sentry-trace"


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

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

sentrytrace_kwargs = extract_sentrytrace_data(headers.get("sentry-trace"))
sentrytrace_kwargs = extract_sentrytrace_data(
headers.get(SENTRY_TRACE_HEADER_NAME)
)

if sentrytrace_kwargs is not None:
kwargs.update(sentrytrace_kwargs)
Expand All @@ -308,7 +312,7 @@ def iter_headers(self):
`sentry_tracestate` value, this will cause one to be generated and
stored.
"""
yield "sentry-trace", self.to_traceparent()
yield SENTRY_TRACE_HEADER_NAME, self.to_traceparent()

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

@classmethod
def from_traceparent(
Expand All @@ -344,7 +348,9 @@ def from_traceparent(
if not traceparent:
return None

return cls.continue_from_headers({"sentry-trace": traceparent}, **kwargs)
return cls.continue_from_headers(
{SENTRY_TRACE_HEADER_NAME: traceparent}, **kwargs
)

def to_traceparent(self):
# type: () -> str
Expand Down Expand Up @@ -653,6 +659,7 @@ def finish(self, hub=None):
# to a concrete decision.
if self.sampled is None:
logger.warning("Discarding transaction without sampling decision.")

return None

finished_spans = [
Expand Down

0 comments on commit 905b3fd

Please sign in to comment.