Skip to content

Commit 8628732

Browse files
authored
feat: Add set_attribute, remove_attribute to global API (#5555)
### Description `set_attribute` and `remove_attribute` should also be part of the public API via `sentry_sdk.set_attribute()` and `sentry_sdk.remove_attribute()`, respectively, instead of requiring the user to know about the scope system in order to determine which scope to set the attributes on. #### Issues <!-- * resolves: #1234 * resolves: LIN-1234 --> #### Reminders - Please add tests to validate your changes, and lint your code using `tox -e linters`. - Add GH Issue ID _&_ Linear ID (if applicable) - PR title should use [conventional commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type) style (`feat:`, `fix:`, `ref:`, `meta:`) - For external contributors: [CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md), [Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord community](https://discord.gg/Ww9hbqr)
1 parent a4e4c57 commit 8628732

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

sentry_sdk/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
"last_event_id",
3838
"new_scope",
3939
"push_scope",
40+
"remove_attribute",
41+
"set_attribute",
4042
"set_context",
4143
"set_extra",
4244
"set_level",

sentry_sdk/api.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def overload(x: "T") -> "T":
7070
"last_event_id",
7171
"new_scope",
7272
"push_scope",
73+
"remove_attribute",
74+
"set_attribute",
7375
"set_context",
7476
"set_extra",
7577
"set_level",
@@ -287,6 +289,28 @@ def push_scope( # noqa: F811
287289
return _ScopeManager()
288290

289291

292+
@scopemethod
293+
def set_attribute(attribute: str, value: "Any") -> None:
294+
"""
295+
Set an attribute.
296+
297+
Any attributes-based telemetry (logs, metrics) captured in this scope will
298+
include this attribute.
299+
"""
300+
return get_isolation_scope().set_attribute(attribute, value)
301+
302+
303+
@scopemethod
304+
def remove_attribute(attribute: str) -> None:
305+
"""
306+
Remove an attribute.
307+
308+
If the attribute doesn't exist, this function will not have any effect and
309+
it will also not raise an exception.
310+
"""
311+
return get_isolation_scope().remove_attribute(attribute)
312+
313+
290314
@scopemethod
291315
def set_tag(key: str, value: "Any") -> None:
292316
return get_isolation_scope().set_tag(key, value)

tests/test_attributes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@
33
from tests.test_metrics import envelopes_to_metrics
44

55

6+
def test_top_level_api(sentry_init, capture_envelopes):
7+
sentry_init()
8+
9+
envelopes = capture_envelopes()
10+
11+
sentry_sdk.set_attribute("set", "value")
12+
sentry_sdk.set_attribute("removed", "value")
13+
sentry_sdk.remove_attribute("removed")
14+
# Attempting to remove a nonexistent attribute should not raise
15+
sentry_sdk.remove_attribute("nonexistent")
16+
17+
sentry_sdk.metrics.count("test", 1)
18+
sentry_sdk.get_client().flush()
19+
20+
metrics = envelopes_to_metrics(envelopes)
21+
(metric,) = metrics
22+
23+
assert metric["attributes"]["set"] == "value"
24+
assert "removed" not in metric["attributes"]
25+
26+
627
def test_scope_precedence(sentry_init, capture_envelopes):
728
# Order of precedence, from most important to least:
829
# 1. telemetry attributes (directly supplying attributes on creation or using set_attribute)

0 commit comments

Comments
 (0)