forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunchdarkly.py
More file actions
63 lines (48 loc) · 1.87 KB
/
Copy pathlaunchdarkly.py
File metadata and controls
63 lines (48 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import TYPE_CHECKING
from sentry_sdk.feature_flags import add_feature_flag
from sentry_sdk.integrations import DidNotEnable, Integration
try:
import ldclient
from ldclient.hook import Hook, Metadata
if TYPE_CHECKING:
from typing import Any
from ldclient import LDClient
from ldclient.evaluation import EvaluationDetail
from ldclient.hook import EvaluationSeriesContext
except ImportError:
raise DidNotEnable("LaunchDarkly is not installed")
class LaunchDarklyIntegration(Integration):
identifier = "launchdarkly"
def __init__(self, ld_client: "LDClient | None" = None) -> None:
"""
:param client: An initialized LDClient instance. If a client is not provided, this
integration will attempt to use the shared global instance.
"""
try:
client = ld_client or ldclient.get()
except Exception as exc:
raise DidNotEnable("Error getting LaunchDarkly client. " + repr(exc))
if not client.is_initialized():
raise DidNotEnable("LaunchDarkly client is not initialized.")
# Register the flag collection hook with the LD client.
client.add_hook(LaunchDarklyHook())
@staticmethod
def setup_once() -> None:
pass
class LaunchDarklyHook(Hook):
@property
def metadata(self) -> "Metadata":
return Metadata(name="sentry-flag-auditor")
def after_evaluation(
self,
series_context: "EvaluationSeriesContext",
data: "dict[Any, Any]",
detail: "EvaluationDetail",
) -> "dict[Any, Any]":
if isinstance(detail.value, bool):
add_feature_flag(series_context.key, detail.value)
return data
def before_evaluation(
self, series_context: "EvaluationSeriesContext", data: "dict[Any, Any]"
) -> "dict[Any, Any]":
return data # No-op.