Skip to content

Commit e51cbe9

Browse files
committed
Added transaction source to flask
1 parent 9e69fcf commit e51cbe9

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
# See https://pre-commit.com/hooks.html for more hooks
33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: v3.2.0
5+
rev: v4.3.0
66
hooks:
77
- id: trailing-whitespace
88
- id: end-of-file-fixer
99

1010
- repo: https://github.com/psf/black
11-
rev: stable
11+
rev: 22.6.0
1212
hooks:
1313
- id: black
1414

1515
- repo: https://gitlab.com/pycqa/flake8
16-
rev: 4.0.1
16+
rev: 3.9.2
1717
hooks:
1818
- id: flake8
1919

sentry_sdk/integrations/flask.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
from __future__ import absolute_import
22

3+
from sentry_sdk._types import MYPY
34
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
76
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+
)
1014

1115
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
1917

2018
from sentry_sdk._types import EventProcessor
19+
from sentry_sdk.integrations.wsgi import _ScopedResponse
20+
from werkzeug.datastructures import FileStorage, ImmutableMultiDict
2121

2222

2323
try:
@@ -26,14 +26,9 @@
2626
flask_login = None
2727

2828
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
3732
from flask.signals import (
3833
before_render_template,
3934
got_request_exception,
@@ -114,6 +109,22 @@ def _add_sentry_trace(sender, template, context, **extra):
114109
)
115110

116111

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+
117128
def _request_started(sender, **kwargs):
118129
# type: (Flask, **Any) -> None
119130
hub = Hub.current
@@ -125,13 +136,10 @@ def _request_started(sender, **kwargs):
125136
with hub.configure_scope() as scope:
126137
request = _request_ctx_stack.top.request
127138

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
130141
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)
135143
except Exception:
136144
pass
137145

sentry_sdk/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242
MAX_STRING_LENGTH = 512
4343
BASE64_ALPHABET = re.compile(r"^[a-zA-Z0-9/+=]*$")
4444

45+
# Transaction source
46+
# see https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations
47+
TRANSACTION_SOURCE_CUSTOM = "custom"
48+
TRANSACTION_SOURCE_URL = "url"
49+
TRANSACTION_SOURCE_ROUTE = "route"
50+
TRANSACTION_SOURCE_VIEW = "view"
51+
TRANSACTION_SOURCE_COMPONENT = "component"
52+
TRANSACTION_SOURCE_TASK = "task"
53+
TRANSACTION_SOURCE_UNKNOWN = "unknown"
54+
4555

4656
def json_dumps(data):
4757
# type: (Any) -> bytes

0 commit comments

Comments
 (0)