Skip to content

Commit 36d918c

Browse files
Docs for Starlette and FastAPI integration. (#5329)
* Added guide for Starlette * Added guide for FastAPI * Added wizard for Starlette * Added wizard for FastAPI Co-authored-by: Isabel <[email protected]>
1 parent fd42b83 commit 36d918c

File tree

6 files changed

+337
-5
lines changed

6 files changed

+337
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"gray-matter": "^4.0.2",
6161
"jsdom": "^16.3.0",
6262
"node-sass": "^4.14.1",
63-
"platformicons": "4.4.0",
63+
"platformicons": "5.2.1",
6464
"prism-sentry": "^1.0.2",
6565
"prismjs": "^1.27.0",
6666
"prop-types": "^15.7.2",
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: FastAPI
3+
redirect_from:
4+
- /clients/python/integrations/fastapi/
5+
- /platforms/python/fastapi/
6+
description: "Learn about using Sentry with FastAPI."
7+
---
8+
9+
The FastAPI integration adds support for the [FastAPI Framework](https://fastapi.tiangolo.com/).
10+
11+
## Install
12+
13+
Install `sentry-sdk` from PyPI with the `fastapi` extra:
14+
15+
```bash
16+
pip install --upgrade 'sentry-sdk[fastapi]'
17+
```
18+
19+
## Configure
20+
21+
To configure the SDK, initialize it with the integration before your app has been initialized:
22+
23+
```python
24+
from fastapi import FastAPI
25+
26+
import sentry_sdk
27+
from sentry_sdk.integrations.starlette import StarletteIntegration
28+
from sentry_sdk.integrations.fastapi import FastApiIntegration
29+
30+
31+
sentry_sdk.init(
32+
dsn="___PUBLIC_DSN___",
33+
integrations=[
34+
StarletteIntegration(),
35+
FastApiIntegration(),
36+
],
37+
38+
# Set traces_sample_rate to 1.0 to capture 100%
39+
# of transactions for performance monitoring.
40+
# We recommend adjusting this value in production,
41+
traces_sample_rate=1.0,
42+
)
43+
44+
app = FastAPI()
45+
```
46+
47+
Because FastAPI is based on the Starlette framework, you need to enable both `StarletteIntegration` and `FastApiIntegration`.
48+
49+
<Note>
50+
51+
In the future, we will make this auto-enabling, so you won't need to add the `StarletteIntegration` to your `sentry_sdk.init()` call. Instead, the Sentry SDK will detect the presence of Starlette and will set up the integration automatically.
52+
53+
</Note>
54+
55+
## Verify
56+
57+
This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:
58+
59+
```python
60+
from fastapi import FastAPI
61+
62+
63+
app = FastAPI()
64+
65+
@app.get("/sentry-debug")
66+
async def trigger_error():
67+
division_by_zero = 1 / 0
68+
69+
```
70+
71+
Visiting `"/sentry-debug"` will trigger an error that will be captured by Sentry.
72+
73+
## Behavior
74+
75+
- All exceptions leading to an Internal Server Error are reported.
76+
77+
- Request data is attached to all events: **HTTP method, URL, headers, form data, JSON payloads**. 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`.
78+
79+
- If a `traces_sample_rate` is set, then performance information is also reported, which you can see on the **Performance** page of [sentry.io](https://sentry.io).
80+
81+
## Options
82+
83+
You can pass the following keyword arguments to `FastApiIntegration()`:
84+
85+
- `transaction_style`:
86+
87+
With this option, you can influence how the transactions are named in Sentry. For example:
88+
89+
```python
90+
sentry_sdk.init(
91+
# ...
92+
integrations=[
93+
StarletteIntegration(transaction_style="endpoint"),
94+
FastApiIntegration(transaction_style="endpoint"),
95+
],
96+
)
97+
98+
app = FastAPI()
99+
100+
@app.get("/catalog/product/{product_id}")
101+
async def product_detail(product_id):
102+
return {...}
103+
```
104+
105+
In the above code, the transaction name will be:
106+
107+
- `"/catalog/product/{product_id}"` if you set `transaction_style="url"`
108+
- `"product_detail"` if you set `transaction_style="endpoint"`
109+
110+
The default is `"url"`.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: Starlette
3+
redirect_from:
4+
- /clients/python/integrations/starlette/
5+
- /platforms/python/starlette/
6+
description: "Learn about using Sentry with Starlette."
7+
---
8+
9+
The Starlette integration adds support for the [Starlette Framework](https://www.starlette.io/).
10+
11+
## Install
12+
13+
Install `sentry-sdk` from PyPI with the `starlette` extra:
14+
15+
```bash
16+
pip install --upgrade 'sentry-sdk[starlette]'
17+
```
18+
19+
## Configure
20+
21+
To configure the SDK, initialize it with the integration before your app has been initialized:
22+
23+
```python
24+
from starlette.applications import Starlette
25+
26+
import sentry_sdk
27+
from sentry_sdk.integrations.starlette import StarletteIntegration
28+
29+
30+
sentry_sdk.init(
31+
dsn="___PUBLIC_DSN___",
32+
integrations=[
33+
StarletteIntegration(),
34+
],
35+
36+
# Set traces_sample_rate to 1.0 to capture 100%
37+
# of transactions for performance monitoring.
38+
# We recommend adjusting this value in production,
39+
traces_sample_rate=1.0,
40+
)
41+
42+
app = Starlette(routes=[...])
43+
```
44+
45+
<Note>
46+
47+
In the future, we will make this auto-enabling, so you won't need to add the `StarletteIntegration` to your `sentry_sdk.init()` call. Instead, the Sentry SDK will detect the presence of Starlette and will set up the integration automatically.
48+
49+
</Note>
50+
51+
## Verify
52+
53+
This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:
54+
55+
```python
56+
from starlette.applications import Starlette
57+
from starlette.routing import Route
58+
59+
60+
async def trigger_error(request):
61+
division_by_zero = 1 / 0
62+
63+
app = Starlette(routes=[
64+
Route("/sentry-debug", trigger_error),
65+
])
66+
```
67+
68+
Visiting `"/sentry-debug"` will trigger an error that will be captured by Sentry.
69+
70+
## Behavior
71+
72+
- All exceptions leading to an Internal Server Error are reported.
73+
74+
- Request data is attached to all events: **HTTP method, URL, headers, form data, JSON payloads**. 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`.
75+
76+
- If a `traces_sample_rate` is set, then performance information is also reported, which you can see on the **Performance** page of [sentry.io](https://sentry.io).
77+
78+
## Options
79+
80+
You can pass the following keyword arguments to `StarletteIntegration()`:
81+
82+
- `transaction_style`:
83+
84+
With this option, you can influence how the transactions are named in Sentry. For example:
85+
86+
```python
87+
sentry_sdk.init(
88+
# ...
89+
integrations=[
90+
StarletteIntegration(transaction_style="endpoint"),
91+
],
92+
)
93+
94+
async def product_detail(request):
95+
return JSONResponse({...})
96+
97+
app = Starlette(routes=[
98+
Route('/catalog/product/{product_id}', product_detail),
99+
])
100+
```
101+
102+
In the above code, the transaction name will be:
103+
104+
- `"/catalog/product/{product_id}"` if you set `transaction_style="url"`.
105+
- `"product_detail"` if you set `transaction_style="endpoint"`
106+
107+
The default is `"url"`.

src/wizard/python/fastapi.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
name: FastAPI
3+
doc_link: https://docs.sentry.io/platforms/python/guides/fastapi/
4+
support_level: production
5+
type: framework
6+
---
7+
8+
The FastAPI integration adds support for the [FastAPI Framework](https://fastapi.tiangolo.com/).
9+
10+
1. Install `sentry-sdk` from PyPI with the `fastapi` extra:
11+
12+
```bash
13+
pip install --upgrade 'sentry-sdk[fastapi]'
14+
```
15+
16+
2. To configure the SDK, initialize it with the integration before your app has been initialized:
17+
18+
```python
19+
from fastapi import FastAPI
20+
21+
import sentry_sdk
22+
from sentry_sdk.integrations.starlette import StarletteIntegration
23+
from sentry_sdk.integrations.fastapi import FastApiIntegration
24+
25+
26+
sentry_sdk.init(
27+
dsn="___PUBLIC_DSN___",
28+
integrations=[
29+
StarletteIntegration(),
30+
FastApiIntegration(),
31+
],
32+
33+
# Set traces_sample_rate to 1.0 to capture 100%
34+
# of transactions for performance monitoring.
35+
# We recommend adjusting this value in production,
36+
traces_sample_rate=1.0,
37+
)
38+
39+
app = FastAPI()
40+
```
41+
42+
The above configuration captures both error and performance data. To reduce the volume of performance data captured, change `traces_sample_rate` to a value between 0 and 1.
43+
44+
3. You can easily verify your Sentry installation by creating a route that triggers an error:
45+
46+
```python
47+
from fastapi import FastAPI
48+
49+
50+
app = FastAPI()
51+
52+
@app.get("/sentry-debug")
53+
async def trigger_error():
54+
division_by_zero = 1 / 0
55+
56+
```
57+
58+
Visiting this route will trigger an error that will be captured by Sentry.

src/wizard/python/starlette.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
name: Starlette
3+
doc_link: https://docs.sentry.io/platforms/python/guides/starlette/
4+
support_level: production
5+
type: framework
6+
---
7+
8+
The Starlette integration adds support for the [Starlette Framework](https://www.starlette.io/).
9+
10+
1. Install `sentry-sdk` from PyPI with the `starlette` extra:
11+
12+
```bash
13+
pip install --upgrade 'sentry-sdk[starlette]'
14+
```
15+
16+
2. To configure the SDK, initialize it with the integration before your app has been initialized:
17+
18+
```python
19+
from starlette.applications import Starlette
20+
21+
import sentry_sdk
22+
from sentry_sdk.integrations.starlette import StarletteIntegration
23+
24+
25+
sentry_sdk.init(
26+
dsn="___PUBLIC_DSN___",
27+
integrations=[
28+
StarletteIntegration(),
29+
],
30+
31+
# Set traces_sample_rate to 1.0 to capture 100%
32+
# of transactions for performance monitoring.
33+
# We recommend adjusting this value in production,
34+
traces_sample_rate=1.0,
35+
)
36+
37+
app = Starlette(routes=[...])
38+
```
39+
40+
The above configuration captures both error and performance data. To reduce the volume of performance data captured, change `traces_sample_rate` to a value between 0 and 1.
41+
42+
3. You can easily verify your Sentry installation by creating a route that triggers an error:
43+
44+
```python
45+
from starlette.applications import Starlette
46+
from starlette.routing import Route
47+
48+
49+
async def trigger_error(request):
50+
division_by_zero = 1 / 0
51+
52+
app = Starlette(routes=[
53+
Route("/sentry-debug", trigger_error),
54+
])
55+
```
56+
57+
Visiting this route will trigger an error that will be captured by Sentry.

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13878,10 +13878,10 @@ pkg-dir@^4.1.0, pkg-dir@^4.2.0:
1387813878
dependencies:
1387913879
find-up "^4.0.0"
1388013880

13881-
platformicons@4.4.0:
13882-
version "4.4.0"
13883-
resolved "https://registry.yarnpkg.com/platformicons/-/platformicons-4.4.0.tgz#4188696c73fcc40afff212aa3ed0d0559cb25607"
13884-
integrity sha512-aC1akUKsIh9WJe7uRpfbuiMz/Xp4WzsxIx1z07OXahFOf2+QNlnNcN/D1Lpt2ymdJ8y86KCkjunl4WbPn3BArw==
13881+
platformicons@5.2.1:
13882+
version "5.2.1"
13883+
resolved "https://registry.yarnpkg.com/platformicons/-/platformicons-5.2.1.tgz#4edbb1db58bf84c3f2fd3ed65afb90110253929e"
13884+
integrity sha512-56S/1RHaSYeeYCpumg+jWGAnL2iCd3i3xVLqwN9F26LzGz2SMZA8LBkEEbFWf2DbBv3N5GnsgUDtJrnN1f0SqA==
1388513885
dependencies:
1388613886
"@types/node" "*"
1388713887
"@types/react" "^16 || ^17"

0 commit comments

Comments
 (0)