Add url.scheme to HTTP client metrics (#9642)
This commit is contained in:
parent
6038a872c5
commit
62504d28e8
|
@ -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.getHttpResponseBodySize;
|
||||
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.mergeClientAttributes;
|
||||
import static java.util.logging.Level.FINE;
|
||||
|
||||
import io.opentelemetry.api.common.Attributes;
|
||||
|
@ -83,7 +84,7 @@ public final class HttpClientExperimentalMetrics implements OperationListener {
|
|||
return;
|
||||
}
|
||||
|
||||
Attributes sizeAttributes = startAttributes.toBuilder().putAll(endAttributes).build();
|
||||
Attributes sizeAttributes = mergeClientAttributes(startAttributes, endAttributes);
|
||||
|
||||
Long requestBodySize = getHttpRequestBodySize(endAttributes, startAttributes);
|
||||
if (requestBodySize != null) {
|
||||
|
|
|
@ -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;
|
||||
|
@ -90,7 +91,7 @@ public final class HttpClientMetrics implements OperationListener {
|
|||
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);
|
||||
|
|
|
@ -29,7 +29,8 @@ final class HttpMetricsAdvice {
|
|||
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
|
||||
|
@ -64,6 +65,7 @@ final class HttpMetricsAdvice {
|
|||
SemanticAttributes.NETWORK_PROTOCOL_VERSION,
|
||||
SemanticAttributes.SERVER_ADDRESS,
|
||||
SemanticAttributes.SERVER_PORT,
|
||||
SemanticAttributes.URL_SCHEME,
|
||||
// old attributes
|
||||
SemanticAttributes.HTTP_METHOD,
|
||||
SemanticAttributes.HTTP_STATUS_CODE,
|
||||
|
|
|
@ -8,9 +8,12 @@ package io.opentelemetry.instrumentation.api.instrumenter.http;
|
|||
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 {
|
||||
|
@ -31,5 +34,17 @@ final class HttpMetricsUtil {
|
|||
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() {}
|
||||
}
|
||||
|
|
|
@ -36,7 +36,6 @@ class HttpClientExperimentalMetricsStableSemconvTest {
|
|||
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")
|
||||
|
@ -97,7 +96,8 @@ class HttpClientExperimentalMetricsStableSemconvTest {
|
|||
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
|
||||
|
@ -123,7 +123,8 @@ class HttpClientExperimentalMetricsStableSemconvTest {
|
|||
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
|
||||
|
|
|
@ -38,7 +38,6 @@ class HttpClientMetricsStableSemconvTest {
|
|||
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")
|
||||
|
@ -99,7 +98,8 @@ class HttpClientMetricsStableSemconvTest {
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue