|
9 | 9 | from functools import wraps |
10 | 10 | from itertools import chain |
11 | 11 |
|
| 12 | +import sentry_sdk |
12 | 13 | from sentry_sdk._types import AnnotatedValue |
13 | 14 | from sentry_sdk.attachments import Attachment |
14 | 15 | from sentry_sdk.consts import ( |
@@ -365,6 +366,26 @@ def get_global_scope(cls) -> "Scope": |
365 | 366 |
|
366 | 367 | return _global_scope |
367 | 368 |
|
| 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 | + |
368 | 389 | @classmethod |
369 | 390 | def last_event_id(cls) -> "Optional[str]": |
370 | 391 | """ |
@@ -467,7 +488,14 @@ def set_client( |
467 | 488 | If `None` the client of the scope will be replaced by a :py:class:`sentry_sdk.NonRecordingClient`. |
468 | 489 |
|
469 | 490 | """ |
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() |
471 | 499 |
|
472 | 500 | def fork(self) -> "Scope": |
473 | 501 | """ |
@@ -1468,33 +1496,6 @@ def _apply_flags_to_event( |
1468 | 1496 | {"values": flags} |
1469 | 1497 | ) |
1470 | 1498 |
|
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 | | - |
1498 | 1499 | def _apply_scope_attributes_to_telemetry( |
1499 | 1500 | self, telemetry: "Union[Log, Metric]" |
1500 | 1501 | ) -> None: |
@@ -1638,7 +1639,6 @@ def apply_to_telemetry(self, telemetry: "Union[Log, Metric]") -> None: |
1638 | 1639 |
|
1639 | 1640 | self._apply_scope_attributes_to_telemetry(telemetry) |
1640 | 1641 | self._apply_user_attributes_to_telemetry(telemetry) |
1641 | | - self._apply_global_attributes_to_telemetry(telemetry) |
1642 | 1642 |
|
1643 | 1643 | def update_from_scope(self, scope: "Scope") -> None: |
1644 | 1644 | """Update the scope with another scope's data.""" |
|
0 commit comments