Skip to content

Commit

Permalink
Make network.transport and network.type opt-in (#9719)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek authored Oct 19, 2023
1 parent 79f2726 commit 0fc1a99
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ InternalNetClientAttributesExtractor<REQUEST, RESPONSE> buildNetExtractor() {
InternalNetworkAttributesExtractor<REQUEST, RESPONSE> buildNetworkExtractor() {
return new InternalNetworkAttributesExtractor<>(
netAttributesGetter,
HttpNetworkTransportFilter.INSTANCE,
AddressAndPortExtractor.noop(),
serverAddressAndPortExtractor,
/* captureNetworkTransportAndType= */ false,
/* captureLocalSocketAttributes= */ false,
/* captureOldPeerDomainAttribute= */ true,
SemconvStability.emitStableHttpSemconv(),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ InternalNetServerAttributesExtractor<REQUEST, RESPONSE> buildNetExtractor() {
InternalNetworkAttributesExtractor<REQUEST, RESPONSE> buildNetworkExtractor() {
return new InternalNetworkAttributesExtractor<>(
netAttributesGetter,
HttpNetworkTransportFilter.INSTANCE,
serverAddressPortExtractor,
clientAddressPortExtractor,
/* captureNetworkTransportAndType= */ false,
/* captureLocalSocketAttributes= */ false,
/* captureOldPeerDomainAttribute= */ false,
SemconvStability.emitStableHttpSemconv(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalNetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkTransportFilter;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ServerAddressAndPortExtractor;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -51,9 +50,9 @@ private NetClientAttributesExtractor(NetClientAttributesGetter<REQUEST, RESPONSE
internalNetworkExtractor =
new InternalNetworkAttributesExtractor<>(
getter,
NetworkTransportFilter.alwaysTrue(),
AddressAndPortExtractor.noop(),
serverAddressAndPortExtractor,
/* captureNetworkTransportAndType= */ true,
/* captureLocalSocketAttributes= */ false,
/* captureOldPeerDomainAttribute= */ true,
SemconvStability.emitStableHttpSemconv(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalNetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkTransportFilter;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ServerAddressAndPortExtractor;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -53,9 +52,9 @@ private NetServerAttributesExtractor(NetServerAttributesGetter<REQUEST, RESPONSE
internalNetworkExtractor =
new InternalNetworkAttributesExtractor<>(
getter,
NetworkTransportFilter.alwaysTrue(),
serverAddressAndPortExtractor,
clientAddressAndPortExtractor,
/* captureNetworkTransportAndType= */ true,
/* captureLocalSocketAttributes= */ true,
/* captureOldPeerDomainAttribute= */ false,
SemconvStability.emitStableHttpSemconv(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalNetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkTransportFilter;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import javax.annotation.Nullable;

Expand All @@ -37,9 +36,9 @@ public static <REQUEST, RESPONSE> NetworkAttributesExtractor<REQUEST, RESPONSE>
internalExtractor =
new InternalNetworkAttributesExtractor<>(
getter,
NetworkTransportFilter.alwaysTrue(),
AddressAndPortExtractor.noop(),
AddressAndPortExtractor.noop(),
/* captureNetworkTransportAndType= */ true,
/* captureLocalSocketAttributes= */ true,
// capture the old net.sock.peer.name attr for backwards compatibility
/* captureOldPeerDomainAttribute= */ true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@
public final class InternalNetworkAttributesExtractor<REQUEST, RESPONSE> {

private final NetworkAttributesGetter<REQUEST, RESPONSE> getter;
private final NetworkTransportFilter networkTransportFilter;
private final AddressAndPortExtractor<REQUEST> logicalLocalAddressAndPortExtractor;
private final AddressAndPortExtractor<REQUEST> logicalPeerAddressAndPortExtractor;
private final boolean captureNetworkTransportAndType;
private final boolean captureLocalSocketAttributes;
private final boolean captureOldPeerDomainAttribute;
private final boolean emitStableUrlAttributes;
private final boolean emitOldHttpAttributes;

public InternalNetworkAttributesExtractor(
NetworkAttributesGetter<REQUEST, RESPONSE> getter,
NetworkTransportFilter networkTransportFilter,
AddressAndPortExtractor<REQUEST> logicalLocalAddressAndPortExtractor,
AddressAndPortExtractor<REQUEST> logicalPeerAddressAndPortExtractor,
boolean captureNetworkTransportAndType,
boolean captureLocalSocketAttributes,
boolean captureOldPeerDomainAttribute,
boolean emitStableUrlAttributes,
boolean emitOldHttpAttributes) {
this.getter = getter;
this.networkTransportFilter = networkTransportFilter;
this.logicalLocalAddressAndPortExtractor = logicalLocalAddressAndPortExtractor;
this.logicalPeerAddressAndPortExtractor = logicalPeerAddressAndPortExtractor;
this.captureNetworkTransportAndType = captureNetworkTransportAndType;
this.captureLocalSocketAttributes = captureLocalSocketAttributes;
this.captureOldPeerDomainAttribute = captureOldPeerDomainAttribute;
this.emitStableUrlAttributes = emitStableUrlAttributes;
Expand All @@ -56,14 +56,13 @@ public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPO

if (emitStableUrlAttributes) {
String transport = lowercase(getter.getNetworkTransport(request, response));
if (networkTransportFilter.shouldAddNetworkTransport(
protocolName, protocolVersion, transport)) {
if (captureNetworkTransportAndType) {
internalSet(attributes, SemanticAttributes.NETWORK_TRANSPORT, transport);
internalSet(
attributes,
SemanticAttributes.NETWORK_TYPE,
lowercase(getter.getNetworkType(request, response)));
}
internalSet(
attributes,
SemanticAttributes.NETWORK_TYPE,
lowercase(getter.getNetworkType(request, response)));
internalSet(attributes, SemanticAttributes.NETWORK_PROTOCOL_NAME, protocolName);
internalSet(attributes, SemanticAttributes.NETWORK_PROTOCOL_VERSION, protocolVersion);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ void normal() {
asList("654", "321")),
entry(SemanticAttributes.NET_PROTOCOL_NAME, "http"),
entry(SemanticAttributes.NET_PROTOCOL_VERSION, "1.1"),
entry(SemanticAttributes.NETWORK_TRANSPORT, "udp"),
entry(SemanticAttributes.NETWORK_TYPE, "ipv4"),
entry(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"),
entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "1.1"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ void normal() {
.containsOnly(
entry(SemanticAttributes.NET_PROTOCOL_NAME, "http"),
entry(SemanticAttributes.NET_PROTOCOL_VERSION, "2.0"),
entry(SemanticAttributes.NETWORK_TRANSPORT, "udp"),
entry(SemanticAttributes.NETWORK_TYPE, "ipv4"),
entry(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"),
entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"),
entry(SemanticAttributes.HTTP_ROUTE, "/repositories/{repoId}"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
Expand All @@ -28,13 +27,9 @@
import java.util.List;
import java.util.Map;
import java.util.function.ToIntFunction;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -194,57 +189,12 @@ void normal() {
entry(
AttributeKey.stringArrayKey("http.response.header.custom_response_header"),
asList("654", "321")),
entry(SemanticAttributes.NETWORK_TRANSPORT, "udp"),
entry(SemanticAttributes.NETWORK_TYPE, "ipv4"),
entry(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"),
entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "1.1"),
entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1"),
entry(NetworkAttributes.NETWORK_PEER_PORT, 456L));
}

@ParameterizedTest
@ArgumentsSource(NetworkTransportAndProtocolProvider.class)
void skipNetworkTransportIfDefaultForProtocol(
String observedProtocolName,
String observedProtocolVersion,
String observedTransport,
@Nullable String extractedTransport) {
Map<String, String> request = new HashMap<>();
request.put("networkProtocolName", observedProtocolName);
request.put("networkProtocolVersion", observedProtocolVersion);
request.put("networkTransport", observedTransport);

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

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

if (extractedTransport != null) {
assertThat(attributes.build())
.containsEntry(SemanticAttributes.NETWORK_TRANSPORT, extractedTransport);
} else {
assertThat(attributes.build()).doesNotContainKey(SemanticAttributes.NETWORK_TRANSPORT);
}
}

static final class NetworkTransportAndProtocolProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
arguments("http", "1.0", "tcp", null),
arguments("http", "1.1", "tcp", null),
arguments("http", "2.0", "tcp", null),
arguments("http", "3.0", "udp", null),
arguments("http", "1.1", "udp", "udp"),
arguments("ftp", "2.0", "tcp", "tcp"),
arguments("http", "3.0", "tcp", "tcp"),
arguments("http", "42", "tcp", "tcp"));
}
}

@ParameterizedTest
@ArgumentsSource(ValidRequestMethodsProvider.class)
void shouldExtractKnownMethods(String requestMethod) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
Expand All @@ -28,13 +27,9 @@
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -231,8 +226,6 @@ void normal() {
extractor.onEnd(endAttributes, Context.root(), request, response, null);
assertThat(endAttributes.build())
.containsOnly(
entry(SemanticAttributes.NETWORK_TRANSPORT, "udp"),
entry(SemanticAttributes.NETWORK_TYPE, "ipv4"),
entry(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"),
entry(SemanticAttributes.NETWORK_PROTOCOL_VERSION, "2.0"),
entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1"),
Expand All @@ -246,49 +239,6 @@ void normal() {
asList("654", "321")));
}

@ParameterizedTest
@ArgumentsSource(NetworkTransportAndProtocolProvider.class)
void skipNetworkTransportIfDefaultForProtocol(
String observedProtocolName,
String observedProtocolVersion,
String observedTransport,
@Nullable String extractedTransport) {
Map<String, String> request = new HashMap<>();
request.put("networkProtocolName", observedProtocolName);
request.put("networkProtocolVersion", observedProtocolVersion);
request.put("networkTransport", observedTransport);

AttributesExtractor<Map<String, String>, Map<String, String>> extractor =
HttpServerAttributesExtractor.create(new TestHttpServerAttributesGetter());

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

if (extractedTransport != null) {
assertThat(attributes.build())
.containsEntry(SemanticAttributes.NETWORK_TRANSPORT, extractedTransport);
} else {
assertThat(attributes.build()).doesNotContainKey(SemanticAttributes.NETWORK_TRANSPORT);
}
}

static final class NetworkTransportAndProtocolProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
arguments("http", "1.0", "tcp", null),
arguments("http", "1.1", "tcp", null),
arguments("http", "2.0", "tcp", null),
arguments("http", "3.0", "udp", null),
arguments("http", "1.1", "udp", "udp"),
arguments("ftp", "2.0", "tcp", "tcp"),
arguments("http", "3.0", "tcp", "tcp"),
arguments("http", "42", "tcp", "tcp"));
}
}

@ParameterizedTest
@ArgumentsSource(ValidRequestMethodsProvider.class)
void shouldExtractKnownMethods(String requestMethod) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ class UndertowServerTest extends HttpServerTest<Undertow> implements AgentTestTr
"$SemanticAttributes.HTTP_RESPONSE_BODY_SIZE" Long
"$SemanticAttributes.NETWORK_PROTOCOL_NAME" "http"
"$SemanticAttributes.NETWORK_PROTOCOL_VERSION" "1.1"
"$SemanticAttributes.NETWORK_TYPE" "ipv4"
"$SemanticAttributes.SERVER_ADDRESS" uri.host
"$SemanticAttributes.SERVER_PORT" uri.port
"$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1"
Expand Down Expand Up @@ -247,7 +246,6 @@ class UndertowServerTest extends HttpServerTest<Undertow> implements AgentTestTr
"$SemanticAttributes.HTTP_RESPONSE_BODY_SIZE" Long
"$SemanticAttributes.NETWORK_PROTOCOL_NAME" "http"
"$SemanticAttributes.NETWORK_PROTOCOL_VERSION" "1.1"
"$SemanticAttributes.NETWORK_TYPE" "ipv4"
"$SemanticAttributes.SERVER_ADDRESS" uri.host
"$SemanticAttributes.SERVER_PORT" uri.port
"$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1"
Expand Down
Loading

0 comments on commit 0fc1a99

Please sign in to comment.