Skip to content

Commit 03a895d

Browse files
committed
method_original and duplicate histograms
1 parent 2578e2e commit 03a895d

File tree

1 file changed

+40
-11
lines changed
  • instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests

1 file changed

+40
-11
lines changed

instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@
9494
_RequestHookT = Optional[Callable[[Span, PreparedRequest], None]]
9595
_ResponseHookT = Optional[Callable[[Span, PreparedRequest], None]]
9696

97+
_METRIC_INSTRUMENTS_HTTP_CLIENT_REQUEST_DURATION = "http.client.request.duration"
98+
9799

98100
# pylint: disable=unused-argument
99101
# pylint: disable=R0915
100102
def _instrument(
101103
tracer: Tracer,
102-
duration_histogram: Histogram,
104+
duration_histograms: list[Histogram],
103105
request_hook: _RequestHookT = None,
104106
response_hook: _ResponseHookT = None,
105107
excluded_urls: ExcludeList = None,
@@ -138,19 +140,22 @@ def get_or_create_headers():
138140
return wrapped_send(self, request, **kwargs)
139141

140142
# 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
142144
method = request.method.upper()
145+
sanitized_method = sanitize_method(method)
143146
span_name = get_default_span_name(method)
144147

145148
url = remove_url_credentials(request.url)
146149

147150
span_attributes = {
148-
SpanAttributes.HTTP_METHOD: method,
151+
# TODO: This was previously set to method.
152+
SpanAttributes.HTTP_METHOD: sanitized_method,
149153
SpanAttributes.HTTP_URL: url,
150154
}
151155

152156
metric_labels = {
153-
SpanAttributes.HTTP_METHOD: method,
157+
# TODO: JEREVOSS: CONFIRM
158+
SpanAttributes.HTTP_METHOD: sanitized_method,
154159
}
155160

156161
try:
@@ -170,6 +175,20 @@ def get_or_create_headers():
170175
span_attributes,
171176
)
172177

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+
173192
with tracer.start_as_current_span(
174193
span_name, kind=SpanKind.CLIENT, attributes=span_attributes
175194
) as span, set_ip_on_next_http_connection(span):
@@ -238,7 +257,8 @@ def get_or_create_headers():
238257
sem_conv_opt_in_mode,
239258
metric_labels,
240259
)
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)
242262

243263
if exception is not None:
244264
raise exception.with_traceback(exception.__traceback__)
@@ -313,17 +333,26 @@ def _instrument(self, **kwargs):
313333
__version__,
314334
meter_provider,
315335
)
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-
)
321336
sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
322337
_OpenTelemetryStabilitySignalType.HTTP
323338
)
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+
324353
_instrument(
325354
tracer,
326-
duration_histogram,
355+
duration_histograms,
327356
request_hook=kwargs.get("request_hook"),
328357
response_hook=kwargs.get("response_hook"),
329358
excluded_urls=_excluded_urls_from_env

0 commit comments

Comments
 (0)