Attributes
Attributes automatically enrich your telemetry with typed key-value data. Use them to add business context that you can then filter and search in Sentry.
Available since: v2.54.0
Attributes are key-value pairs you can attach to your telemetry (like spans, logs, and metrics).
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.
Attributes on spans require stream mode (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.
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:
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")
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:
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")
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 Scopes for more on the global, isolation, and current scopes.
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.
In stream mode, you can set attributes when starting a span:
import sentry_sdk
with sentry_sdk.traces.start_span(
name="process-order",
attributes={
"sentry.op": "queue.process",
"order.id": "abc-123",
"order.item_count": 5,
"order.priority": True,
},
):
process_order()
import sentry_sdk
with sentry_sdk.traces.start_span(
name="process-order",
attributes={
"sentry.op": "queue.process",
"order.id": "abc-123",
"order.item_count": 5,
"order.priority": True,
},
):
process_order()
Or add them to an already running span:
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",
})
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",
})
See Add Span Attributes for more.
Pass attributes to any sentry_sdk.logger call via the attributes kwarg:
sentry_sdk.logger.error(
"Payment processing failed",
attributes={
"payment.provider": "stripe",
"payment.method": "credit_card",
}
)
sentry_sdk.logger.error(
"Payment processing failed",
attributes={
"payment.provider": "stripe",
"payment.method": "credit_card",
}
)
See Logs for more.
Pass attributes in the attributes parameter of any metric:
import sentry_sdk
sentry_sdk.metrics.count(
"button_click",
5,
attributes={
"browser": "Firefox",
"app_version": "1.0.0"
},
)
import sentry_sdk
sentry_sdk.metrics.count(
"button_click",
5,
attributes={
"browser": "Firefox",
"app_version": "1.0.0"
},
)
See Application Metrics for more.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").