94
94
_RequestHookT = Optional [Callable [[Span , PreparedRequest ], None ]]
95
95
_ResponseHookT = Optional [Callable [[Span , PreparedRequest ], None ]]
96
96
97
+ _METRIC_INSTRUMENTS_HTTP_CLIENT_REQUEST_DURATION = "http.client.request.duration"
98
+
97
99
98
100
# pylint: disable=unused-argument
99
101
# pylint: disable=R0915
100
102
def _instrument (
101
103
tracer : Tracer ,
102
- duration_histogram : Histogram ,
104
+ duration_histograms : list [ Histogram ] ,
103
105
request_hook : _RequestHookT = None ,
104
106
response_hook : _ResponseHookT = None ,
105
107
excluded_urls : ExcludeList = None ,
@@ -138,19 +140,22 @@ def get_or_create_headers():
138
140
return wrapped_send (self , request , ** kwargs )
139
141
140
142
# See
141
- # https://github.com/open-telemetry/opentelemetry-specification /blob/main/specification/trace/semantic_conventions/ http.md#http-client
143
+ # https://github.com/open-telemetry/semantic-conventions /blob/main/docs/http/ http-spans .md#http-client
142
144
method = request .method .upper ()
145
+ sanitized_method = sanitize_method (method )
143
146
span_name = get_default_span_name (method )
144
147
145
148
url = remove_url_credentials (request .url )
146
149
147
150
span_attributes = {
148
- SpanAttributes .HTTP_METHOD : method ,
151
+ # TODO: This was previously set to method.
152
+ SpanAttributes .HTTP_METHOD : sanitized_method ,
149
153
SpanAttributes .HTTP_URL : url ,
150
154
}
151
155
152
156
metric_labels = {
153
- SpanAttributes .HTTP_METHOD : method ,
157
+ # TODO: JEREVOSS: CONFIRM
158
+ SpanAttributes .HTTP_METHOD : sanitized_method ,
154
159
}
155
160
156
161
try :
@@ -170,6 +175,20 @@ def get_or_create_headers():
170
175
span_attributes ,
171
176
)
172
177
178
+ # See https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-spans.md#common-attributes
179
+ # Method is case sensitive. "http.request.method_original" should not be sanitized or autpmatically capitalized.
180
+ sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability ._get_opentelemetry_stability_opt_in_mode (
181
+ _OpenTelemetryStabilitySignalType .HTTP
182
+ )
183
+ if (
184
+ (
185
+ sem_conv_opt_in_mode is _OpenTelemetryStabilityMode .HTTP
186
+ or sem_conv_opt_in_mode is _OpenTelemetryStabilityMode .HTTP_DUP
187
+ )
188
+ and request .method != sanitized_method
189
+ ):
190
+ span_attributes [SpanAttributes .HTTP_REQUEST_METHOD_ORIGINAL ] = request .method
191
+
173
192
with tracer .start_as_current_span (
174
193
span_name , kind = SpanKind .CLIENT , attributes = span_attributes
175
194
) as span , set_ip_on_next_http_connection (span ):
@@ -238,7 +257,8 @@ def get_or_create_headers():
238
257
sem_conv_opt_in_mode ,
239
258
metric_labels ,
240
259
)
241
- duration_histogram .record (elapsed_time , attributes = metric_labels )
260
+ for duration_histogram in duration_histograms :
261
+ duration_histogram .record (elapsed_time , attributes = metric_labels )
242
262
243
263
if exception is not None :
244
264
raise exception .with_traceback (exception .__traceback__ )
@@ -313,17 +333,26 @@ def _instrument(self, **kwargs):
313
333
__version__ ,
314
334
meter_provider ,
315
335
)
316
- duration_histogram = meter .create_histogram (
317
- name = MetricInstruments .HTTP_CLIENT_DURATION ,
318
- unit = "s" ,
319
- description = "measures the duration of the outbound HTTP requests" ,
320
- )
321
336
sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability ._get_opentelemetry_stability_opt_in_mode (
322
337
_OpenTelemetryStabilitySignalType .HTTP
323
338
)
339
+ duration_histograms = []
340
+ if sem_conv_opt_in_mode is _OpenTelemetryStabilityMode .HTTP or sem_conv_opt_in_mode is _OpenTelemetryStabilityMode .HTTP_DUP :
341
+ duration_histograms .append (meter .create_histogram (
342
+ name = _METRIC_INSTRUMENTS_HTTP_CLIENT_REQUEST_DURATION ,
343
+ unit = "s" ,
344
+ description = "measures the duration of the outbound HTTP requests" ,
345
+ ))
346
+ if sem_conv_opt_in_mode is _OpenTelemetryStabilityMode .DEFAULT or sem_conv_opt_in_mode is _OpenTelemetryStabilityMode .HTTP_DUP :
347
+ duration_histograms .append (meter .create_histogram (
348
+ name = MetricInstruments .HTTP_CLIENT_DURATION ,
349
+ unit = "ms" ,
350
+ description = "measures the duration of the outbound HTTP request" ,
351
+ ))
352
+
324
353
_instrument (
325
354
tracer ,
326
- duration_histogram ,
355
+ duration_histograms ,
327
356
request_hook = kwargs .get ("request_hook" ),
328
357
response_hook = kwargs .get ("response_hook" ),
329
358
excluded_urls = _excluded_urls_from_env
0 commit comments