From a153e192c44352f312c8d7f0dcbe2ed4ddea966f Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 10 Jan 2025 14:40:31 +0100 Subject: [PATCH] Moved adding of flags context into scope --- sentry_sdk/flag_utils.py | 11 +---------- sentry_sdk/integrations/feature_flags.py | 8 -------- sentry_sdk/integrations/launchdarkly.py | 7 ------- sentry_sdk/integrations/openfeature.py | 4 ---- sentry_sdk/integrations/unleash.py | 5 ----- sentry_sdk/scope.py | 3 +++ 6 files changed, 4 insertions(+), 34 deletions(-) diff --git a/sentry_sdk/flag_utils.py b/sentry_sdk/flag_utils.py index cf4800e855..5a6e483e21 100644 --- a/sentry_sdk/flag_utils.py +++ b/sentry_sdk/flag_utils.py @@ -1,11 +1,9 @@ from typing import TYPE_CHECKING -import sentry_sdk from sentry_sdk._lru_cache import LRUCache if TYPE_CHECKING: - from typing import TypedDict, Optional - from sentry_sdk._types import Event, ExcInfo + from typing import TypedDict FlagData = TypedDict("FlagData", {"flag": str, "result": bool}) @@ -31,10 +29,3 @@ def get(self): def set(self, flag, result): # type: (str, bool) -> None self.buffer.set(flag, result) - - -def flag_error_processor(event, exc_info): - # type: (Event, ExcInfo) -> Optional[Event] - scope = sentry_sdk.get_current_scope() - event["contexts"]["flags"] = {"values": scope.flags.get()} - return event diff --git a/sentry_sdk/integrations/feature_flags.py b/sentry_sdk/integrations/feature_flags.py index 2aeabffbfa..2739e93f3e 100644 --- a/sentry_sdk/integrations/feature_flags.py +++ b/sentry_sdk/integrations/feature_flags.py @@ -1,5 +1,3 @@ -from sentry_sdk.flag_utils import flag_error_processor - import sentry_sdk from sentry_sdk.integrations import Integration @@ -27,12 +25,6 @@ class FeatureFlagsIntegration(Integration): identifier = "feature_flags" - @staticmethod - def setup_once(): - # type: () -> None - scope = sentry_sdk.get_current_scope() - scope.add_error_processor(flag_error_processor) - def add_feature_flag(flag, result): # type: (str, bool) -> None diff --git a/sentry_sdk/integrations/launchdarkly.py b/sentry_sdk/integrations/launchdarkly.py index a9eef9e1a9..e0c1561a28 100644 --- a/sentry_sdk/integrations/launchdarkly.py +++ b/sentry_sdk/integrations/launchdarkly.py @@ -2,7 +2,6 @@ import sentry_sdk from sentry_sdk.integrations import DidNotEnable, Integration -from sentry_sdk.flag_utils import flag_error_processor try: import ldclient @@ -38,12 +37,6 @@ def __init__(self, ld_client=None): # Register the flag collection hook with the LD client. client.add_hook(LaunchDarklyHook()) - @staticmethod - def setup_once(): - # type: () -> None - scope = sentry_sdk.get_current_scope() - scope.add_error_processor(flag_error_processor) - class LaunchDarklyHook(Hook): diff --git a/sentry_sdk/integrations/openfeature.py b/sentry_sdk/integrations/openfeature.py index 18f968a703..bf66b94e8b 100644 --- a/sentry_sdk/integrations/openfeature.py +++ b/sentry_sdk/integrations/openfeature.py @@ -2,7 +2,6 @@ import sentry_sdk from sentry_sdk.integrations import DidNotEnable, Integration -from sentry_sdk.flag_utils import flag_error_processor try: from openfeature import api @@ -21,9 +20,6 @@ class OpenFeatureIntegration(Integration): @staticmethod def setup_once(): # type: () -> None - scope = sentry_sdk.get_current_scope() - scope.add_error_processor(flag_error_processor) - # Register the hook within the global openfeature hooks list. api.add_hooks(hooks=[OpenFeatureHook()]) diff --git a/sentry_sdk/integrations/unleash.py b/sentry_sdk/integrations/unleash.py index 33b0a4b9dc..442ec39d0f 100644 --- a/sentry_sdk/integrations/unleash.py +++ b/sentry_sdk/integrations/unleash.py @@ -2,7 +2,6 @@ from typing import Any import sentry_sdk -from sentry_sdk.flag_utils import flag_error_processor from sentry_sdk.integrations import Integration, DidNotEnable try: @@ -49,7 +48,3 @@ def sentry_get_variant(self, feature, *args, **kwargs): UnleashClient.is_enabled = sentry_is_enabled # type: ignore UnleashClient.get_variant = sentry_get_variant # type: ignore - - # Error processor - scope = sentry_sdk.get_current_scope() - scope.add_error_processor(flag_error_processor) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index cf72fabdd1..85a36109ef 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -1378,6 +1378,9 @@ def _apply_contexts_to_event(self, event, hint, options): else: contexts["trace"] = self.get_trace_context() + # Add "flags" context + contexts.setdefault("flags", {}).update({"values": self.flags.get()}) + def _drop(self, cause, ty): # type: (Any, str) -> Optional[Any] logger.info("%s (%s) dropped event", ty, cause)