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,13 @@ 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),
}
Comment on lines +68 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To better align with the OpenFeature specification, feature_flag.provider_name should be included in the attributes for an error event when available. This change will also require updating test_error to include provider_metadata in the HookContext and assert this new attribute.

Suggested change
attributes = {
EventAttributes.KEY: hook_context.flag_key,
EventAttributes.RESULT_VALUE: json.dumps(hook_context.default_value),
}
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
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danjuv I believe this is correct.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
59 changes: 55 additions & 4 deletions hooks/openfeature-hooks-opentelemetry/tests/test_otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
from openfeature.provider.metadata import Metadata




@pytest.fixture
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 +42,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 +81,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 @@ -102,8 +104,14 @@ def test_error(mock_get_current_span):
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
Expand All @@ -112,4 +120,47 @@ def test_error(mock_get_current_span):
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)

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