Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import io.opentelemetry.instrumentation.api.incubator.semconv.net.internal.UrlParser;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter;
import io.opentelemetry.instrumentation.api.semconv.http.internal.HostAddressAndPortExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.internal.AddressAndPort;
import io.opentelemetry.instrumentation.api.semconv.network.internal.AddressAndPortExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.internal.ServerAddressAndPortExtractor;
import java.util.function.Supplier;
import javax.annotation.Nullable;

Expand All @@ -26,26 +30,36 @@ public final class HttpClientPeerServiceAttributesExtractor<REQUEST, RESPONSE>
// copied from PeerIncubatingAttributes
private static final AttributeKey<String> PEER_SERVICE = AttributeKey.stringKey("peer.service");

private final AddressAndPortExtractor<REQUEST> addressAndPortExtractor;
private final HttpClientAttributesGetter<REQUEST, RESPONSE> attributesGetter;
private final PeerServiceResolver peerServiceResolver;

// visible for tests
HttpClientPeerServiceAttributesExtractor(
AddressAndPortExtractor<REQUEST> addressAndPortExtractor,
HttpClientAttributesGetter<REQUEST, RESPONSE> attributesGetter,
PeerServiceResolver peerServiceResolver) {
this.addressAndPortExtractor = addressAndPortExtractor;
this.attributesGetter = attributesGetter;
this.peerServiceResolver = peerServiceResolver;
}

/**
* Returns a new {@link HttpClientPeerServiceAttributesExtractor} that will use the passed {@code
* attributesGetter} instance to determine the value of the {@code peer.service} attribute.
* attributesGetter} to extract server address and port (with fallback to the HTTP Host header).
*
* @param attributesGetter the HTTP attributes getter
* @param peerServiceResolver the peer service resolver
*/
public static <REQUEST, RESPONSE>
HttpClientPeerServiceAttributesExtractor<REQUEST, RESPONSE> create(
HttpClientAttributesGetter<REQUEST, RESPONSE> attributesGetter,
PeerServiceResolver peerServiceResolver) {
return new HttpClientPeerServiceAttributesExtractor<>(attributesGetter, peerServiceResolver);
AddressAndPortExtractor<REQUEST> addressAndPortExtractor =
new ServerAddressAndPortExtractor<>(
attributesGetter, new HostAddressAndPortExtractor<>(attributesGetter));
return new HttpClientPeerServiceAttributesExtractor<>(
addressAndPortExtractor, attributesGetter, peerServiceResolver);
}

@Override
Expand All @@ -65,10 +79,11 @@ public void onEnd(
return;
}

String serverAddress = attributesGetter.getServerAddress(request);
Integer serverPort = attributesGetter.getServerPort(request);
AddressAndPort addressAndPort = addressAndPortExtractor.extract(request);

Supplier<String> pathSupplier = () -> getUrlPath(attributesGetter, request);
String peerService = mapToPeerService(serverAddress, serverPort, pathSupplier);
String peerService =
mapToPeerService(addressAndPort.getAddress(), addressAndPort.getPort(), pathSupplier);
if (peerService != null) {
attributes.put(PEER_SERVICE, peerService);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
package io.opentelemetry.instrumentation.api.incubator.semconv.http;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import io.opentelemetry.api.common.Attributes;
Expand All @@ -36,7 +38,7 @@ void shouldNotSetAnyValueIfNetExtractorReturnsNulls() {
PeerServiceResolver.create(singletonMap("1.2.3.4", "myService"));

HttpClientPeerServiceAttributesExtractor<String, String> underTest =
new HttpClientPeerServiceAttributesExtractor<>(
HttpClientPeerServiceAttributesExtractor.create(
httpAttributesExtractor, peerServiceResolver);

Context context = Context.root();
Expand All @@ -57,7 +59,7 @@ void shouldNotSetAnyValueIfPeerNameDoesNotMatch() {
PeerServiceResolver.create(singletonMap("example.com", "myService"));

HttpClientPeerServiceAttributesExtractor<String, String> underTest =
new HttpClientPeerServiceAttributesExtractor<>(
HttpClientPeerServiceAttributesExtractor.create(
httpAttributesExtractor, peerServiceResolver);

when(httpAttributesExtractor.getServerAddress(any())).thenReturn("example2.com");
Expand Down Expand Up @@ -85,7 +87,7 @@ void shouldSetPeerNameIfItMatches() {
PeerServiceResolver peerServiceResolver = PeerServiceResolver.create(peerServiceMapping);

HttpClientPeerServiceAttributesExtractor<String, String> underTest =
new HttpClientPeerServiceAttributesExtractor<>(
HttpClientPeerServiceAttributesExtractor.create(
httpAttributesExtractor, peerServiceResolver);

when(httpAttributesExtractor.getServerAddress(any())).thenReturn("example.com");
Expand All @@ -103,4 +105,34 @@ void shouldSetPeerNameIfItMatches() {
assertThat(endAttributes.build())
.containsOnly(entry(PeerIncubatingAttributes.PEER_SERVICE, "myService"));
}

@Test
void shouldFallbackToHostHeaderWhenServerAddressIsNull() {
// given
PeerServiceResolver peerServiceResolver =
PeerServiceResolver.create(singletonMap("example.com", "myService"));

HttpClientPeerServiceAttributesExtractor<String, String> underTest =
HttpClientPeerServiceAttributesExtractor.create(
httpAttributesExtractor, peerServiceResolver);

// server address is null, should fallback to Host header
when(httpAttributesExtractor.getServerAddress(any())).thenReturn(null);
when(httpAttributesExtractor.getServerPort(any())).thenReturn(null);
when(httpAttributesExtractor.getHttpRequestHeader(any(), eq("host")))
.thenReturn(singletonList("example.com:8080"));

Context context = Context.root();

// when
AttributesBuilder startAttributes = Attributes.builder();
underTest.onStart(startAttributes, context, "request");
AttributesBuilder endAttributes = Attributes.builder();
underTest.onEnd(endAttributes, context, "request", "response", null);

// then
assertThat(startAttributes.build()).isEmpty();
assertThat(endAttributes.build())
.containsOnly(entry(PeerIncubatingAttributes.PEER_SERVICE, "myService"));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.internal.Experimental;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import io.opentelemetry.instrumentation.api.semconv.http.internal.HostAddressAndPortExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.internal.AddressAndPortExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.internal.InternalNetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.internal.InternalServerAttributesExtractor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.semconv.http.internal;

import io.opentelemetry.instrumentation.api.semconv.http.HttpCommonAttributesGetter;
import io.opentelemetry.instrumentation.api.semconv.network.internal.AddressAndPortExtractor;
import java.util.List;
import javax.annotation.Nullable;

/**
* Extracts server address and port from the HTTP Host header.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public final class HostAddressAndPortExtractor<REQUEST>
implements AddressAndPortExtractor<REQUEST> {

private final HttpCommonAttributesGetter<REQUEST, ?> getter;

public HostAddressAndPortExtractor(HttpCommonAttributesGetter<REQUEST, ?> getter) {
this.getter = getter;
}

@Override
public void extract(AddressPortSink sink, REQUEST request) {
String host = firstHeaderValue(getter.getHttpRequestHeader(request, "host"));
if (host == null) {
return;
}

int hostHeaderSeparator = host.indexOf(':');
if (hostHeaderSeparator == -1) {
sink.setAddress(host);
} else {
sink.setAddress(host.substring(0, hostHeaderSeparator));
setPort(sink, host, hostHeaderSeparator + 1, host.length());
}
}

@Nullable
private static String firstHeaderValue(List<String> values) {
return values.isEmpty() ? null : values.get(0);
}

private static void setPort(AddressPortSink sink, String header, int start, int end) {
if (start == end) {
return;
}
try {
sink.setPort(Integer.parseInt(header.substring(start, end)));
} catch (NumberFormatException ignored) {
// malformed port, ignoring
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.semconv.http;
package io.opentelemetry.instrumentation.api.semconv.http.internal;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
Expand All @@ -12,6 +12,7 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import io.opentelemetry.instrumentation.api.semconv.http.HttpCommonAttributesGetter;
import io.opentelemetry.instrumentation.api.semconv.network.internal.AddressAndPortExtractor;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ dependencies {
tasks {
test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ tasks {
withType<Test>().configureEach {
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ dependencies {
tasks {
test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ tasks {

test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ tasks {
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)

systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ tasks.withType<Test>().configureEach {
}

systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}

// async-http-client 2.0.0 does not work with Netty versions newer than this due to referencing an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ dependencies {
tasks {
test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ muzzle {
tasks {
test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ dependencies {
tasks {
test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ dependencies {
tasks {
test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ dependencies {
tasks {
test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
1 change: 1 addition & 0 deletions instrumentation/jodd-http-4.2/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ dependencies {
tasks {
test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ tasks {
tasks.withType<Test>().configureEach {
// TODO run tests both with and without experimental span attributes
jvmArgs("-Dotel.instrumentation.kubernetes-client.experimental-span-attributes=true")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ if (!(findProperty("testLatestDeps") as Boolean)) {
tasks {
test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ tasks {

test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")

filter {
excludeTestsMatching("Netty40ConnectionSpanTest")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public final class NettyClientSingletons {
enabledOrErrorOnly(connectionTelemetryEnabled),
enabledOrErrorOnly(sslTelemetryEnabled));
INSTRUMENTER = factory.instrumenter();
CONNECTION_INSTRUMENTER = factory.createConnectionInstrumenter();
CONNECTION_INSTRUMENTER =
factory.createConnectionInstrumenter(AgentCommonConfig.get().getPeerServiceResolver());
SSL_INSTRUMENTER = factory.createSslInstrumenter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ tasks {
test {
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
systemProperty("otel.instrumentation.common.peer-service-mapping", "127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service")

filter {
excludeTestsMatching("Netty41ConnectionSpanTest")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public final class NettyClientSingletons {
enabledOrErrorOnly(connectionTelemetryEnabled),
enabledOrErrorOnly(sslTelemetryEnabled));
INSTRUMENTER = factory.instrumenter();
CONNECTION_INSTRUMENTER = factory.createConnectionInstrumenter();
CONNECTION_INSTRUMENTER =
factory.createConnectionInstrumenter(AgentCommonConfig.get().getPeerServiceResolver());
SSL_INSTRUMENTER = factory.createSslInstrumenter();
CLIENT_HANDLER_FACTORY =
new NettyClientHandlerFactory(
Expand Down
Loading
Loading