Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved adding of flags context into Scope #3917

Merged
merged 15 commits into from
Jan 13, 2025
Merged
11 changes: 1 addition & 10 deletions sentry_sdk/flag_utils.py
Original file line number Diff line number Diff line change
@@ -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})

Expand All @@ -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
8 changes: 0 additions & 8 deletions sentry_sdk/integrations/feature_flags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from sentry_sdk.flag_utils import flag_error_processor

import sentry_sdk
from sentry_sdk.integrations import Integration

Expand Down Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions sentry_sdk/integrations/launchdarkly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):

Expand Down
4 changes: 0 additions & 4 deletions sentry_sdk/integrations/openfeature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()])

Expand Down
5 changes: 0 additions & 5 deletions sentry_sdk/integrations/unleash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
3 changes: 3 additions & 0 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

def _drop(self, cause, ty):
# type: (Any, str) -> Optional[Any]
logger.info("%s (%s) dropped event", ty, cause)
Expand Down
Loading