Skip to content

Commit c7a7fc1

Browse files
committed
fix: move extra attribute retrieval to after hook
Signed-off-by: Danju Visvanathan <danju.visvanathan@gmail.com>
1 parent 3d016ee commit c7a7fc1

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

hooks/openfeature-hooks-opentelemetry/src/openfeature/contrib/hook/opentelemetry/metric.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def after(
5454
attributes[Attributes.OTEL_PROVIDER_NAME] = (
5555
hook_context.provider_metadata.name
5656
)
57+
attributes = attributes | attributes_from_dimensions(
58+
self.extra_attributes, details.flag_metadata
59+
)
5760
self.evaluation_success_total.add(1, attributes)
5861

5962
def error(
@@ -82,9 +85,6 @@ def finally_after(
8285
attributes[Attributes.OTEL_PROVIDER_NAME] = (
8386
hook_context.provider_metadata.name
8487
)
85-
attributes = attributes | attributes_from_dimensions(
86-
self.extra_attributes, details.flag_metadata
87-
)
8888
self.evaluation_active_count.add(-1, attributes)
8989

9090

hooks/openfeature-hooks-opentelemetry/tests/test_metric.py

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -95,33 +95,42 @@ def test_metric_after(mock_get_meter):
9595
mock_counters["feature_flag.evaluation.active_count"].add.assert_not_called()
9696

9797

98-
def test_metric_error(mock_get_meter):
98+
def test_metric_after_with_extra_dimensions(mock_get_meter):
9999
_, mock_counters = mock_get_meter
100-
hook = MetricsHook()
100+
hook = MetricsHook(extra_attributes=["scope", "test"])
101101
hook_context = HookContext(
102102
flag_key="flag_key",
103103
flag_type=FlagType.BOOLEAN,
104104
default_value=False,
105105
evaluation_context=EvaluationContext(),
106106
provider_metadata=Metadata(name="test-provider"),
107107
)
108-
hook.error(hook_context, Exception("test error"), hints={})
109-
mock_counters["feature_flag.evaluation.error_total"].add.assert_called_once_with(
108+
details = FlagEvaluationDetails(
109+
flag_key="flag_key",
110+
value=True,
111+
variant="enabled",
112+
reason=Reason.TARGETING_MATCH,
113+
flag_metadata={"scope": "application", "test": True},
114+
error_code=None,
115+
error_message=None,
116+
)
117+
hook.after(hook_context, details, hints={})
118+
mock_counters["feature_flag.evaluation.success_total"].add.assert_called_once_with(
110119
1,
111120
{
112121
"feature_flag.key": "flag_key",
122+
"feature_flag.result.reason": "targeting_match",
123+
"feature_flag.result.variant": "enabled",
113124
"feature_flag.provider.name": "test-provider",
114-
"exception": "test error",
125+
"scope": "application",
126+
"test": True,
115127
},
116128
)
117-
mock_counters["feature_flag.evaluation.success_total"].add.assert_not_called()
118-
mock_counters["feature_flag.evaluation.request_total"].add.assert_not_called()
119-
mock_counters["feature_flag.evaluation.active_count"].add.assert_not_called()
120129

121130

122-
def test_metric_finally_after(mock_get_meter):
131+
def test_metric_after_with_extra_dimensions_missing_attribute(mock_get_meter):
123132
_, mock_counters = mock_get_meter
124-
hook = MetricsHook()
133+
hook = MetricsHook(extra_attributes=["scope", "test"])
125134
hook_context = HookContext(
126135
flag_key="flag_key",
127136
flag_type=FlagType.BOOLEAN,
@@ -134,54 +143,48 @@ def test_metric_finally_after(mock_get_meter):
134143
value=True,
135144
variant="enabled",
136145
reason=Reason.TARGETING_MATCH,
137-
error_code=None,
138-
error_message=None,
146+
flag_metadata={"test": True},
139147
)
140-
hook.finally_after(hook_context, details, hints={})
141-
mock_counters["feature_flag.evaluation.active_count"].add.assert_called_once_with(
142-
-1,
148+
hook.after(hook_context, details, hints={})
149+
mock_counters["feature_flag.evaluation.success_total"].add.assert_called_once_with(
150+
1,
143151
{
144152
"feature_flag.key": "flag_key",
153+
"feature_flag.result.reason": "targeting_match",
154+
"feature_flag.result.variant": "enabled",
145155
"feature_flag.provider.name": "test-provider",
156+
"test": True,
146157
},
147158
)
148-
mock_counters["feature_flag.evaluation.success_total"].add.assert_not_called()
149-
mock_counters["feature_flag.evaluation.request_total"].add.assert_not_called()
150-
mock_counters["feature_flag.evaluation.error_total"].add.assert_not_called()
151159

152160

153-
def test_metric_finally_after_with_extra_dimensions(mock_get_meter):
161+
def test_metric_error(mock_get_meter):
154162
_, mock_counters = mock_get_meter
155-
hook = MetricsHook(extra_attributes=["scope", "test"])
163+
hook = MetricsHook()
156164
hook_context = HookContext(
157165
flag_key="flag_key",
158166
flag_type=FlagType.BOOLEAN,
159167
default_value=False,
160168
evaluation_context=EvaluationContext(),
161169
provider_metadata=Metadata(name="test-provider"),
162170
)
163-
details = FlagEvaluationDetails(
164-
flag_key="flag_key",
165-
value=True,
166-
variant="enabled",
167-
reason=Reason.TARGETING_MATCH,
168-
flag_metadata={"scope": "application", "test": True},
169-
)
170-
hook.finally_after(hook_context, details, hints={})
171-
mock_counters["feature_flag.evaluation.active_count"].add.assert_called_once_with(
172-
-1,
171+
hook.error(hook_context, Exception("test error"), hints={})
172+
mock_counters["feature_flag.evaluation.error_total"].add.assert_called_once_with(
173+
1,
173174
{
174175
"feature_flag.key": "flag_key",
175176
"feature_flag.provider.name": "test-provider",
176-
"scope": "application",
177-
"test": True,
177+
"exception": "test error",
178178
},
179179
)
180+
mock_counters["feature_flag.evaluation.success_total"].add.assert_not_called()
181+
mock_counters["feature_flag.evaluation.request_total"].add.assert_not_called()
182+
mock_counters["feature_flag.evaluation.active_count"].add.assert_not_called()
180183

181184

182-
def test_metric_finally_after_with_extra_dimensions_missing_attribute(mock_get_meter):
185+
def test_metric_finally_after(mock_get_meter):
183186
_, mock_counters = mock_get_meter
184-
hook = MetricsHook(extra_attributes=["scope", "test"])
187+
hook = MetricsHook()
185188
hook_context = HookContext(
186189
flag_key="flag_key",
187190
flag_type=FlagType.BOOLEAN,
@@ -194,14 +197,17 @@ def test_metric_finally_after_with_extra_dimensions_missing_attribute(mock_get_m
194197
value=True,
195198
variant="enabled",
196199
reason=Reason.TARGETING_MATCH,
197-
flag_metadata={"test": True},
200+
error_code=None,
201+
error_message=None,
198202
)
199203
hook.finally_after(hook_context, details, hints={})
200204
mock_counters["feature_flag.evaluation.active_count"].add.assert_called_once_with(
201205
-1,
202206
{
203207
"feature_flag.key": "flag_key",
204208
"feature_flag.provider.name": "test-provider",
205-
"test": True,
206209
},
207210
)
211+
mock_counters["feature_flag.evaluation.success_total"].add.assert_not_called()
212+
mock_counters["feature_flag.evaluation.request_total"].add.assert_not_called()
213+
mock_counters["feature_flag.evaluation.error_total"].add.assert_not_called()

0 commit comments

Comments
 (0)