@@ -101,15 +101,14 @@ def response_hook(span, request_obj, response):
101
101
_set_http_network_protocol_version ,
102
102
_set_http_peer_port_client ,
103
103
_set_http_scheme ,
104
- _set_http_status_code ,
105
104
_set_http_url ,
105
+ _set_status ,
106
106
_StabilityMode ,
107
107
)
108
108
from opentelemetry .instrumentation .instrumentor import BaseInstrumentor
109
109
from opentelemetry .instrumentation .requests .package import _instruments
110
110
from opentelemetry .instrumentation .requests .version import __version__
111
111
from opentelemetry .instrumentation .utils import (
112
- http_status_to_status_code ,
113
112
is_http_instrumentation_enabled ,
114
113
suppress_http_instrumentation ,
115
114
)
@@ -126,7 +125,6 @@ def response_hook(span, request_obj, response):
126
125
)
127
126
from opentelemetry .trace import SpanKind , Tracer , get_tracer
128
127
from opentelemetry .trace .span import Span
129
- from opentelemetry .trace .status import StatusCode
130
128
from opentelemetry .util .http import (
131
129
ExcludeList ,
132
130
get_excluded_urls ,
@@ -142,6 +140,32 @@ def response_hook(span, request_obj, response):
142
140
_ResponseHookT = Optional [Callable [[Span , PreparedRequest , Response ], None ]]
143
141
144
142
143
+ def _set_http_status_code_attribute (
144
+ span ,
145
+ status_code ,
146
+ metric_attributes = None ,
147
+ sem_conv_opt_in_mode = _StabilityMode .DEFAULT ,
148
+ ):
149
+ status_code_str = str (status_code )
150
+ try :
151
+ status_code = int (status_code )
152
+ except ValueError :
153
+ status_code = - 1
154
+ if metric_attributes is None :
155
+ metric_attributes = {}
156
+ # When we have durations we should set metrics only once
157
+ # Also the decision to include status code on a histogram should
158
+ # not be dependent on tracing decisions.
159
+ _set_status (
160
+ span ,
161
+ metric_attributes ,
162
+ status_code ,
163
+ status_code_str ,
164
+ server_span = False ,
165
+ sem_conv_opt_in_mode = sem_conv_opt_in_mode ,
166
+ )
167
+
168
+
145
169
# pylint: disable=unused-argument
146
170
# pylint: disable=R0915
147
171
def _instrument (
@@ -269,25 +293,12 @@ def get_or_create_headers():
269
293
270
294
if isinstance (result , Response ):
271
295
span_attributes = {}
272
- if span .is_recording ():
273
- _set_http_status_code (
274
- span_attributes ,
275
- result .status_code ,
276
- sem_conv_opt_in_mode ,
277
- )
278
- _set_http_status_code (
279
- metric_labels , result .status_code , sem_conv_opt_in_mode
280
- )
281
- status_code = http_status_to_status_code (
282
- result .status_code
283
- )
284
- span .set_status (status_code )
285
- if (
286
- _report_new (sem_conv_opt_in_mode )
287
- and status_code is StatusCode .ERROR
288
- ):
289
- span_attributes [ERROR_TYPE ] = str (result .status_code )
290
- metric_labels [ERROR_TYPE ] = str (result .status_code )
296
+ _set_http_status_code_attribute (
297
+ span ,
298
+ result .status_code ,
299
+ metric_labels ,
300
+ sem_conv_opt_in_mode ,
301
+ )
291
302
292
303
if result .raw is not None :
293
304
version = getattr (result .raw , "version" , None )
0 commit comments