Skip to content

Commit f2317dc

Browse files
authored
ref(scope): Set global attrs on global scope (#5259)
### Description Since we now have scope attributes, we can set global attributes on the global scope and they'll get automatically applied when the scopes are merged. #### 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 3d83b39 commit f2317dc

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

sentry_sdk/scope.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from functools import wraps
1010
from itertools import chain
1111

12+
import sentry_sdk
1213
from sentry_sdk._types import AnnotatedValue
1314
from sentry_sdk.attachments import Attachment
1415
from sentry_sdk.consts import (
@@ -365,6 +366,26 @@ def get_global_scope(cls) -> "Scope":
365366

366367
return _global_scope
367368

369+
def set_global_attributes(self) -> None:
370+
from sentry_sdk.client import SDK_INFO
371+
372+
self.set_attribute("sentry.sdk.name", SDK_INFO["name"])
373+
self.set_attribute("sentry.sdk.version", SDK_INFO["version"])
374+
375+
options = sentry_sdk.get_client().options
376+
377+
server_name = options.get("server_name")
378+
if server_name:
379+
self.set_attribute(SPANDATA.SERVER_ADDRESS, server_name)
380+
381+
environment = options.get("environment")
382+
if environment:
383+
self.set_attribute("sentry.environment", environment)
384+
385+
release = options.get("release")
386+
if release:
387+
self.set_attribute("sentry.release", release)
388+
368389
@classmethod
369390
def last_event_id(cls) -> "Optional[str]":
370391
"""
@@ -467,7 +488,14 @@ def set_client(
467488
If `None` the client of the scope will be replaced by a :py:class:`sentry_sdk.NonRecordingClient`.
468489
469490
"""
470-
self.client = client if client is not None else NonRecordingClient()
491+
if client is not None:
492+
self.client = client
493+
# We need a client to set the initial global attributes on the global
494+
# scope since they mostly come from client options, so populate them
495+
# as soon as a client is set
496+
sentry_sdk.get_global_scope().set_global_attributes()
497+
else:
498+
self.client = NonRecordingClient()
471499

472500
def fork(self) -> "Scope":
473501
"""
@@ -1468,33 +1496,6 @@ def _apply_flags_to_event(
14681496
{"values": flags}
14691497
)
14701498

1471-
def _apply_global_attributes_to_telemetry(
1472-
self, telemetry: "Union[Log, Metric]"
1473-
) -> None:
1474-
# TODO: Global stuff like this should just be retrieved at init time and
1475-
# put onto the global scope's attributes and then applied to events
1476-
# from there
1477-
from sentry_sdk.client import SDK_INFO
1478-
1479-
attributes = telemetry["attributes"]
1480-
1481-
attributes["sentry.sdk.name"] = SDK_INFO["name"]
1482-
attributes["sentry.sdk.version"] = SDK_INFO["version"]
1483-
1484-
options = self.get_client().options
1485-
1486-
server_name = options.get("server_name")
1487-
if server_name is not None and SPANDATA.SERVER_ADDRESS not in attributes:
1488-
attributes[SPANDATA.SERVER_ADDRESS] = server_name
1489-
1490-
environment = options.get("environment")
1491-
if environment is not None and "sentry.environment" not in attributes:
1492-
attributes["sentry.environment"] = environment
1493-
1494-
release = options.get("release")
1495-
if release is not None and "sentry.release" not in attributes:
1496-
attributes["sentry.release"] = release
1497-
14981499
def _apply_scope_attributes_to_telemetry(
14991500
self, telemetry: "Union[Log, Metric]"
15001501
) -> None:
@@ -1638,7 +1639,6 @@ def apply_to_telemetry(self, telemetry: "Union[Log, Metric]") -> None:
16381639

16391640
self._apply_scope_attributes_to_telemetry(telemetry)
16401641
self._apply_user_attributes_to_telemetry(telemetry)
1641-
self._apply_global_attributes_to_telemetry(telemetry)
16421642

16431643
def update_from_scope(self, scope: "Scope") -> None:
16441644
"""Update the scope with another scope's data."""

0 commit comments

Comments
 (0)