Skip to content

Support Starlette/FastAPI app.host #4157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2025
Merged
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
6 changes: 5 additions & 1 deletion sentry_sdk/integrations/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,11 @@ def _transaction_name_from_router(scope):
for route in router.routes:
match = route.matches(scope)
if match[0] == Match.FULL:
return route.path
try:
return route.path
except AttributeError:
# routes added via app.host() won't have a path attribute
return scope.get("path")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question] could we just use scope.get("path") in all cases, instead? Or, are there times where route.path is defined, but scope.get("path") is not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, so I'm opting for the least invasive approach that will only affect host routes to avoid potentially breaking existing functionality.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough – I suppose we could look into this as a future improvement but tbh it's probs not worth much effort


return None

Expand Down
35 changes: 35 additions & 0 deletions tests/integrations/fastapi/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,38 @@ async def _error():
client.get("/error")

assert len(events) == int(expected_error)


@pytest.mark.parametrize("transaction_style", ["endpoint", "url"])
def test_app_host(sentry_init, capture_events, transaction_style):
sentry_init(
traces_sample_rate=1.0,
integrations=[
StarletteIntegration(transaction_style=transaction_style),
FastApiIntegration(transaction_style=transaction_style),
],
)

app = FastAPI()
subapp = FastAPI()

@subapp.get("/subapp")
async def subapp_route():
return {"message": "Hello world!"}

app.host("subapp", subapp)

events = capture_events()

client = TestClient(app)
client.get("/subapp", headers={"Host": "subapp"})

assert len(events) == 1

(event,) = events
assert "transaction" in event

if transaction_style == "url":
assert event["transaction"] == "/subapp"
else:
assert event["transaction"].endswith("subapp_route")
Loading