Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion sentry_sdk/integrations/flask.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import TYPE_CHECKING

import sentry_sdk
from sentry_sdk.consts import SPANDATA
Comment thread
alexander-alderman-webb marked this conversation as resolved.
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
from sentry_sdk.integrations._wsgi_common import (
DEFAULT_HTTP_METHODS_TO_CAPTURE,
Expand All @@ -18,7 +19,7 @@
)

if TYPE_CHECKING:
from typing import Any, Callable, Dict, Union
from typing import Any, Callable, Dict, Optional, Union

from werkzeug.datastructures import FileStorage, ImmutableMultiDict

Expand Down Expand Up @@ -150,6 +151,16 @@ def _request_started(app: "Flask", **kwargs: "Any") -> None:

request = flask_request._get_current_object()

route_path: "Optional[str]" = None
try:
route_path = request.url_rule.rule
except Exception:
pass

server_span = sentry_sdk.get_current_scope()._server_segment_span
if server_span is not None and route_path is not None:
server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path)
Comment thread
alexander-alderman-webb marked this conversation as resolved.

# Set the transaction name and source here,
# but rely on WSGI middleware to actually start the transaction
_set_transaction_name_and_source(
Expand Down
21 changes: 21 additions & 0 deletions tests/integrations/flask/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@ def test_transaction_or_segment_style(
assert event["transaction_info"] == {"source": expected_source}


def test_http_route(
sentry_init,
app,
capture_items,
):
sentry_init(
integrations=[flask_sentry.FlaskIntegration()],
traces_sample_rate=1.0,
trace_lifecycle="stream",
)
items = capture_items("span")

client = app.test_client()
client.get("/message/123456")

sentry_sdk.flush()

(segment,) = (item.payload for item in items if item.payload.get("is_segment"))
assert segment["attributes"][SPANDATA.HTTP_ROUTE] == "/message/<int:message_id>"


@pytest.mark.parametrize("debug", (True, False))
@pytest.mark.parametrize("testing", (True, False))
def test_errors(
Expand Down
Loading