feat: Add `set_attribute`, `remove_attribute` to global API (#5555) · gitcommit90/sentry-python@8628732 · GitHub
Skip to content

Commit 8628732

Browse files
authored
feat: Add set_attribute, remove_attribute to global API (getsentry#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: getsentry#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

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

0 commit comments

Comments
 (0)