forked from open-feature/python-sdk-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_otel.py
70 lines (56 loc) · 1.74 KB
/
test_otel.py
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
64
65
66
67
68
69
70
from unittest.mock import Mock
import pytest
from opentelemetry import trace
from opentelemetry.trace import Span
from openfeature.contrib.hook.opentelemetry import TracingHook
from openfeature.evaluation_context import EvaluationContext
from openfeature.flag_evaluation import FlagEvaluationDetails, FlagType
from openfeature.hook import HookContext
@pytest.fixture
def mock_get_current_span(monkeypatch):
monkeypatch.setattr(trace, "get_current_span", Mock())
def test_before(mock_get_current_span):
# Given
hook = TracingHook()
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 = TracingHook()
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)