Skip to content
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

Add excluded_urls example for / #487

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 6 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
38 changes: 38 additions & 0 deletions docs/integrations/fastapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,44 @@ To avoid tracing certain URLs, you can specify a string of comma-separated regex
- The environment variable `OTEL_PYTHON_FASTAPI_EXCLUDED_URLS`.
- The environment variable `OTEL_PYTHON_EXCLUDED_URLS` (which will also apply to other instrumentation).

!!! example
If you'd like to trace all URLs except the base `/` URL, you can use the following regex for `excluded_urls`: `^https?://[^/]+/$`

Breaking it down:

* `^` matches the start of the string
* `https?` matches `http` or `https`
* `://` matches `://`
* `[^/]+` matches one or more characters that are not `/` (this will be the host part of the URL)
* `/` matches `/`
* `$` matches the end of the string

So this regex will only match routes that have no path after the host.

This instrumentation might look like:

```py
from fastapi import FastAPI

import logfire

app = FastAPI()

logfire.configure()
logfire.instrument_fastapi(app, excluded_urls='^https?://[^/]+/$')

if __name__ == '__main__':
import uvicorn

uvicorn.run(app)
```

If you visit http://127.0.0.1:8000/, that matches the above regex, so no span will be sent to logfire.
If you visit http://127.0.0.1:8000/hello/ (or any other endpoing that's not `/`, for that matter), a span will be sent to logfire.
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved

Note that under the hood, the `opentelemetry` library is using `re.search` (not `re.match` or `re.fullmatch`) to check for a match between the route and the `excluded_urls` regex, which is why we need to include the `^` at the start and `$` at the end of the regex.


## Capturing request and response headers
<!-- note that this section is duplicated for different frameworks but with slightly different links -->

Expand Down