-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement OpenTelemetry traces hook
- Loading branch information
1 parent
1c52d12
commit febd150
Showing
5 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import json | ||
|
||
from open_feature.flag_evaluation.flag_evaluation_details import FlagEvaluationDetails | ||
from open_feature.hooks.hook import Hook | ||
from open_feature.hooks.hook_context import HookContext | ||
from opentelemetry import trace | ||
|
||
|
||
OTEL_EVENT_NAME = "feature_flag" | ||
|
||
|
||
class EventAttributes: | ||
FLAG_KEY = f"{OTEL_EVENT_NAME}.key" | ||
FLAG_VARIANT = f"{OTEL_EVENT_NAME}.variant" | ||
PROVIDER_NAME = f"{OTEL_EVENT_NAME}.provider_name" | ||
|
||
|
||
class OTelTracesHook(Hook): | ||
def after( | ||
self, hook_context: HookContext, details: FlagEvaluationDetails, hints: dict | ||
): | ||
current_span = trace.get_current_span() | ||
|
||
variant = details.variant | ||
if variant is None: | ||
if isinstance(details.value, str): | ||
variant = str(details.value) | ||
else: | ||
variant = json.dumps(details.value) | ||
|
||
event_attributes = { | ||
EventAttributes.FLAG_KEY: details.flag_key, | ||
EventAttributes.FLAG_VARIANT: variant, | ||
} | ||
|
||
if hook_context.provider_metadata: | ||
event_attributes[ | ||
EventAttributes.PROVIDER_NAME | ||
] = hook_context.provider_metadata.name | ||
|
||
current_span.add_event(OTEL_EVENT_NAME, event_attributes) | ||
|
||
def error(self, hook_context: HookContext, exception: Exception, hints: dict): | ||
current_span = trace.get_current_span() | ||
current_span.record_exception(exception) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ flake8 | |
pytest-mock | ||
coverage | ||
openfeature-sdk | ||
opentelemetry-api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from open_feature.hooks.hook_context import HookContext | ||
from open_feature.flag_evaluation.flag_type import FlagType | ||
from open_feature.evaluation_context.evaluation_context import EvaluationContext | ||
from open_feature.flag_evaluation.flag_evaluation_details import FlagEvaluationDetails | ||
from open_feature_contrib.hooks.otel import OTelTracesHook | ||
from opentelemetry import trace | ||
from opentelemetry.trace import Span | ||
|
||
|
||
@pytest.fixture | ||
def mock_get_current_span(monkeypatch): | ||
monkeypatch.setattr(trace, "get_current_span", Mock()) | ||
|
||
|
||
def test_before(mock_get_current_span): | ||
# Given | ||
hook = OTelTracesHook() | ||
hook_context = HookContext( | ||
flag_key="flag_key", | ||
flag_type=FlagType.BOOLEAN, | ||
default_value=False, | ||
evaluation_context=EvaluationContext(), | ||
) | ||
details = FlagEvaluationDetails( | ||
flag_key="flag_key", | ||
value=True, | ||
variant="enabled", | ||
reason=None, | ||
error_code=None, | ||
error_message=None, | ||
) | ||
|
||
mock_span = Mock(spec=Span) | ||
trace.get_current_span.return_value = mock_span | ||
|
||
# When | ||
hook.after(hook_context, details, hints={}) | ||
|
||
# Then | ||
mock_span.add_event.assert_called_once_with( | ||
"feature_flag", | ||
{ | ||
"feature_flag.key": "flag_key", | ||
"feature_flag.variant": "enabled", | ||
}, | ||
) | ||
|
||
|
||
def test_error(mock_get_current_span): | ||
# Given | ||
hook = OTelTracesHook() | ||
hook_context = HookContext( | ||
flag_key="flag_key", | ||
flag_type=FlagType.BOOLEAN, | ||
default_value=False, | ||
evaluation_context=EvaluationContext(), | ||
) | ||
exception = Exception() | ||
|
||
mock_span = Mock(spec=Span) | ||
trace.get_current_span.return_value = mock_span | ||
|
||
# When | ||
hook.error(hook_context, exception, hints={}) | ||
|
||
# Then | ||
mock_span.record_exception.assert_called_once_with(exception) |