Skip to content

Commit 9018dc2

Browse files
committed
fix(flask): use try/finally for active_requests decrement per review
Signed-off-by: alliasgher <alliasgher123@gmail.com>
1 parent e9fed7d commit 9018dc2

1 file changed

Lines changed: 38 additions & 40 deletions

File tree

  • instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask

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

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -417,50 +417,48 @@ def _start_response(status, response_headers, *args, **kwargs):
417417

418418
try:
419419
result = wsgi_app(wrapped_app_environ, _start_response)
420-
except Exception:
421-
active_requests_counter.add(-1, active_requests_count_attrs)
422-
raise
423-
424-
# Note: Streaming response context cleanup is now handled in the Flask teardown function
425-
# (_wrapped_teardown_request) to ensure proper cleanup following Logfire's recommendations
426-
# for OpenTelemetry generator context management
427-
428-
if should_trace:
429-
duration_s = default_timer() - start
430-
# Get the span from wrapped_app_environ and re-create context manually
431-
# to pass to histogram for exemplars generation
432-
span = wrapped_app_environ.get(_ENVIRON_SPAN_KEY)
433-
metrics_context = trace.set_span_in_context(span)
434-
435-
if duration_histogram_old:
436-
duration_attrs_old = otel_wsgi._parse_duration_attrs(
437-
attributes, _StabilityMode.DEFAULT
438-
)
439420

440-
if request_route:
441-
# http.target to be included in old semantic conventions
442-
duration_attrs_old[HTTP_TARGET] = str(request_route)
443-
duration_histogram_old.record(
444-
max(round(duration_s * 1000), 0),
445-
duration_attrs_old,
446-
context=metrics_context,
447-
)
448-
if duration_histogram_new:
449-
duration_attrs_new = otel_wsgi._parse_duration_attrs(
450-
attributes, _StabilityMode.HTTP
451-
)
421+
# Note: Streaming response context cleanup is now handled in the Flask teardown function
422+
# (_wrapped_teardown_request) to ensure proper cleanup following Logfire's recommendations
423+
# for OpenTelemetry generator context management
452424

453-
if request_route:
454-
duration_attrs_new[HTTP_ROUTE] = str(request_route)
425+
if should_trace:
426+
duration_s = default_timer() - start
427+
# Get the span from wrapped_app_environ and re-create context manually
428+
# to pass to histogram for exemplars generation
429+
span = wrapped_app_environ.get(_ENVIRON_SPAN_KEY)
430+
metrics_context = trace.set_span_in_context(span)
431+
432+
if duration_histogram_old:
433+
duration_attrs_old = otel_wsgi._parse_duration_attrs(
434+
attributes, _StabilityMode.DEFAULT
435+
)
455436

456-
duration_histogram_new.record(
457-
max(duration_s, 0),
458-
duration_attrs_new,
459-
context=metrics_context,
460-
)
437+
if request_route:
438+
# http.target to be included in old semantic conventions
439+
duration_attrs_old[HTTP_TARGET] = str(request_route)
440+
duration_histogram_old.record(
441+
max(round(duration_s * 1000), 0),
442+
duration_attrs_old,
443+
context=metrics_context,
444+
)
445+
if duration_histogram_new:
446+
duration_attrs_new = otel_wsgi._parse_duration_attrs(
447+
attributes, _StabilityMode.HTTP
448+
)
449+
450+
if request_route:
451+
duration_attrs_new[HTTP_ROUTE] = str(request_route)
461452

462-
active_requests_counter.add(-1, active_requests_count_attrs)
463-
return result
453+
duration_histogram_new.record(
454+
max(duration_s, 0),
455+
duration_attrs_new,
456+
context=metrics_context,
457+
)
458+
459+
return result
460+
finally:
461+
active_requests_counter.add(-1, active_requests_count_attrs)
464462

465463
def _should_trace(excluded_urls) -> bool:
466464
return bool(

0 commit comments

Comments
 (0)