Skip to content

Commit 7944811

Browse files
committed
Abstract into the add_feature_flag function
1 parent aca8bde commit 7944811

File tree

5 files changed

+11
-47
lines changed

5 files changed

+11
-47
lines changed

sentry_sdk/feature_flags.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,7 @@ def add_feature_flag(flag, result):
6666
"""
6767
flags = sentry_sdk.get_current_scope().flags
6868
flags.set(flag, result)
69+
70+
span = sentry_sdk.get_current_span()
71+
if span:
72+
span.set_data(f"flag.evaluation.{flag}", result)

sentry_sdk/integrations/launchdarkly.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import TYPE_CHECKING
2-
import sentry_sdk
32

3+
from sentry_sdk.feature_flags import add_feature_flag
44
from sentry_sdk.integrations import DidNotEnable, Integration
55

66
try:
@@ -53,14 +53,7 @@ def metadata(self):
5353
def after_evaluation(self, series_context, data, detail):
5454
# type: (EvaluationSeriesContext, dict[Any, Any], EvaluationDetail) -> dict[Any, Any]
5555
if isinstance(detail.value, bool):
56-
# Errors support.
57-
flags = sentry_sdk.get_current_scope().flags
58-
flags.set(series_context.key, detail.value)
59-
60-
# Spans support.
61-
span = sentry_sdk.get_current_span()
62-
if span:
63-
span.set_data(f"flag.evaluation.{series_context.key}", detail.value)
56+
add_feature_flag(series_context.key, detail.value)
6457

6558
return data
6659

sentry_sdk/integrations/openfeature.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import TYPE_CHECKING
2-
import sentry_sdk
32

3+
from sentry_sdk.feature_flags import add_feature_flag
44
from sentry_sdk.integrations import DidNotEnable, Integration
55

66
try:
@@ -29,26 +29,9 @@ class OpenFeatureHook(Hook):
2929
def after(self, hook_context, details, hints):
3030
# type: (HookContext, FlagEvaluationDetails[bool], HookHints) -> None
3131
if isinstance(details.value, bool):
32-
# Errors support.
33-
flags = sentry_sdk.get_current_scope().flags
34-
flags.set(details.flag_key, details.value)
35-
36-
# Spans support.
37-
span = sentry_sdk.get_current_span()
38-
if span:
39-
span.set_data(f"flag.evaluation.{details.flag_key}", details.value)
32+
add_feature_flag(details.flag_key, details.value)
4033

4134
def error(self, hook_context, exception, hints):
4235
# type: (HookContext, Exception, HookHints) -> None
4336
if isinstance(hook_context.default_value, bool):
44-
# Errors support.
45-
flags = sentry_sdk.get_current_scope().flags
46-
flags.set(hook_context.flag_key, hook_context.default_value)
47-
48-
# Spans support.
49-
span = sentry_sdk.get_current_span()
50-
if span:
51-
span.set_data(
52-
f"flag.evaluation.{hook_context.flag_key}",
53-
hook_context.default_value,
54-
)
37+
add_feature_flag(hook_context.flag_key, hook_context.default_value)

sentry_sdk/integrations/statsig.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from functools import wraps
22
from typing import Any, TYPE_CHECKING
33

4-
import sentry_sdk
54
from sentry_sdk.feature_flags import add_feature_flag
65
from sentry_sdk.integrations import Integration, DidNotEnable, _check_minimum_version
76
from sentry_sdk.utils import parse_version
@@ -31,15 +30,8 @@ def setup_once():
3130
@wraps(old_check_gate)
3231
def sentry_check_gate(user, gate, *args, **kwargs):
3332
# type: (StatsigUser, str, *Any, **Any) -> Any
34-
# Errors support.
3533
enabled = old_check_gate(user, gate, *args, **kwargs)
3634
add_feature_flag(gate, enabled)
37-
38-
# Spans support.
39-
span = sentry_sdk.get_current_span()
40-
if span:
41-
span.set_data(f"flag.evaluation.{gate}", enabled)
42-
4335
return enabled
4436

4537
statsig_module.check_gate = sentry_check_gate

sentry_sdk/integrations/unleash.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from functools import wraps
22
from typing import Any
33

4-
import sentry_sdk
4+
from sentry_sdk.feature_flags import add_feature_flag
55
from sentry_sdk.integrations import Integration, DidNotEnable
66

77
try:
@@ -24,17 +24,9 @@ def sentry_is_enabled(self, feature, *args, **kwargs):
2424
# type: (UnleashClient, str, *Any, **Any) -> Any
2525
enabled = old_is_enabled(self, feature, *args, **kwargs)
2626

27-
# Errors support.
28-
#
2927
# We have no way of knowing what type of unleash feature this is, so we have to treat
3028
# it as a boolean / toggle feature.
31-
flags = sentry_sdk.get_current_scope().flags
32-
flags.set(feature, enabled)
33-
34-
# Spans support.
35-
span = sentry_sdk.get_current_span()
36-
if span:
37-
span.set_data(f"flag.evaluation.{feature}", enabled)
29+
add_feature_flag(feature, enabled)
3830

3931
return enabled
4032

0 commit comments

Comments
 (0)