Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class EventAttributes:


class TracingHook(Hook):
def after(
def __init__(self, exclude_exceptions: bool = False):
self.exclude_exceptions = exclude_exceptions

def finally_after(
self,
hook_context: HookContext,
details: FlagEvaluationDetails,
Expand Down Expand Up @@ -60,5 +63,15 @@ def after(
def error(
self, hook_context: HookContext, exception: Exception, hints: HookHints
) -> None:
if self.exclude_exceptions:
return
attributes = {
EventAttributes.KEY: hook_context.flag_key,
EventAttributes.RESULT_VALUE: json.dumps(hook_context.default_value),
}
if hook_context.provider_metadata:
attributes[EventAttributes.PROVIDER_NAME] = (
hook_context.provider_metadata.name
)
current_span = trace.get_current_span()
current_span.record_exception(exception)
current_span.record_exception(exception, attributes)
58 changes: 54 additions & 4 deletions hooks/openfeature-hooks-opentelemetry/tests/test_otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def mock_get_current_span(monkeypatch):
monkeypatch.setattr(trace, "get_current_span", Mock())


def test_after(mock_get_current_span):
def test_finally_after(mock_get_current_span):
# Given
hook = TracingHook()
hook_context = HookContext(
Expand All @@ -40,7 +40,7 @@ def test_after(mock_get_current_span):
trace.get_current_span.return_value = mock_span

# When
hook.after(hook_context, details, hints={})
hook.finally_after(hook_context, details, hints={})

# Then
mock_span.add_event.assert_called_once_with(
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_after_evaluation_error(mock_get_current_span):
trace.get_current_span.return_value = mock_span

# When
hook.after(hook_context, details, hints={})
hook.finally_after(hook_context, details, hints={})

# Then
mock_span.add_event.assert_called_once_with(
Expand All @@ -97,6 +97,33 @@ def test_after_evaluation_error(mock_get_current_span):
def test_error(mock_get_current_span):
# Given
hook = TracingHook()
hook_context = HookContext(
flag_key="flag_key",
flag_type=FlagType.BOOLEAN,
default_value=False,
evaluation_context=EvaluationContext(),
provider_metadata=Metadata(name="test-provider"),
)
exception = Exception()
attributes = {
"feature_flag.key": "flag_key",
"feature_flag.result.value": "false",
"feature_flag.provider.name": "test-provider",
}

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, attributes)


def test_error_exclude_exceptions(mock_get_current_span):
# Given
hook = TracingHook(exclude_exceptions=True)
hook_context = HookContext(
flag_key="flag_key",
flag_type=FlagType.BOOLEAN,
Expand All @@ -110,6 +137,29 @@ def test_error(mock_get_current_span):

# When
hook.error(hook_context, exception, hints={})
# Then
mock_span.record_exception.assert_not_called()


def test_error_no_provider_metadata(mock_get_current_span):
# Given
hook = TracingHook()
hook_context = HookContext(
flag_key="flag_key",
flag_type=FlagType.BOOLEAN,
default_value=False,
evaluation_context=EvaluationContext(),
)
exception = Exception()
attributes = {
"feature_flag.key": "flag_key",
"feature_flag.result.value": "false",
}

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)
mock_span.record_exception.assert_called_once_with(exception, attributes)