@@ -216,6 +216,9 @@ def response_hook(span, req, resp):
216216from opentelemetry .metrics import get_meter
217217from opentelemetry .semconv .attributes .error_attributes import ERROR_TYPE
218218from opentelemetry .semconv .metrics import MetricInstruments
219+ from opentelemetry .semconv .metrics .http_metrics import (
220+ HTTP_SERVER_REQUEST_DURATION ,
221+ )
219222from opentelemetry .semconv .trace import SpanAttributes
220223from opentelemetry .trace .status import StatusCode
221224from opentelemetry .util .http import get_excluded_urls , get_traced_request_attrs
@@ -279,11 +282,22 @@ def __init__(self, *args, **kwargs):
279282 meter_provider ,
280283 schema_url = _get_schema_url (self ._sem_conv_opt_in_mode ),
281284 )
282- self .duration_histogram = self ._otel_meter .create_histogram (
283- name = MetricInstruments .HTTP_SERVER_DURATION ,
284- unit = "ms" ,
285- description = "Measures the duration of inbound HTTP requests." ,
286- )
285+
286+ self .duration_histogram_old = None
287+ if _report_old (self ._sem_conv_opt_in_mode ):
288+ self .duration_histogram_old = self ._otel_meter .create_histogram (
289+ name = MetricInstruments .HTTP_SERVER_DURATION ,
290+ unit = "ms" ,
291+ description = "Measures the duration of inbound HTTP requests." ,
292+ )
293+ self .duration_histogram_new = None
294+ if _report_new (self ._sem_conv_opt_in_mode ):
295+ self .duration_histogram_new = self ._otel_meter .create_histogram (
296+ name = HTTP_SERVER_REQUEST_DURATION ,
297+ description = "Duration of HTTP server requests." ,
298+ unit = "s" ,
299+ )
300+
287301 self .active_requests_counter = self ._otel_meter .create_up_down_counter (
288302 name = MetricInstruments .HTTP_SERVER_ACTIVE_REQUESTS ,
289303 unit = "requests" ,
@@ -358,11 +372,10 @@ def __call__(self, env, start_response):
358372 context_carrier = env ,
359373 context_getter = otel_wsgi .wsgi_getter ,
360374 )
361- attributes = otel_wsgi .collect_request_attributes (env )
375+ attributes = otel_wsgi .collect_request_attributes (env , self . _sem_conv_opt_in_mode )
362376 active_requests_count_attrs = (
363- otel_wsgi ._parse_active_request_count_attrs (attributes )
377+ otel_wsgi ._parse_active_request_count_attrs (attributes , self . _sem_conv_opt_in_mode )
364378 )
365- duration_attrs = otel_wsgi ._parse_duration_attrs (attributes )
366379 self .active_requests_counter .add (1 , active_requests_count_attrs )
367380
368381 if span .is_recording ():
@@ -394,12 +407,23 @@ def _start_response(status, response_headers, *args, **kwargs):
394407 exception = exc
395408 raise
396409 finally :
397- if span .is_recording ():
398- if _report_old (self ._sem_conv_opt_in_mode ):
410+ duration_s = default_timer () - start
411+ if self .duration_histogram_old :
412+ duration_attrs = otel_wsgi ._parse_duration_attrs (
413+ attributes , _HTTPStabilityMode .DEFAULT
414+ )
415+ if span .is_recording ():
399416 duration_attrs [SpanAttributes .HTTP_STATUS_CODE ] = (
400417 span .attributes .get (SpanAttributes .HTTP_STATUS_CODE )
401418 )
402- if _report_new (self ._sem_conv_opt_in_mode ):
419+ self .duration_histogram_old .record (
420+ max (round (duration_s * 1000 ), 0 ), duration_attrs
421+ )
422+ if self .duration_histogram_new :
423+ duration_attrs = otel_wsgi ._parse_duration_attrs (
424+ attributes , _HTTPStabilityMode .HTTP
425+ )
426+ if span .is_recording ():
403427 duration_attrs [
404428 SpanAttributes .HTTP_RESPONSE_STATUS_CODE
405429 ] = span .attributes .get (
@@ -409,9 +433,10 @@ def _start_response(status, response_headers, *args, **kwargs):
409433 duration_attrs [ERROR_TYPE ] = span .attributes .get (
410434 ERROR_TYPE
411435 )
436+ self .duration_histogram_new .record (
437+ max (duration_s , 0 ), duration_attrs
438+ )
412439
413- duration = max (round ((default_timer () - start ) * 1000 ), 0 )
414- self .duration_histogram .record (duration , duration_attrs )
415440 self .active_requests_counter .add (- 1 , active_requests_count_attrs )
416441 if exception is None :
417442 activation .__exit__ (None , None , None )
0 commit comments