Skip to content

Commit c25b802

Browse files
committed
Initial src and dependency code
1 parent 3f7fdc8 commit c25b802

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

requirements-linting.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ pep8-naming
1616
pre-commit # local linting
1717
httpcore
1818
openfeature-sdk
19+
launchdarkly-server-sdk

scripts/split-tox-gh-actions/split-tox-gh-actions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
"tornado",
126126
],
127127
"Miscellaneous": [
128+
"ldclient",
128129
"loguru",
129130
"openfeature",
130131
"opentelemetry",
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from typing import TYPE_CHECKING
2+
import sentry_sdk
3+
4+
from sentry_sdk.integrations import DidNotEnable, Integration
5+
6+
try:
7+
import ldclient
8+
from ldclient.hook import Hook
9+
10+
if TYPE_CHECKING:
11+
from ldclient import LDClient
12+
from ldclient.hook import EvaluationSeriesContext, Metadata
13+
from ldclient.evaluation import EvaluationDetail
14+
15+
from sentry_sdk._types import Event, ExcInfo
16+
from typing import Any, Optional
17+
except ImportError:
18+
raise DidNotEnable("LaunchDarkly is not installed")
19+
20+
21+
class LaunchDarklyIntegration(Integration):
22+
identifier = "launchdarkly"
23+
24+
@staticmethod
25+
def get_ldclient():
26+
# type: () -> LDClient
27+
try:
28+
client = ldclient.get()
29+
except Exception as exc:
30+
sentry_sdk.capture_exception(exc)
31+
raise DidNotEnable(
32+
"Failed to find LaunchDarkly client instance. " + str(exc)
33+
)
34+
35+
if client and client.is_initialized():
36+
return client
37+
raise DidNotEnable("LaunchDarkly client is not initialized")
38+
39+
@staticmethod
40+
def setup_once():
41+
# type: () -> None
42+
def error_processor(event, _exc_info):
43+
# type: (Event, ExcInfo) -> Optional[Event]
44+
scope = sentry_sdk.get_current_scope()
45+
event["contexts"]["flags"] = {"values": scope.flags.get()}
46+
return event
47+
48+
scope = sentry_sdk.get_current_scope()
49+
scope.add_error_processor(error_processor)
50+
51+
# Register the hook with the global launchdarkly client.
52+
client = LaunchDarklyIntegration.get_ldclient()
53+
client.add_hook(LaunchDarklyHook())
54+
55+
56+
class LaunchDarklyHook(Hook):
57+
58+
@property
59+
def metadata(self):
60+
# type: () -> Metadata
61+
return Metadata(name="sentry-on-error-hook")
62+
63+
def after_evaluation(self, series_context, data, detail):
64+
# type: (EvaluationSeriesContext, dict[Any, Any], EvaluationDetail) -> dict[Any, Any]
65+
if isinstance(detail.value, bool):
66+
flags = sentry_sdk.get_current_scope().flags
67+
flags.set(series_context.key, detail.value)
68+
return data
69+
70+
def before_evaluation(self, _series_context, data):
71+
# type: (EvaluationSeriesContext, dict[Any, Any]) -> dict[Any, Any]
72+
return data # No-op.

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def get_file_text(file_name):
6363
"huey": ["huey>=2"],
6464
"huggingface_hub": ["huggingface_hub>=0.22"],
6565
"langchain": ["langchain>=0.0.210"],
66+
"ldclient": ["launchdarkly-server-sdk>=9.8.0"],
6667
"litestar": ["litestar>=2.0.0"],
6768
"loguru": ["loguru>=0.5"],
6869
"openai": ["openai>=1.0.0", "tiktoken>=0.3.0"],
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pytest
2+
3+
pytest.importorskip("ldclient")

tox.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ envlist =
167167
{py3.9,py3.11,py3.12}-langchain-latest
168168
{py3.9,py3.11,py3.12}-langchain-notiktoken
169169

170+
# LaunchDarkly
171+
{py3.8,py3.9,py3.10,py3.11,py3.12}-launchdarkly-latest
172+
170173
# Litestar
171174
# litestar 2.0.0 is the earliest version that supports Python < 3.12
172175
{py3.8,py3.11}-litestar-v{2.0}
@@ -520,6 +523,9 @@ deps =
520523
langchain-notiktoken: langchain-openai
521524
langchain-notiktoken: openai>=1.6.1
522525

526+
# LaunchDarkly
527+
launchdarkly-latest: launchdarkly-server-sdk~=9.8.0
528+
523529
# Litestar
524530
litestar: pytest-asyncio
525531
litestar: python-multipart
@@ -727,6 +733,7 @@ setenv =
727733
huey: TESTPATH=tests/integrations/huey
728734
huggingface_hub: TESTPATH=tests/integrations/huggingface_hub
729735
langchain: TESTPATH=tests/integrations/langchain
736+
launchdarkly: TESTPATH=tests/integrations/launchdarkly
730737
litestar: TESTPATH=tests/integrations/litestar
731738
loguru: TESTPATH=tests/integrations/loguru
732739
openai: TESTPATH=tests/integrations/openai

0 commit comments

Comments
 (0)