Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions sentry_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"last_event_id",
"new_scope",
"push_scope",
"remove_attribute",
"set_attribute",
"set_context",
"set_extra",
"set_level",
Expand Down
24 changes: 24 additions & 0 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def overload(x: "T") -> "T":
"last_event_id",
"new_scope",
"push_scope",
"remove_attribute",
"set_attribute",
Comment thread
sentrivana marked this conversation as resolved.
"set_context",
"set_extra",
"set_level",
Expand Down Expand Up @@ -287,6 +289,28 @@ def push_scope( # noqa: F811
return _ScopeManager()


@scopemethod
def set_attribute(key: str, value: "Any") -> None:
"""
Set an attribute.

Any attributes-based telemetry (logs, metrics) captured in this scope will
include this attribute.
"""
return get_isolation_scope().set_attribute(key, value)
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated


@scopemethod
def remove_attribute(key: str) -> None:
"""
Remove an attribute.

If the attribute doesn't exist, this function will not have any effect and
it will also not raise an exception.
"""
return get_isolation_scope().remove_attribute(key)


@scopemethod
def set_tag(key: str, value: "Any") -> None:
return get_isolation_scope().set_tag(key, value)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
from tests.test_metrics import envelopes_to_metrics


def test_top_level_api(sentry_init, capture_envelopes):
sentry_init()

envelopes = capture_envelopes()

sentry_sdk.set_attribute("set", "value")
sentry_sdk.set_attribute("removed", "value")
sentry_sdk.remove_attribute("removed")
Comment thread
sentrivana marked this conversation as resolved.
# Attempting to remove a nonexistent attribute should not raise
sentry_sdk.remove_attribute("nonexistent")

sentry_sdk.metrics.count("test", 1)
sentry_sdk.get_client().flush()

metrics = envelopes_to_metrics(envelopes)
(metric,) = metrics

assert metric["attributes"]["set"] == "value"
assert "removed" not in metric["attributes"]


def test_scope_precedence(sentry_init, capture_envelopes):
# Order of precedence, from most important to least:
# 1. telemetry attributes (directly supplying attributes on creation or using set_attribute)
Expand Down
Loading