1
1
from __future__ import absolute_import
2
2
3
+ from sentry_sdk ._types import MYPY
3
4
from sentry_sdk .hub import Hub , _should_send_default_pii
4
- from sentry_sdk .utils import capture_internal_exceptions , event_from_exception
5
- from sentry_sdk .integrations import Integration , DidNotEnable
6
- from sentry_sdk .integrations .wsgi import SentryWsgiMiddleware
5
+ from sentry_sdk .integrations import DidNotEnable , Integration
7
6
from sentry_sdk .integrations ._wsgi_common import RequestExtractor
8
-
9
- from sentry_sdk ._types import MYPY
7
+ from sentry_sdk .integrations .wsgi import SentryWsgiMiddleware
8
+ from sentry_sdk .utils import (
9
+ TRANSACTION_SOURCE_COMPONENT ,
10
+ TRANSACTION_SOURCE_ROUTE ,
11
+ capture_internal_exceptions ,
12
+ event_from_exception ,
13
+ )
10
14
11
15
if MYPY :
12
- from sentry_sdk .integrations .wsgi import _ScopedResponse
13
- from typing import Any
14
- from typing import Dict
15
- from werkzeug .datastructures import ImmutableMultiDict
16
- from werkzeug .datastructures import FileStorage
17
- from typing import Union
18
- from typing import Callable
16
+ from typing import Any , Callable , Dict , Union
19
17
20
18
from sentry_sdk ._types import EventProcessor
19
+ from sentry_sdk .integrations .wsgi import _ScopedResponse
20
+ from werkzeug .datastructures import FileStorage , ImmutableMultiDict
21
21
22
22
23
23
try :
26
26
flask_login = None
27
27
28
28
try :
29
- from flask import ( # type: ignore
30
- Markup ,
31
- Request ,
32
- Flask ,
33
- _request_ctx_stack ,
34
- _app_ctx_stack ,
35
- __version__ as FLASK_VERSION ,
36
- )
29
+ from flask import Flask , Markup , Request
30
+ from flask import __version__ as FLASK_VERSION # type: ignore
31
+ from flask import _app_ctx_stack , _request_ctx_stack
37
32
from flask .signals import (
38
33
before_render_template ,
39
34
got_request_exception ,
@@ -114,6 +109,22 @@ def _add_sentry_trace(sender, template, context, **extra):
114
109
)
115
110
116
111
112
+ def _add_transaction (scope , style , request ):
113
+ name_for_style = {
114
+ "url" : request .url_rule .rule ,
115
+ "endpoint" : request .url_rule .endpoint ,
116
+ }
117
+ source_for_style = {
118
+ "url" : TRANSACTION_SOURCE_ROUTE ,
119
+ "endpoint" : TRANSACTION_SOURCE_COMPONENT ,
120
+ }
121
+
122
+ scope .transaction = name_for_style [style ]
123
+ scope .transaction_info = {"source" : source_for_style [style ]}
124
+
125
+ return scope
126
+
127
+
117
128
def _request_started (sender , ** kwargs ):
118
129
# type: (Flask, **Any) -> None
119
130
hub = Hub .current
@@ -125,13 +136,10 @@ def _request_started(sender, **kwargs):
125
136
with hub .configure_scope () as scope :
126
137
request = _request_ctx_stack .top .request
127
138
128
- # Set the transaction name here, but rely on WSGI middleware to actually
129
- # start the transaction
139
+ # Set the transaction name and source here,
140
+ # but rely on WSGI middleware to actually start the transaction
130
141
try :
131
- if integration .transaction_style == "endpoint" :
132
- scope .transaction = request .url_rule .endpoint
133
- elif integration .transaction_style == "url" :
134
- scope .transaction = request .url_rule .rule
142
+ scope = _add_transaction (scope , integration .transaction_style , request )
135
143
except Exception :
136
144
pass
137
145
0 commit comments