Skip to content

Commit

Permalink
method_original and duplicate histograms
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydvoss committed Oct 26, 2023
1 parent 2578e2e commit 03a895d
Showing 1 changed file with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@
_RequestHookT = Optional[Callable[[Span, PreparedRequest], None]]
_ResponseHookT = Optional[Callable[[Span, PreparedRequest], None]]

_METRIC_INSTRUMENTS_HTTP_CLIENT_REQUEST_DURATION = "http.client.request.duration"


# pylint: disable=unused-argument
# pylint: disable=R0915
def _instrument(
tracer: Tracer,
duration_histogram: Histogram,
duration_histograms: list[Histogram],
request_hook: _RequestHookT = None,
response_hook: _ResponseHookT = None,
excluded_urls: ExcludeList = None,
Expand Down Expand Up @@ -138,19 +140,22 @@ def get_or_create_headers():
return wrapped_send(self, request, **kwargs)

# See
# https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-client
# https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-spans.md#http-client
method = request.method.upper()
sanitized_method = sanitize_method(method)
span_name = get_default_span_name(method)

url = remove_url_credentials(request.url)

span_attributes = {
SpanAttributes.HTTP_METHOD: method,
# TODO: This was previously set to method.
SpanAttributes.HTTP_METHOD: sanitized_method,
SpanAttributes.HTTP_URL: url,
}

metric_labels = {
SpanAttributes.HTTP_METHOD: method,
# TODO: JEREVOSS: CONFIRM
SpanAttributes.HTTP_METHOD: sanitized_method,
}

try:
Expand All @@ -170,6 +175,20 @@ def get_or_create_headers():
span_attributes,
)

# See https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-spans.md#common-attributes
# Method is case sensitive. "http.request.method_original" should not be sanitized or autpmatically capitalized.
sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
_OpenTelemetryStabilitySignalType.HTTP
)
if (
(
sem_conv_opt_in_mode is _OpenTelemetryStabilityMode.HTTP
or sem_conv_opt_in_mode is _OpenTelemetryStabilityMode.HTTP_DUP
)
and request.method != sanitized_method
):
span_attributes[SpanAttributes.HTTP_REQUEST_METHOD_ORIGINAL] = request.method

with tracer.start_as_current_span(
span_name, kind=SpanKind.CLIENT, attributes=span_attributes
) as span, set_ip_on_next_http_connection(span):
Expand Down Expand Up @@ -238,7 +257,8 @@ def get_or_create_headers():
sem_conv_opt_in_mode,
metric_labels,
)
duration_histogram.record(elapsed_time, attributes=metric_labels)
for duration_histogram in duration_histograms:
duration_histogram.record(elapsed_time, attributes=metric_labels)

if exception is not None:
raise exception.with_traceback(exception.__traceback__)
Expand Down Expand Up @@ -313,17 +333,26 @@ def _instrument(self, **kwargs):
__version__,
meter_provider,
)
duration_histogram = meter.create_histogram(
name=MetricInstruments.HTTP_CLIENT_DURATION,
unit="s",
description="measures the duration of the outbound HTTP requests",
)
sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
_OpenTelemetryStabilitySignalType.HTTP
)
duration_histograms = []
if sem_conv_opt_in_mode is _OpenTelemetryStabilityMode.HTTP or sem_conv_opt_in_mode is _OpenTelemetryStabilityMode.HTTP_DUP:
duration_histograms.append(meter.create_histogram(
name=_METRIC_INSTRUMENTS_HTTP_CLIENT_REQUEST_DURATION,
unit="s",
description="measures the duration of the outbound HTTP requests",
))
if sem_conv_opt_in_mode is _OpenTelemetryStabilityMode.DEFAULT or sem_conv_opt_in_mode is _OpenTelemetryStabilityMode.HTTP_DUP:
duration_histograms.append(meter.create_histogram(
name=MetricInstruments.HTTP_CLIENT_DURATION,
unit="ms",
description="measures the duration of the outbound HTTP request",
))

_instrument(
tracer,
duration_histogram,
duration_histograms,
request_hook=kwargs.get("request_hook"),
response_hook=kwargs.get("response_hook"),
excluded_urls=_excluded_urls_from_env
Expand Down

0 comments on commit 03a895d

Please sign in to comment.