Add url.scheme to HTTP client metrics (#9642)

This commit is contained in:
Mateusz Rzeszutek 2023-10-10 16:57:10 +02:00 committed by GitHub
parent 6038a872c5
commit 62504d28e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 8 deletions

View File

@ -7,6 +7,7 @@ package io.opentelemetry.instrumentation.api.instrumenter.http;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMessageBodySizeUtil.getHttpRequestBodySize; 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.HttpMessageBodySizeUtil.getHttpResponseBodySize;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.mergeClientAttributes;
import static java.util.logging.Level.FINE; import static java.util.logging.Level.FINE;
import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.Attributes;
@ -83,7 +84,7 @@ public final class HttpClientExperimentalMetrics implements OperationListener {
return; return;
} }
Attributes sizeAttributes = startAttributes.toBuilder().putAll(endAttributes).build(); Attributes sizeAttributes = mergeClientAttributes(startAttributes, endAttributes);
Long requestBodySize = getHttpRequestBodySize(endAttributes, startAttributes); Long requestBodySize = getHttpRequestBodySize(endAttributes, startAttributes);
if (requestBodySize != null) { if (requestBodySize != null) {

View File

@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.api.instrumenter.http; 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.createStableDurationHistogramBuilder;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.mergeClientAttributes;
import static java.util.logging.Level.FINE; import static java.util.logging.Level.FINE;
import com.google.auto.value.AutoValue; import com.google.auto.value.AutoValue;
@ -90,7 +91,7 @@ public final class HttpClientMetrics implements OperationListener {
return; return;
} }
Attributes attributes = state.startAttributes().toBuilder().putAll(endAttributes).build(); Attributes attributes = mergeClientAttributes(state.startAttributes(), endAttributes);
if (stableDuration != null) { if (stableDuration != null) {
stableDuration.record((endNanos - state.startTimeNanos()) / NANOS_PER_S, attributes, context); stableDuration.record((endNanos - state.startTimeNanos()) / NANOS_PER_S, attributes, context);

View File

@ -29,7 +29,8 @@ final class HttpMetricsAdvice {
SemanticAttributes.NETWORK_PROTOCOL_NAME, SemanticAttributes.NETWORK_PROTOCOL_NAME,
SemanticAttributes.NETWORK_PROTOCOL_VERSION, SemanticAttributes.NETWORK_PROTOCOL_VERSION,
SemanticAttributes.SERVER_ADDRESS, SemanticAttributes.SERVER_ADDRESS,
SemanticAttributes.SERVER_PORT)); SemanticAttributes.SERVER_PORT,
SemanticAttributes.URL_SCHEME));
} }
@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0 @SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
@ -64,6 +65,7 @@ final class HttpMetricsAdvice {
SemanticAttributes.NETWORK_PROTOCOL_VERSION, SemanticAttributes.NETWORK_PROTOCOL_VERSION,
SemanticAttributes.SERVER_ADDRESS, SemanticAttributes.SERVER_ADDRESS,
SemanticAttributes.SERVER_PORT, SemanticAttributes.SERVER_PORT,
SemanticAttributes.URL_SCHEME,
// old attributes // old attributes
SemanticAttributes.HTTP_METHOD, SemanticAttributes.HTTP_METHOD,
SemanticAttributes.HTTP_STATUS_CODE, SemanticAttributes.HTTP_STATUS_CODE,

View File

@ -8,9 +8,12 @@ package io.opentelemetry.instrumentation.api.instrumenter.http;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList; 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.DoubleHistogramBuilder;
import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleHistogramBuilder; import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleHistogramBuilder;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.List; import java.util.List;
final class HttpMetricsUtil { final class HttpMetricsUtil {
@ -31,5 +34,17 @@ final class HttpMetricsUtil {
return durationBuilder; 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() {} private HttpMetricsUtil() {}
} }

View File

@ -36,7 +36,6 @@ class HttpClientExperimentalMetricsStableSemconvTest {
Attributes.builder() Attributes.builder()
.put(SemanticAttributes.HTTP_REQUEST_METHOD, "GET") .put(SemanticAttributes.HTTP_REQUEST_METHOD, "GET")
.put(SemanticAttributes.URL_FULL, "https://localhost:1234/") .put(SemanticAttributes.URL_FULL, "https://localhost:1234/")
.put(SemanticAttributes.URL_SCHEME, "https")
.put(SemanticAttributes.URL_PATH, "/") .put(SemanticAttributes.URL_PATH, "/")
.put(SemanticAttributes.URL_QUERY, "q=a") .put(SemanticAttributes.URL_QUERY, "q=a")
.put(SemanticAttributes.SERVER_ADDRESS, "localhost") .put(SemanticAttributes.SERVER_ADDRESS, "localhost")
@ -97,7 +96,8 @@ class HttpClientExperimentalMetricsStableSemconvTest {
equalTo( equalTo(
SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"), SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"), equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, 1234)) equalTo(SemanticAttributes.SERVER_PORT, 1234),
equalTo(SemanticAttributes.URL_SCHEME, "https"))
.hasExemplarsSatisfying( .hasExemplarsSatisfying(
exemplar -> exemplar ->
exemplar exemplar
@ -123,7 +123,8 @@ class HttpClientExperimentalMetricsStableSemconvTest {
equalTo( equalTo(
SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"), SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"), equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, 1234)) equalTo(SemanticAttributes.SERVER_PORT, 1234),
equalTo(SemanticAttributes.URL_SCHEME, "https"))
.hasExemplarsSatisfying( .hasExemplarsSatisfying(
exemplar -> exemplar ->
exemplar exemplar

View File

@ -38,7 +38,6 @@ class HttpClientMetricsStableSemconvTest {
Attributes.builder() Attributes.builder()
.put(SemanticAttributes.HTTP_REQUEST_METHOD, "GET") .put(SemanticAttributes.HTTP_REQUEST_METHOD, "GET")
.put(SemanticAttributes.URL_FULL, "https://localhost:1234/") .put(SemanticAttributes.URL_FULL, "https://localhost:1234/")
.put(SemanticAttributes.URL_SCHEME, "https")
.put(SemanticAttributes.URL_PATH, "/") .put(SemanticAttributes.URL_PATH, "/")
.put(SemanticAttributes.URL_QUERY, "q=a") .put(SemanticAttributes.URL_QUERY, "q=a")
.put(SemanticAttributes.SERVER_ADDRESS, "localhost") .put(SemanticAttributes.SERVER_ADDRESS, "localhost")
@ -99,7 +98,8 @@ class HttpClientMetricsStableSemconvTest {
equalTo( equalTo(
SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"), SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"), equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, 1234)) equalTo(SemanticAttributes.SERVER_PORT, 1234),
equalTo(SemanticAttributes.URL_SCHEME, "https"))
.hasExemplarsSatisfying( .hasExemplarsSatisfying(
exemplar -> exemplar ->
exemplar exemplar