Skip to content

Commit ea25349

Browse files
Fix FlaskInstrumentor http.server.request.duration exemplars
1 parent 34db73e commit ea25349

File tree

1 file changed

+15
-3
lines changed
  • instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask

1 file changed

+15
-3
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ def response_hook(span: Span, status: str, response_headers: List):
278278
from opentelemetry.semconv.metrics.http_metrics import (
279279
HTTP_SERVER_REQUEST_DURATION,
280280
)
281+
from opentelemetry.trace.propagation import _SPAN_KEY
281282
from opentelemetry.util._importlib_metadata import version
282283
from opentelemetry.util.http import (
283284
get_excluded_urls,
@@ -418,9 +419,20 @@ def _start_response(status, response_headers, *args, **kwargs):
418419
if request_route:
419420
duration_attrs_new[HTTP_ROUTE] = str(request_route)
420421

421-
duration_histogram_new.record(
422-
max(duration_s, 0), duration_attrs_new
423-
)
422+
# Get the span from wrapped_app_environ and re-create context manually
423+
# to pass to histogram for exemplars generation
424+
span = wrapped_app_environ.get(_ENVIRON_SPAN_KEY)
425+
if span:
426+
exemplar_context = context.set_value(_SPAN_KEY, span)
427+
duration_histogram_new.record(
428+
max(duration_s, 0),
429+
duration_attrs_new,
430+
context=exemplar_context,
431+
)
432+
else:
433+
duration_histogram_new.record(
434+
max(duration_s, 0), duration_attrs_new
435+
)
424436
active_requests_counter.add(-1, active_requests_count_attrs)
425437
return result
426438

0 commit comments

Comments
 (0)