@@ -417,7 +417,6 @@ def _apply_request_client_attributes_to_span(
417
417
418
418
def _apply_response_client_attributes_to_span (
419
419
span : Span ,
420
- metric_attributes : dict [str , typing .Any ],
421
420
status_code : int ,
422
421
http_version : str ,
423
422
semconv : _StabilityMode ,
@@ -433,6 +432,30 @@ def _apply_response_client_attributes_to_span(
433
432
http_status_code = http_status_to_status_code (status_code )
434
433
span .set_status (http_status_code )
435
434
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."""
436
459
# Set HTTP status code in metric attributes
437
460
_set_status (
438
461
span ,
@@ -443,26 +466,12 @@ def _apply_response_client_attributes_to_span(
443
466
sem_conv_opt_in_mode = semconv ,
444
467
)
445
468
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
-
450
469
if http_version and _report_new (semconv ):
451
- # http semconv transition: http.flavor -> network.protocol.version
452
470
_set_http_network_protocol_version (
453
471
metric_attributes ,
454
472
http_version .replace ("HTTP/" , "" ),
455
473
semconv ,
456
474
)
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 )
466
475
467
476
468
477
class SyncOpenTelemetryTransport (httpx .BaseTransport ):
@@ -592,11 +601,19 @@ def handle_request(
592
601
_extract_response (response )
593
602
)
594
603
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
+
595
613
if span .is_recording ():
596
614
# apply http client response attributes according to semconv
597
615
_apply_response_client_attributes_to_span (
598
616
span ,
599
- metric_attributes ,
600
617
status_code ,
601
618
http_version ,
602
619
self ._sem_conv_opt_in_mode ,
@@ -777,11 +794,19 @@ async def handle_async_request(
777
794
_extract_response (response )
778
795
)
779
796
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
+
780
806
if span .is_recording ():
781
807
# apply http client response attributes according to semconv
782
808
_apply_response_client_attributes_to_span (
783
809
span ,
784
- metric_attributes ,
785
810
status_code ,
786
811
http_version ,
787
812
self ._sem_conv_opt_in_mode ,
@@ -1001,11 +1026,19 @@ def _handle_request_wrapper( # pylint: disable=too-many-locals
1001
1026
_extract_response (response )
1002
1027
)
1003
1028
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
+
1004
1038
if span .is_recording ():
1005
1039
# apply http client response attributes according to semconv
1006
1040
_apply_response_client_attributes_to_span (
1007
1041
span ,
1008
- metric_attributes ,
1009
1042
status_code ,
1010
1043
http_version ,
1011
1044
sem_conv_opt_in_mode ,
@@ -1109,11 +1142,19 @@ async def _handle_async_request_wrapper( # pylint: disable=too-many-locals
1109
1142
_extract_response (response )
1110
1143
)
1111
1144
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
+
1112
1154
if span .is_recording ():
1113
1155
# apply http client response attributes according to semconv
1114
1156
_apply_response_client_attributes_to_span (
1115
1157
span ,
1116
- metric_attributes ,
1117
1158
status_code ,
1118
1159
http_version ,
1119
1160
sem_conv_opt_in_mode ,
0 commit comments