Skip to content

Commit 9cfe349

Browse files
committed
Added transaction info to scope (and events)
1 parent e51cbe9 commit 9cfe349

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

sentry_sdk/integrations/flask.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from sentry_sdk.integrations import DidNotEnable, Integration
66
from sentry_sdk.integrations._wsgi_common import RequestExtractor
77
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
8+
from sentry_sdk.scope import Scope
89
from sentry_sdk.utils import (
910
TRANSACTION_SOURCE_COMPONENT,
1011
TRANSACTION_SOURCE_ROUTE,
@@ -110,6 +111,7 @@ def _add_sentry_trace(sender, template, context, **extra):
110111

111112

112113
def _add_transaction(scope, style, request):
114+
# type: (Scope, str, Request) -> Scope
113115
name_for_style = {
114116
"url": request.url_rule.rule,
115117
"endpoint": request.url_rule.endpoint,
@@ -119,8 +121,9 @@ def _add_transaction(scope, style, request):
119121
"endpoint": TRANSACTION_SOURCE_COMPONENT,
120122
}
121123

122-
scope.transaction = name_for_style[style]
123-
scope.transaction_info = {"source": source_for_style[style]}
124+
scope.set_transaction_name(
125+
name_for_style.get(style), source=source_for_style.get(style)
126+
)
124127

125128
return scope
126129

sentry_sdk/scope.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class Scope(object):
8181
# note that for legacy reasons, _transaction is the transaction *name*,
8282
# not a Transaction object (the object is stored in _span)
8383
"_transaction",
84+
"_transaction_info",
8485
"_user",
8586
"_tags",
8687
"_contexts",
@@ -109,6 +110,7 @@ def clear(self):
109110
self._level = None # type: Optional[str]
110111
self._fingerprint = None # type: Optional[List[str]]
111112
self._transaction = None # type: Optional[str]
113+
self._transaction_info = {} # type: Dict[str, str]
112114
self._user = None # type: Optional[Dict[str, Any]]
113115

114116
self._tags = {} # type: Dict[str, Any]
@@ -176,6 +178,17 @@ def transaction(self, value):
176178
if self._span and self._span.containing_transaction:
177179
self._span.containing_transaction.name = value
178180

181+
def set_transaction_name(self, name, source=None):
182+
# type: (str, Optional[str]) -> None
183+
"""Set the transaction name and optionally the transaction source."""
184+
self._transaction = name
185+
186+
if self._span and self._span.containing_transaction:
187+
self._span.containing_transaction.name = name
188+
189+
if source:
190+
self._transaction_info["source"] = source
191+
179192
@_attr_setter
180193
def user(self, value):
181194
# type: (Optional[Dict[str, Any]]) -> None
@@ -363,6 +376,9 @@ def _drop(event, cause, ty):
363376
if event.get("transaction") is None and self._transaction is not None:
364377
event["transaction"] = self._transaction
365378

379+
if event.get("transaction_info") is None and self._transaction_info is not None:
380+
event["transaction_info"] = self._transaction_info
381+
366382
if event.get("fingerprint") is None and self._fingerprint is not None:
367383
event["fingerprint"] = self._fingerprint
368384

@@ -406,6 +422,8 @@ def update_from_scope(self, scope):
406422
self._fingerprint = scope._fingerprint
407423
if scope._transaction is not None:
408424
self._transaction = scope._transaction
425+
if scope._transaction_info is not None:
426+
self._transaction_info.update(scope._transaction_info)
409427
if scope._user is not None:
410428
self._user = scope._user
411429
if scope._tags:
@@ -452,6 +470,7 @@ def __copy__(self):
452470
rv._name = self._name
453471
rv._fingerprint = self._fingerprint
454472
rv._transaction = self._transaction
473+
rv._transaction_info = dict(self._transaction_info)
455474
rv._user = self._user
456475

457476
rv._tags = dict(self._tags)

0 commit comments

Comments
 (0)