Skip to content

Commit 2dda456

Browse files
feat(python): Document default span attrs in ASGI, update for 3.0 (#14368)
Document what span attributes will be attached to transactions by default by our builtin integrations. This PR updates ASGI. Rest will follow. I created a new 3.x page for this since it's new behavior in 3.0. --------- Co-authored-by: Daniel Szoke <[email protected]>
1 parent 7a7fede commit 2dda456

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
title: ASGI
3+
description: "Learn about the ASGI integration and how it adds support for ASGI applications."
4+
---
5+
6+
The ASGI middleware can be used to instrument any [ASGI](https://asgi.readthedocs.io/en/latest/)-compatible web framework to attach request data for your events.
7+
8+
<Alert>
9+
10+
Please check our list of [supported integrations](/platforms/python/integrations/) as there might already be a specific integration (like [FastAPI](/platforms/python/integrations/fastapi/) or [Sanic](/platforms/python/integrations/sanic/)) that is easier to use and captures more useful information than our generic ASGI middleware. If that's the case, you should use the specific integration instead of this middleware.
11+
12+
</Alert>
13+
14+
## Install
15+
16+
```bash {tabTitle:pip}
17+
pip install "sentry-sdk"
18+
```
19+
```bash {tabTitle:uv}
20+
uv add "sentry-sdk"
21+
```
22+
23+
## Configure
24+
25+
In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/). You can also collect and analyze performance profiles from real users with [profiling](/product/explore/profiling/).
26+
27+
Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
28+
29+
<OnboardingOptionButtons
30+
options={[
31+
'error-monitoring',
32+
'performance',
33+
'profiling',
34+
]}
35+
/>
36+
37+
```python
38+
import sentry_sdk
39+
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
40+
41+
from my_asgi_app import app
42+
43+
sentry_sdk.init(
44+
dsn="___PUBLIC_DSN___",
45+
# Add data like request headers and IP for users, if applicable;
46+
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
47+
send_default_pii=True,
48+
# ___PRODUCT_OPTION_START___ performance
49+
# Set traces_sample_rate to 1.0 to capture 100%
50+
# of transactions for tracing.
51+
traces_sample_rate=1.0,
52+
# ___PRODUCT_OPTION_END___ performance
53+
# ___PRODUCT_OPTION_START___ profiling
54+
# To collect profiles for all profile sessions,
55+
# set `profile_session_sample_rate` to 1.0.
56+
profile_session_sample_rate=1.0,
57+
# Profiles will be automatically collected while
58+
# there is an active span.
59+
profile_lifecycle="trace",
60+
# ___PRODUCT_OPTION_END___ profiling
61+
)
62+
63+
app = SentryAsgiMiddleware(app)
64+
```
65+
66+
The middleware supports both ASGI 2 and ASGI 3.
67+
68+
## Verify
69+
70+
Trigger an error in your code and see it show up in [sentry.io](https://sentry.io).
71+
72+
```python
73+
sentry_sdk.init(...) # same as above
74+
75+
def app(scope):
76+
async def get_body():
77+
return f"The number is: {1/0}" # raises an error!
78+
79+
async def asgi(receive, send):
80+
await send(
81+
{
82+
"type": "http.response.start",
83+
"status": 200,
84+
"headers": [[b"content-type", b"text/plain"]],
85+
}
86+
)
87+
await send({"type": "http.response.body", "body": await get_body()})
88+
89+
return asgi
90+
91+
app = SentryAsgiMiddleware(app)
92+
```
93+
94+
Run your ASGI app with unicorn:
95+
96+
```bash
97+
uvicorn main:app --port 8000
98+
```
99+
100+
Point your browser to [http://localhost:8000](http://localhost:8000) to trigger the error which is then sent to Sentry.
101+
102+
Additionally, a transaction will show up in the "Performance" section on [sentry.io](https://sentry.io).
103+
104+
## Behavior
105+
106+
- Request data is attached to all events: **HTTP method, URL, headers**. Sentry excludes raw bodies and multipart file uploads. Sentry also excludes personally identifiable information (such as user ids, usernames, cookies, authorization headers, IP addresses) unless you set `send_default_pii` to `True`.
107+
108+
- Each request has a separate scope. Changes to the scope within a view, for example setting a tag, will only apply to events sent as part of the request being handled.
109+
110+
- The ASGI middleware does not behave like a regular integration. It is not initialized through an extra parameter to `init` and is not attached to a client. When capturing or supplementing events, it just uses the currently active scopes.
111+
112+
### Default Span Attributes
113+
114+
A set of predefined span attributes will be attached to ASGI transactions by default. These can also be used for sampling since they will also be accessible via the `sampling_context` dictionary in the [`traces_sampler`](/platforms/python/configuration/options/#traces_sampler).
115+
116+
| Span Attribute | Description |
117+
| ------------------------------- | -------------------------------------------------------- |
118+
| `network.protocol.name` | `type` on ASGI scope |
119+
| `url.scheme` | `scheme` on ASGI scope |
120+
| `url.path` | `path` on ASGI scope |
121+
| `url.query` | `query` on ASGI scope |
122+
| `network.protocol.version` | `http_version` on ASGI scope |
123+
| `http.request.method` | `method` on ASGI scope |
124+
| `server.address`, `server.port` | `server` on ASGI scope |
125+
| `client.address`, `client.port` | `client` on ASGI scope |
126+
| `url.full` | full URL, reconstructed from individual ASGI scope parts |
127+
| `http.request.header.{header}` | `headers` on ASGI scope |
128+
129+
These attributes will also be sent to Sentry. If you don't want that, you can filter them out using a custom [`before_send`](/platforms/python/configuration/options/#before_send) function.
130+
131+
## Supported Versions
132+
133+
- Python: 3.7+

0 commit comments

Comments
 (0)