Skip to content

Commit

Permalink
Add url.scheme to HTTP client metrics (#9642)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Oct 10, 2023
1 parent 6038a87 commit 62504d2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMessageBodySizeUtil.getHttpRequestBodySize;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMessageBodySizeUtil.getHttpResponseBodySize;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.mergeClientAttributes;
import static java.util.logging.Level.FINE;

import io.opentelemetry.api.common.Attributes;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {
return;
}

Attributes sizeAttributes = startAttributes.toBuilder().putAll(endAttributes).build();
Attributes sizeAttributes = mergeClientAttributes(startAttributes, endAttributes);

Long requestBodySize = getHttpRequestBodySize(endAttributes, startAttributes);
if (requestBodySize != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.api.instrumenter.http;

import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.createStableDurationHistogramBuilder;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.mergeClientAttributes;
import static java.util.logging.Level.FINE;

import com.google.auto.value.AutoValue;
Expand Down Expand Up @@ -90,7 +91,7 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {
return;
}

Attributes attributes = state.startAttributes().toBuilder().putAll(endAttributes).build();
Attributes attributes = mergeClientAttributes(state.startAttributes(), endAttributes);

if (stableDuration != null) {
stableDuration.record((endNanos - state.startTimeNanos()) / NANOS_PER_S, attributes, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ static void applyStableClientDurationAdvice(DoubleHistogramBuilder builder) {
SemanticAttributes.NETWORK_PROTOCOL_NAME,
SemanticAttributes.NETWORK_PROTOCOL_VERSION,
SemanticAttributes.SERVER_ADDRESS,
SemanticAttributes.SERVER_PORT));
SemanticAttributes.SERVER_PORT,
SemanticAttributes.URL_SCHEME));
}

@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
Expand Down Expand Up @@ -64,6 +65,7 @@ static void applyClientRequestSizeAdvice(LongHistogramBuilder builder) {
SemanticAttributes.NETWORK_PROTOCOL_VERSION,
SemanticAttributes.SERVER_ADDRESS,
SemanticAttributes.SERVER_PORT,
SemanticAttributes.URL_SCHEME,
// old attributes
SemanticAttributes.HTTP_METHOD,
SemanticAttributes.HTTP_STATUS_CODE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleHistogramBuilder;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.List;

final class HttpMetricsUtil {
Expand All @@ -31,5 +34,17 @@ static DoubleHistogramBuilder createStableDurationHistogramBuilder(
return durationBuilder;
}

static Attributes mergeClientAttributes(Attributes startAttributes, Attributes endAttributes) {
AttributesBuilder builder = startAttributes.toBuilder().putAll(endAttributes);
String url = startAttributes.get(SemanticAttributes.URL_FULL);
if (url != null) {
int index = url.indexOf("://");
if (index > 0) {
builder.put(SemanticAttributes.URL_SCHEME, url.substring(0, index));
}
}
return builder.build();
}

private HttpMetricsUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ void collectsMetrics() {
Attributes.builder()
.put(SemanticAttributes.HTTP_REQUEST_METHOD, "GET")
.put(SemanticAttributes.URL_FULL, "https://localhost:1234/")
.put(SemanticAttributes.URL_SCHEME, "https")
.put(SemanticAttributes.URL_PATH, "/")
.put(SemanticAttributes.URL_QUERY, "q=a")
.put(SemanticAttributes.SERVER_ADDRESS, "localhost")
Expand Down Expand Up @@ -97,7 +96,8 @@ void collectsMetrics() {
equalTo(
SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, 1234))
equalTo(SemanticAttributes.SERVER_PORT, 1234),
equalTo(SemanticAttributes.URL_SCHEME, "https"))
.hasExemplarsSatisfying(
exemplar ->
exemplar
Expand All @@ -123,7 +123,8 @@ void collectsMetrics() {
equalTo(
SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, 1234))
equalTo(SemanticAttributes.SERVER_PORT, 1234),
equalTo(SemanticAttributes.URL_SCHEME, "https"))
.hasExemplarsSatisfying(
exemplar ->
exemplar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void collectsMetrics() {
Attributes.builder()
.put(SemanticAttributes.HTTP_REQUEST_METHOD, "GET")
.put(SemanticAttributes.URL_FULL, "https://localhost:1234/")
.put(SemanticAttributes.URL_SCHEME, "https")
.put(SemanticAttributes.URL_PATH, "/")
.put(SemanticAttributes.URL_QUERY, "q=a")
.put(SemanticAttributes.SERVER_ADDRESS, "localhost")
Expand Down Expand Up @@ -99,7 +98,8 @@ void collectsMetrics() {
equalTo(
SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, 1234))
equalTo(SemanticAttributes.SERVER_PORT, 1234),
equalTo(SemanticAttributes.URL_SCHEME, "https"))
.hasExemplarsSatisfying(
exemplar ->
exemplar
Expand Down

0 comments on commit 62504d2

Please sign in to comment.