Skip to content

Commit 9041460

Browse files
antonpirkergetsantry[bot]shanamatthews
authored
Getting Started: fastapi (#7836)
* Getting Started: fastapi * style(lint): Auto commit lint changes * fix * Apply suggestions from code review Co-authored-by: Shana Matthews <shana.l.matthews@gmail.com> --------- Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com> Co-authored-by: Shana Matthews <shana.l.matthews@gmail.com>
1 parent 8dff2f4 commit 9041460

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

src/platforms/python/guides/fastapi/index.mdx

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,18 @@ pip install --upgrade 'sentry-sdk[fastapi]'
1818

1919
## Configure
2020

21-
To configure the Sentry SDK, initialize it before your app has been initialized:
21+
If you have the `fastapi` package in your dependencies, the FastAPI integration will be enabled automatically when you initialize the Sentry SDK.
2222

2323
<SignInNote />
2424

2525
```python
2626
from fastapi import FastAPI
27-
2827
import sentry_sdk
2928

30-
3129
sentry_sdk.init(
3230
dsn="___PUBLIC_DSN___",
33-
3431
# Set traces_sample_rate to 1.0 to capture 100%
3532
# of transactions for performance monitoring.
36-
# We recommend adjusting this value in production,
3733
traces_sample_rate=1.0,
3834
)
3935

@@ -42,21 +38,21 @@ app = FastAPI()
4238

4339
## Verify
4440

45-
This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:
46-
4741
```python
4842
from fastapi import FastAPI
4943

44+
sentry_sdk.init(...) # same as above
5045

5146
app = FastAPI()
5247

5348
@app.get("/sentry-debug")
5449
async def trigger_error():
5550
division_by_zero = 1 / 0
56-
5751
```
5852

59-
Visiting `"/sentry-debug"` will trigger an error that will be captured by Sentry.
53+
When you point your browser to [http://localhost:8000/sentry-debug](http://localhost:8000/sentry-debug) a transaction will be created in the Performance section of [sentry.io](https://sentry.io). Additionally, an error event will be sent to [sentry.io](https://sentry.io) and will be connected to the transaction.
54+
55+
It takes a couple of moments for the data to appear in [sentry.io](https://sentry.io).
6056

6157
## Behaviour
6258

@@ -88,43 +84,59 @@ The parameter `traces_sample_rate` needs to be set when initializing the Sentry
8884

8985
</Note>
9086

91-
## Integration Options
92-
93-
If you want to change the default behavior of the FastAPI integration, you need to instantiate the integration manually and then pass it to Sentry's `init` function. Because FastAPI is based on the Starlette framework, both integrations, `StarletteIntegration` and `FastApiIntegration`, must be instantiated.
94-
95-
<ConfigKey name="transaction_style">
87+
## Options
9688

97-
You can pass the keyword argument `transaction_style` to `StarletteIntegration()` and `FastApiIntegration()`.
98-
99-
With this option, you can influence how the transactions are named in Sentry. For example:
89+
By adding `FastApiIntegration` to your `sentry_sdk.init()` call explicitly, you can set options for `FastApiIntegration` to change its behavior.
90+
Because FastAPI is based on the Starlette framework, both integrations, `StarletteIntegration` and `FastApiIntegration`, must be instantiated.
10091

10192
```python
10293
from sentry_sdk.integrations.starlette import StarletteIntegration
10394
from sentry_sdk.integrations.fastapi import FastApiIntegration
10495

10596
sentry_sdk.init(
106-
# ...
97+
dsn="___PUBLIC_DSN___",
98+
# Set traces_sample_rate to 1.0 to capture 100%
99+
# of transactions for performance monitoring.
100+
traces_sample_rate=1.0,
107101
integrations=[
108-
StarletteIntegration(transaction_style="endpoint"),
109-
FastApiIntegration(transaction_style="endpoint"),
110-
],
102+
StarletteIntegration(transaction_style="endpoint",),
103+
FastApiIntegration(transaction_style="endpoint",),
104+
]
111105
)
106+
```
112107

113-
app = FastAPI()
108+
You can pass the following keyword arguments to `StarletteIntegration()` and `FastApiIntegration()`:
114109

115-
@app.get("/catalog/product/{product_id}")
116-
async def product_detail(product_id):
117-
return {...}
118-
```
110+
- `transaction_style`:
111+
112+
This option lets you influence how the transactions are named in Sentry. For example:
113+
114+
```python
115+
import sentry_sdk
116+
from sentry_sdk.integrations.starlette import StarletteIntegration
117+
from sentry_sdk.integrations.fastapi import FastApiIntegration
118+
119+
sentry_sdk.init(
120+
# ...
121+
integrations=[
122+
StarletteIntegration(transaction_style="endpoint"),
123+
FastApiIntegration(transaction_style="endpoint"),
124+
],
125+
)
126+
127+
app = FastAPI()
119128

120-
In the above code, the transaction name will be:
129+
@app.get("/catalog/product/{product_id}")
130+
async def product_detail(product_id):
131+
return {...}
132+
```
121133

122-
- `"/catalog/product/{product_id}"` if you set `transaction_style="url"`
123-
- `"product_detail"` if you set `transaction_style="endpoint"`
134+
In the above code, the transaction name will be:
124135

125-
The default is `"url"`.
136+
- `"/catalog/product/{product_id}"` if you set `transaction_style="url"`
137+
- `"product_detail"` if you set `transaction_style="endpoint"`
126138

127-
</ConfigKey>
139+
The default is `"url"`.
128140

129141
## Supported Versions
130142

0 commit comments

Comments
 (0)