-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
docs(python): Add Attributes page #18782
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
Open
inventarSarah
wants to merge
3
commits into
master
Choose a base branch
from
smi/attributes/python
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
docs/platforms/python/enriching-events/attributes/index.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| --- | ||
| title: Attributes | ||
| description: "Attributes automatically enrich your telemetry with typed key-value data. Use them to add business context that you can filter and search in Sentry." | ||
| --- | ||
|
|
||
| <AvailableSince version="2.54.0" /> | ||
|
|
||
| **Attributes** are key-value pairs you can attach to your telemetry (like <PlatformLink to="/tracing/streamed-spans">spans</PlatformLink>, <PlatformLink to="/logs">logs</PlatformLink>, and <PlatformLink to="/metrics">metrics</PlatformLink>). | ||
|
|
||
| Common uses include subscription tier, feature flags, or any business context that helps you filter and query your telemetry. | ||
|
|
||
| Attribute values can be `str`, `int`, `float`, or `bool`, as well as arrays of these types. | ||
|
|
||
| <Alert level="info"> | ||
|
|
||
| Attributes on spans require <PlatformLink to="/tracing/streamed-spans">stream mode</PlatformLink> (SDK version `2.62.0`+). In stream mode, spans have no contexts, data, or tags — everything is an attribute, so `set_data()`, `set_tag()`, and `set_context()` are replaced by `set_attribute()` and `set_attributes()`. | ||
|
|
||
| Attributes on logs and metrics don't require stream mode — they work regardless of your `trace_lifecycle` setting. | ||
|
|
||
| </Alert> | ||
|
|
||
| ## Add Attributes to a Scope | ||
|
|
||
| Use `sentry_sdk.set_attribute` to attach attributes that are automatically included on all logs and metrics — and, in stream mode, on all spans. These write to the isolation scope, so they apply for the current request or session: | ||
|
|
||
| ```python | ||
| import sentry_sdk | ||
|
|
||
| sentry_sdk.set_attribute("org_id", user.org_id) | ||
| sentry_sdk.set_attribute("user_tier", user.tier) | ||
| sentry_sdk.set_attribute("service", "checkout") | ||
| ``` | ||
|
|
||
| To attach attributes more broadly or more narrowly, set them on a specific scope instead: | ||
|
|
||
| ```python | ||
| import sentry_sdk | ||
|
|
||
| # Global scope — applies to every event for the life of the app | ||
| sentry_sdk.get_global_scope().set_attribute("app.version", "1.2.3") | ||
|
|
||
| # Current scope — applies only within this block | ||
| with sentry_sdk.new_scope() as scope: | ||
| scope.set_attribute("checkout.step", "payment") | ||
| sentry_sdk.logger.info("Processing payment") | ||
| ``` | ||
|
|
||
| See <PlatformLink to="/enriching-events/scopes">Scopes</PlatformLink> for more on the global, isolation, and current scopes. | ||
|
|
||
| ## Setting Attributes on Individual Telemetry | ||
|
|
||
| You can also attach attributes to a single span, log, or metric directly, which is useful when the data only makes sense for that one item. | ||
|
|
||
| ### Spans | ||
|
|
||
| In stream mode, you can set attributes when starting a span: | ||
|
|
||
| ```python | ||
| import sentry_sdk | ||
|
|
||
| with sentry_sdk.traces.start_span( | ||
| name="process-order", | ||
| attributes={ | ||
| "sentry.op": "queue.process", | ||
|
sentrivana marked this conversation as resolved.
|
||
| "order.id": "abc-123", | ||
| "order.item_count": 5, | ||
| "order.priority": True, | ||
| }, | ||
| ): | ||
| process_order() | ||
| ``` | ||
|
|
||
| Or add them to an already running span: | ||
|
|
||
| ```python | ||
| import sentry_sdk | ||
|
|
||
| with sentry_sdk.traces.start_span(name="handle-request") as span: | ||
| span.set_attribute("http.response.status_code", 200) | ||
|
|
||
| span.set_attributes({ | ||
| "http.route": "/api/users", | ||
| "user.id": "user-42", | ||
| }) | ||
| ``` | ||
|
inventarSarah marked this conversation as resolved.
|
||
|
|
||
| See <PlatformLink to="/tracing/streamed-spans/#add-span-attributes">Add Span Attributes</PlatformLink> for more. | ||
|
|
||
| ### Logs | ||
|
|
||
| Pass attributes to any `sentry_sdk.logger` call via the `attributes` kwarg: | ||
|
|
||
| ```python | ||
| sentry_sdk.logger.error( | ||
| "Payment processing failed", | ||
| attributes={ | ||
| "payment.provider": "stripe", | ||
| "payment.method": "credit_card", | ||
| } | ||
| ) | ||
| ``` | ||
|
|
||
| See <PlatformLink to="/logs">Logs</PlatformLink> for more. | ||
|
|
||
| ### Metrics | ||
|
|
||
| Pass attributes in the `attributes` parameter of any metric: | ||
|
|
||
| ```python | ||
| import sentry_sdk | ||
|
|
||
| sentry_sdk.metrics.count( | ||
| "button_click", | ||
| 5, | ||
| attributes={ | ||
| "browser": "Firefox", | ||
| "app_version": "1.0.0" | ||
| }, | ||
| ) | ||
| ``` | ||
|
|
||
| See <PlatformLink to="/metrics">Application Metrics</PlatformLink> for more. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.