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.

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:

Copied
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:

Copied
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:

Copied
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:

Copied
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:

Copied
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:

Copied
import sentry_sdk

sentry_sdk.metrics.count(
    "button_click",
    5,
    attributes={
        "browser": "Firefox",
        "app_version": "1.0.0"
    },
)

See Application Metrics for more.

Was this helpful?
Help improve this content
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").