Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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 @@ -94,7 +94,8 @@ class JavaagentTestArgumentsProvider(
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.internal.ManagedChannelImplBuilder=INFO",
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.perfmark.PerfMark=INFO",
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.Context=INFO",
"-Dotel.java.experimental.span-attributes.copy-from-baggage.include=test-baggage-key-1,test-baggage-key-2"
"-Dotel.java.experimental.span-attributes.copy-from-baggage.include=test-baggage-key-1,test-baggage-key-2",
"-Dotel.instrumentation.common.peer-service-mapping=127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service"
)
}

Expand Down
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,33 @@ 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).
*/
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 +76,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(':');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could the host header contain an ipv6 address?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is existing code just moved to .internal package, mind if I open an issue for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@@ -0,0 +1,48 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.apachedubbo.v2_7;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.apachedubbo.v2_7.internal.DubboClientNetworkAttributesGetter;
import io.opentelemetry.instrumentation.api.incubator.semconv.net.PeerServiceAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.net.PeerServiceResolver;
import java.util.HashMap;
import java.util.Map;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;

@Activate(
group = {"consumer"},
order = -1)
public final class TestOpenTelemetryClientFilter implements Filter {

private final Filter delegate;

public TestOpenTelemetryClientFilter() {
// Create peer service mapping for testing
Map<String, String> peerServiceMapping = new HashMap<>();
peerServiceMapping.put("127.0.0.1", "test-peer-service");
peerServiceMapping.put("localhost", "test-peer-service");
peerServiceMapping.put("192.0.2.1", "test-peer-service");

delegate =
DubboTelemetry.builder(GlobalOpenTelemetry.get())
.addAttributesExtractor(
PeerServiceAttributesExtractor.create(
new DubboClientNetworkAttributesGetter(),
PeerServiceResolver.create(peerServiceMapping)))
.build()
.newClientFilter();
}

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) {
return delegate.invoke(invoker, invocation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.apachedubbo.v2_7;

import io.opentelemetry.api.GlobalOpenTelemetry;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;

@Activate(
group = {"provider"},
order = -1)
public final class TestOpenTelemetryServerFilter implements Filter {

private final Filter delegate;

public TestOpenTelemetryServerFilter() {
// Server filter doesn't need peer service configuration
delegate = DubboTelemetry.create(GlobalOpenTelemetry.get()).newServerFilter();
}

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) {
return delegate.invoke(invoker, invocation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
io.opentelemetry.instrumentation.apachedubbo.v2_7.TestOpenTelemetryClientFilter
io.opentelemetry.instrumentation.apachedubbo.v2_7.TestOpenTelemetryServerFilter
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_TYPE;
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
import static io.opentelemetry.semconv.incubating.PeerIncubatingAttributes.PEER_SERVICE;
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_METHOD;
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_SERVICE;
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_SYSTEM;
Expand Down Expand Up @@ -141,6 +142,7 @@ void testApacheDubboBase() throws ReflectiveOperationException {
RpcIncubatingAttributes.RpcSystemIncubatingValues.APACHE_DUBBO),
equalTo(RPC_SERVICE, "org.apache.dubbo.rpc.service.GenericService"),
equalTo(RPC_METHOD, "$invoke"),
equalTo(PEER_SERVICE, "test-peer-service"),
equalTo(SERVER_ADDRESS, "localhost"),
satisfies(SERVER_PORT, k -> k.isInstanceOf(Long.class)),
satisfies(
Expand Down Expand Up @@ -272,6 +274,7 @@ void testApacheDubboTest()
RpcIncubatingAttributes.RpcSystemIncubatingValues.APACHE_DUBBO),
equalTo(RPC_SERVICE, "org.apache.dubbo.rpc.service.GenericService"),
equalTo(RPC_METHOD, "$invokeAsync"),
equalTo(PEER_SERVICE, "test-peer-service"),
equalTo(SERVER_ADDRESS, "localhost"),
satisfies(SERVER_PORT, k -> k.isInstanceOf(Long.class)),
satisfies(
Expand Down
Loading
Loading