Skip to content

Commit

Permalink
Make url.scheme opt in for HTTP client metrics and make `server.por…
Browse files Browse the repository at this point in the history
…t` required (#9784)
  • Loading branch information
Mateusz Rzeszutek authored Nov 1, 2023
1 parent 182ee7e commit bd6d6ee
Show file tree
Hide file tree
Showing 21 changed files with 21 additions and 271 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ InternalNetworkAttributesExtractor<REQUEST, RESPONSE> buildNetworkExtractor() {

InternalServerAttributesExtractor<REQUEST> buildServerExtractor() {
return new InternalServerAttributesExtractor<>(
new ClientSideServerPortCondition<>(httpAttributesGetter),
serverAddressAndPortExtractor,
SemconvStability.emitStableHttpSemconv(),
SemconvStability.emitOldHttpSemconv(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

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 @@ -84,7 +83,7 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {
return;
}

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

Long requestBodySize = getHttpRequestBodySize(endAttributes, startAttributes);
if (requestBodySize != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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 @@ -91,7 +90,7 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {
return;
}

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

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 @@ -31,8 +31,7 @@ static void applyStableClientDurationAdvice(DoubleHistogramBuilder builder) {
SemanticAttributes.NETWORK_PROTOCOL_NAME,
SemanticAttributes.NETWORK_PROTOCOL_VERSION,
SemanticAttributes.SERVER_ADDRESS,
SemanticAttributes.SERVER_PORT,
SemanticAttributes.URL_SCHEME));
SemanticAttributes.SERVER_PORT));
}

@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
Expand Down Expand Up @@ -68,7 +67,6 @@ 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,12 +8,9 @@
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 @@ -34,17 +31,5 @@ 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 @@ -160,7 +160,6 @@ InternalNetworkAttributesExtractor<REQUEST, RESPONSE> buildNetworkExtractor() {

InternalServerAttributesExtractor<REQUEST> buildServerExtractor() {
return new InternalServerAttributesExtractor<>(
new ServerSideServerPortCondition<>(httpAttributesGetter),
serverAddressPortExtractor,
SemconvStability.emitStableHttpSemconv(),
SemconvStability.emitOldHttpSemconv(),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private NetClientAttributesExtractor(NetClientAttributesGetter<REQUEST, RESPONSE
SemconvStability.emitOldHttpSemconv());
internalServerExtractor =
new InternalServerAttributesExtractor<>(
(port, request) -> true,
serverAddressAndPortExtractor,
SemconvStability.emitStableHttpSemconv(),
SemconvStability.emitOldHttpSemconv(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ private NetServerAttributesExtractor(NetServerAttributesGetter<REQUEST, RESPONSE
SemconvStability.emitOldHttpSemconv());
internalServerExtractor =
new InternalServerAttributesExtractor<>(
(port, request) -> true,
serverAddressAndPortExtractor,
SemconvStability.emitStableHttpSemconv(),
SemconvStability.emitOldHttpSemconv(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ ServerAttributesExtractor<REQUEST, RESPONSE> createForServerSide(
ServerAttributesExtractor(
ServerAttributesGetter<REQUEST, RESPONSE> getter,
InternalServerAttributesExtractor.Mode mode) {
// the ServerAttributesExtractor will always emit new semconv
internalExtractor =
new InternalServerAttributesExtractor<>(
(port, request) -> true,
new ServerAddressAndPortExtractor<>(getter, AddressAndPortExtractor.noop()),
SemconvStability.emitStableHttpSemconv(),
SemconvStability.emitOldHttpSemconv(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,23 @@
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.function.BiPredicate;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public final class InternalServerAttributesExtractor<REQUEST> {

private final BiPredicate<Integer, REQUEST> captureServerPortCondition;
private final AddressAndPortExtractor<REQUEST> addressAndPortExtractor;
private final boolean emitStableUrlAttributes;
private final boolean emitOldHttpAttributes;
private final Mode oldSemconvMode;

public InternalServerAttributesExtractor(
BiPredicate<Integer, REQUEST> captureServerPortCondition,
AddressAndPortExtractor<REQUEST> addressAndPortExtractor,
boolean emitStableUrlAttributes,
boolean emitOldHttpAttributes,
Mode oldSemconvMode) {
this.captureServerPortCondition = captureServerPortCondition;
this.addressAndPortExtractor = addressAndPortExtractor;
this.emitStableUrlAttributes = emitStableUrlAttributes;
this.emitOldHttpAttributes = emitOldHttpAttributes;
Expand All @@ -48,9 +44,7 @@ public void onStart(AttributesBuilder attributes, REQUEST request) {
internalSet(attributes, oldSemconvMode.address, serverAddressAndPort.address);
}

if (serverAddressAndPort.port != null
&& serverAddressAndPort.port > 0
&& captureServerPortCondition.test(serverAddressAndPort.port, request)) {
if (serverAddressAndPort.port != null && serverAddressAndPort.port > 0) {
if (emitStableUrlAttributes) {
internalSet(attributes, SemanticAttributes.SERVER_PORT, (long) serverAddressAndPort.port);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void normal() {
request.put("protocolName", "http");
request.put("protocolVersion", "1.1");
request.put("serverAddress", "github.com");
request.put("serverPort", "123");
request.put("serverPort", "80");

Map<String, String> response = new HashMap<>();
response.put("statusCode", "202");
Expand All @@ -148,7 +148,7 @@ void normal() {
AttributeKey.stringArrayKey("http.request.header.custom_request_header"),
asList("123", "456")),
entry(SemanticAttributes.NET_PEER_NAME, "github.com"),
entry(SemanticAttributes.NET_PEER_PORT, 123L),
entry(SemanticAttributes.NET_PEER_PORT, 80L),
entry(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, 2L));

AttributesBuilder endAttributes = Attributes.builder();
Expand Down Expand Up @@ -259,34 +259,6 @@ void extractNetHostAndPortFromNetAttributesGetter() {
entry(SemanticAttributes.NET_PEER_PORT, 777L));
}

@ParameterizedTest
@ArgumentsSource(DefaultPeerPortArgumentSource.class)
void defaultPeerPort(int peerPort, String url) {
Map<String, String> request = new HashMap<>();
request.put("urlFull", url);
request.put("serverPort", String.valueOf(peerPort));

AttributesExtractor<Map<String, String>, Map<String, String>> extractor =
HttpClientAttributesExtractor.create(new TestHttpClientAttributesGetter());

AttributesBuilder attributes = Attributes.builder();
extractor.onStart(attributes, Context.root(), request);

assertThat(attributes.build()).doesNotContainKey(SemanticAttributes.NET_PEER_PORT);
}

static class DefaultPeerPortArgumentSource implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
arguments(80, "http://github.com"),
arguments(80, "HTTP://GITHUB.COM"),
arguments(443, "https://github.com"),
arguments(443, "HTTPS://GITHUB.COM"));
}
}

@Test
void zeroResends() {
Map<String, String> request = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void normal() {
request.put("header.content-length", "10");
request.put("route", "/repositories/{id}");
request.put("header.user-agent", "okhttp 3.x");
request.put("header.host", "github.com");
request.put("header.host", "github.com:80");
request.put("header.forwarded", "for=1.1.1.1;proto=https");
request.put("header.custom-request-header", "123,456");
request.put("networkTransport", "tcp");
Expand All @@ -151,6 +151,7 @@ void normal() {
assertThat(startAttributes.build())
.containsOnly(
entry(SemanticAttributes.NET_HOST_NAME, "github.com"),
entry(SemanticAttributes.NET_HOST_PORT, 80L),
entry(SemanticAttributes.HTTP_METHOD, "POST"),
entry(SemanticAttributes.HTTP_SCHEME, "https"),
entry(SemanticAttributes.HTTP_TARGET, "/repositories/1?details=true"),
Expand Down Expand Up @@ -235,33 +236,6 @@ void extractNetHostAndPortFromHostHeader() {
entry(SemanticAttributes.NET_HOST_PORT, 777L));
}

@ParameterizedTest
@ArgumentsSource(DefaultHostPortArgumentSource.class)
void defaultHostPort(int hostPort, String scheme) {
Map<String, Object> request = new HashMap<>();
request.put("urlScheme", scheme);
request.put("serverPort", hostPort);

AttributesExtractor<Map<String, Object>, Map<String, Object>> extractor =
HttpServerAttributesExtractor.builder(new TestHttpServerAttributesGetter())
.setCapturedRequestHeaders(emptyList())
.setCapturedResponseHeaders(emptyList())
.build();

AttributesBuilder attributes = Attributes.builder();
extractor.onStart(attributes, Context.root(), request);

assertThat(attributes.build()).doesNotContainKey(SemanticAttributes.NET_HOST_PORT);
}

static class DefaultHostPortArgumentSource implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(arguments(80, "http"), arguments(443, "https"));
}
}

@ParameterizedTest
@ArgumentsSource(PathAndQueryArgumentSource.class)
void computeTargetFromPathAndQuery(String path, String query, String expectedTarget) {
Expand Down
Loading

0 comments on commit bd6d6ee

Please sign in to comment.