Skip to content

Commit 65ea232

Browse files
Refactor metrics_context assignment, update test
1 parent 2f8206c commit 65ea232

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ def response_hook(span: Span, status: str, response_headers: List):
290290
from opentelemetry.semconv.metrics.http_metrics import (
291291
HTTP_SERVER_REQUEST_DURATION,
292292
)
293-
from opentelemetry.trace.propagation import _SPAN_KEY
294293
from opentelemetry.util._importlib_metadata import version
295294
from opentelemetry.util.http import (
296295
get_excluded_urls,
@@ -412,6 +411,11 @@ def _start_response(status, response_headers, *args, **kwargs):
412411
result = wsgi_app(wrapped_app_environ, _start_response)
413412
if should_trace:
414413
duration_s = default_timer() - start
414+
# Get the span from wrapped_app_environ and re-create context manually
415+
# to pass to histogram for exemplars generation
416+
span = wrapped_app_environ.get(_ENVIRON_SPAN_KEY)
417+
metrics_context = trace.set_span_in_context(span)
418+
415419
if duration_histogram_old:
416420
duration_attrs_old = otel_wsgi._parse_duration_attrs(
417421
attributes, _StabilityMode.DEFAULT
@@ -420,13 +424,6 @@ def _start_response(status, response_headers, *args, **kwargs):
420424
if request_route:
421425
# http.target to be included in old semantic conventions
422426
duration_attrs_old[HTTP_TARGET] = str(request_route)
423-
424-
# Get the span from wrapped_app_environ and re-create context manually
425-
# to pass to histogram for exemplars generation
426-
span = wrapped_app_environ.get(_ENVIRON_SPAN_KEY)
427-
metrics_context = (
428-
context.set_value(_SPAN_KEY, span) if span else None
429-
)
430427
duration_histogram_old.record(
431428
max(round(duration_s * 1000), 0),
432429
duration_attrs_old,
@@ -440,12 +437,6 @@ def _start_response(status, response_headers, *args, **kwargs):
440437
if request_route:
441438
duration_attrs_new[HTTP_ROUTE] = str(request_route)
442439

443-
# Get the span from wrapped_app_environ and re-create context manually
444-
# to pass to histogram for exemplars generation
445-
span = wrapped_app_environ.get(_ENVIRON_SPAN_KEY)
446-
metrics_context = (
447-
context.set_value(_SPAN_KEY, span) if span else None
448-
)
449440
duration_histogram_new.record(
450441
max(duration_s, 0),
451442
duration_attrs_new,

instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
)
7070
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
7171
from opentelemetry.test.wsgitestutil import WsgiTestBase
72-
from opentelemetry.trace.propagation import _SPAN_KEY
7372
from opentelemetry.util.http import (
7473
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
7574
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
@@ -812,16 +811,15 @@ def test_flask_metrics_excluded_urls_new_semconv(self):
812811
self.assertFalse(histogram_data_point_seen)
813812

814813
def test_duration_histogram_old_record_with_context(self):
815-
with patch("opentelemetry.context.set_value") as mock_set_value:
814+
with patch("opentelemetry.trace.set_span_in_context") as mock_set_span:
816815
self.client.get("/hello/123")
817816

818-
# Verify that context.set_value was called for metrics exemplar context
817+
# Verify that trace.set_span_in_context was called for metrics exemplar context
819818
# with same trace, span ID as trace
820-
mock_set_value.assert_called()
821-
call_args = mock_set_value.call_args
822-
self.assertEqual(len(call_args[0]), 2)
823-
self.assertEqual(call_args[0][0], _SPAN_KEY)
824-
span_arg = call_args[0][1]
819+
mock_set_span.assert_called()
820+
call_args = mock_set_span.call_args
821+
self.assertEqual(len(call_args[0]), 1)
822+
span_arg = call_args[0][0]
825823
self.assertIsNotNone(span_arg)
826824
finished_spans = self.memory_exporter.get_finished_spans()
827825
self.assertEqual(len(finished_spans), 1)
@@ -834,16 +832,15 @@ def test_duration_histogram_old_record_with_context(self):
834832
)
835833

836834
def test_duration_histogram_new_record_with_context_new_semconv(self):
837-
with patch("opentelemetry.context.set_value") as mock_set_value:
835+
with patch("opentelemetry.trace.set_span_in_context") as mock_set_span:
838836
self.client.get("/hello/123")
839837

840-
# Verify that context.set_value was called for metrics exemplar context
838+
# Verify that trace.set_span_in_context was called for metrics exemplar context
841839
# with same trace, span ID as trace
842-
mock_set_value.assert_called()
843-
call_args = mock_set_value.call_args
844-
self.assertEqual(len(call_args[0]), 2)
845-
self.assertEqual(call_args[0][0], _SPAN_KEY)
846-
span_arg = call_args[0][1]
840+
mock_set_span.assert_called()
841+
call_args = mock_set_span.call_args
842+
self.assertEqual(len(call_args[0]), 1)
843+
span_arg = call_args[0][0]
847844
self.assertIsNotNone(span_arg)
848845
finished_spans = self.memory_exporter.get_finished_spans()
849846
self.assertEqual(len(finished_spans), 1)

0 commit comments

Comments
 (0)