@@ -417,7 +417,6 @@ def _apply_request_client_attributes_to_span(
417417
418418def _apply_response_client_attributes_to_span (
419419 span : Span ,
420- metric_attributes : dict [str , typing .Any ],
421420 status_code : int ,
422421 http_version : str ,
423422 semconv : _StabilityMode ,
@@ -433,6 +432,30 @@ def _apply_response_client_attributes_to_span(
433432 http_status_code = http_status_to_status_code (status_code )
434433 span .set_status (http_status_code )
435434
435+ if http_status_code == StatusCode .ERROR and _report_new (semconv ):
436+ # http semconv transition: new error.type
437+ span_attributes [ERROR_TYPE ] = str (status_code )
438+
439+ if http_version and _report_new (semconv ):
440+ # http semconv transition: http.flavor -> network.protocol.version
441+ _set_http_network_protocol_version (
442+ span_attributes ,
443+ http_version .replace ("HTTP/" , "" ),
444+ semconv ,
445+ )
446+
447+ for key , val in span_attributes .items ():
448+ span .set_attribute (key , val )
449+
450+
451+ def _apply_response_client_attributes_to_metrics (
452+ span : Span | None ,
453+ metric_attributes : dict [str , typing .Any ],
454+ status_code : int ,
455+ http_version : str ,
456+ semconv : _StabilityMode ,
457+ ) -> None :
458+ """Apply response attributes to metric attributes."""
436459 # Set HTTP status code in metric attributes
437460 _set_status (
438461 span ,
@@ -443,26 +466,12 @@ def _apply_response_client_attributes_to_span(
443466 sem_conv_opt_in_mode = semconv ,
444467 )
445468
446- if http_status_code == StatusCode .ERROR and _report_new (semconv ):
447- # http semconv transition: new error.type
448- span_attributes [ERROR_TYPE ] = str (status_code )
449-
450469 if http_version and _report_new (semconv ):
451- # http semconv transition: http.flavor -> network.protocol.version
452470 _set_http_network_protocol_version (
453471 metric_attributes ,
454472 http_version .replace ("HTTP/" , "" ),
455473 semconv ,
456474 )
457- if _report_new (semconv ):
458- _set_http_network_protocol_version (
459- span_attributes ,
460- http_version .replace ("HTTP/" , "" ),
461- semconv ,
462- )
463-
464- for key , val in span_attributes .items ():
465- span .set_attribute (key , val )
466475
467476
468477class SyncOpenTelemetryTransport (httpx .BaseTransport ):
@@ -592,11 +601,19 @@ def handle_request(
592601 _extract_response (response )
593602 )
594603
604+ # Always apply response attributes to metrics
605+ _apply_response_client_attributes_to_metrics (
606+ span ,
607+ metric_attributes ,
608+ status_code ,
609+ http_version ,
610+ self ._sem_conv_opt_in_mode ,
611+ )
612+
595613 if span .is_recording ():
596614 # apply http client response attributes according to semconv
597615 _apply_response_client_attributes_to_span (
598616 span ,
599- metric_attributes ,
600617 status_code ,
601618 http_version ,
602619 self ._sem_conv_opt_in_mode ,
@@ -777,11 +794,19 @@ async def handle_async_request(
777794 _extract_response (response )
778795 )
779796
797+ # Always apply response attributes to metrics
798+ _apply_response_client_attributes_to_metrics (
799+ span ,
800+ metric_attributes ,
801+ status_code ,
802+ http_version ,
803+ self ._sem_conv_opt_in_mode ,
804+ )
805+
780806 if span .is_recording ():
781807 # apply http client response attributes according to semconv
782808 _apply_response_client_attributes_to_span (
783809 span ,
784- metric_attributes ,
785810 status_code ,
786811 http_version ,
787812 self ._sem_conv_opt_in_mode ,
@@ -1001,11 +1026,19 @@ def _handle_request_wrapper( # pylint: disable=too-many-locals
10011026 _extract_response (response )
10021027 )
10031028
1029+ # Always apply response attributes to metrics
1030+ _apply_response_client_attributes_to_metrics (
1031+ span ,
1032+ metric_attributes ,
1033+ status_code ,
1034+ http_version ,
1035+ sem_conv_opt_in_mode ,
1036+ )
1037+
10041038 if span .is_recording ():
10051039 # apply http client response attributes according to semconv
10061040 _apply_response_client_attributes_to_span (
10071041 span ,
1008- metric_attributes ,
10091042 status_code ,
10101043 http_version ,
10111044 sem_conv_opt_in_mode ,
@@ -1109,11 +1142,19 @@ async def _handle_async_request_wrapper( # pylint: disable=too-many-locals
11091142 _extract_response (response )
11101143 )
11111144
1145+ # Always apply response attributes to metrics
1146+ _apply_response_client_attributes_to_metrics (
1147+ span ,
1148+ metric_attributes ,
1149+ status_code ,
1150+ http_version ,
1151+ sem_conv_opt_in_mode ,
1152+ )
1153+
11121154 if span .is_recording ():
11131155 # apply http client response attributes according to semconv
11141156 _apply_response_client_attributes_to_span (
11151157 span ,
1116- metric_attributes ,
11171158 status_code ,
11181159 http_version ,
11191160 sem_conv_opt_in_mode ,
0 commit comments