Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PeerService Resolver #9061

Merged
merged 21 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
@@ -0,0 +1,94 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter.http;

import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.PeerServiceResolver;
import io.opentelemetry.instrumentation.api.instrumenter.url.UrlParser;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import javax.annotation.Nullable;

/**
* Extractor of the {@code peer.service} span attribute, described in <a
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/span-general.md#general-remote-service-attributes">the
* specification</a>.
*/
public final class HttpClientPeerServiceAttributesExtractor<REQUEST, RESPONSE>
implements AttributesExtractor<REQUEST, RESPONSE> {

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

// visible for tests
HttpClientPeerServiceAttributesExtractor(
HttpClientAttributesGetter<REQUEST, RESPONSE> attributesGetter,
PeerServiceResolver peerServiceResolver) {
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.
*/
public static <REQUEST, RESPONSE>
HttpClientPeerServiceAttributesExtractor<REQUEST, RESPONSE> create(
HttpClientAttributesGetter<REQUEST, RESPONSE> attributesGetter,
PeerServiceResolver peerServiceResolver) {
return new HttpClientPeerServiceAttributesExtractor<>(attributesGetter, peerServiceResolver);
}

@Override
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {}

@Override
public void onEnd(
AttributesBuilder attributes,
Context context,
REQUEST request,
@Nullable RESPONSE response,
@Nullable Throwable error) {

if (peerServiceResolver.isEmpty()) {
// optimization for common case
return;
}

String serverAddress = attributesGetter.getServerAddress(request);
Integer serverPort = attributesGetter.getServerPort(request);
String path = getUrlPath(attributesGetter, request);
Copy link
Member

Choose a reason for hiding this comment

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

it would be nice if we could avoid the url parsing in the common case where there's no peer service mapping (and even better also in the case where there's no peer service mapping that depends on path)

what do you think about passing in the http.url (and only the http.url) to the PeerServiceResolver? and the PeerServiceResolver could then just check matches directly against the http.url without needing to do any parsing I think

I guess this would only work for http client instrumentation.

Maybe there's not a one-size-fits-all PeerServiceResolver interface.

E.g. for database instrumentation, you may still want a PeerServiceResolver, but path doesn't make sense in that case, so path already is specific to http PeerServiceResolvers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it would be nice if we could avoid the url parsing in the common case where there's no peer service mapping (and even better also in the case where there's no peer service mapping that depends on path)

It would be nice indeed !

what do you think about passing in the http.url (and only the http.url) to the PeerServiceResolver?

I don't mind seing the PeerServiceResolver having different interfaces depending on the client instrumentation type. Does it has an impact on the agent configuration like another property or having to precise the port explicitly ?

PeerServiceResolver could then just check matches directly against the http.url without needing to do any parsing I think

That seems quite clean !

I see your point on performance here but I don't see yet what will be the impact on the configuration of the agent and how it would be implemented.
If you want to proceed on that trajectory, feel free to take it from here.

Copy link
Member

Choose a reason for hiding this comment

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

what do you think about passing in the http.url (and only the http.url) to the PeerServiceResolver? and the PeerServiceResolver could then just check matches directly against the http.url without needing to do any parsing I think

Alternatively, the resolver could accept a Supplier<String> just for the path parameter, and only lazily evaluate that when host & port match.

String peerService = mapToPeerService(serverAddress, serverPort, path);
if (peerService == null) {
String serverSocketDomain = attributesGetter.getServerSocketDomain(request, response);
Integer serverSocketPort = attributesGetter.getServerSocketPort(request, response);
peerService = mapToPeerService(serverSocketDomain, serverSocketPort, null);
}
if (peerService != null) {
attributes.put(SemanticAttributes.PEER_SERVICE, peerService);
}
}

@Nullable
private String mapToPeerService(
@Nullable String host, @Nullable Integer port, @Nullable String path) {
if (host == null) {
return null;
}
return peerServiceResolver.resolveService(host, port, path);
}

@Nullable
private String getUrlPath(
HttpClientAttributesGetter<REQUEST, RESPONSE> attributesGetter, REQUEST request) {
String urlFull = attributesGetter.getUrlFull(request);
if (urlFull == null) {
return null;
}
return UrlParser.getPath(urlFull);
}
}
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.ServerAttributesGetter;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.Map;
import javax.annotation.Nullable;

/**
Expand All @@ -22,24 +21,24 @@ public final class PeerServiceAttributesExtractor<REQUEST, RESPONSE>
implements AttributesExtractor<REQUEST, RESPONSE> {

private final ServerAttributesGetter<REQUEST, RESPONSE> attributesGetter;
private final Map<String, String> peerServiceMapping;
private final PeerServiceResolver peerServiceResolver;

// visible for tests
PeerServiceAttributesExtractor(
ServerAttributesGetter<REQUEST, RESPONSE> attributesGetter,
Map<String, String> peerServiceMapping) {
PeerServiceResolver peerServiceResolver) {
this.attributesGetter = attributesGetter;
this.peerServiceMapping = peerServiceMapping;
this.peerServiceResolver = peerServiceResolver;
}

/**
* Returns a new {@link PeerServiceAttributesExtractor} that will use the passed {@code
* netAttributesExtractor} instance to determine the value of the {@code peer.service} attribute.
* attributesGetter} instance to determine the value of the {@code peer.service} attribute.
*/
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
ServerAttributesGetter<REQUEST, RESPONSE> attributesGetter,
Map<String, String> peerServiceMapping) {
return new PeerServiceAttributesExtractor<>(attributesGetter, peerServiceMapping);
PeerServiceResolver peerServiceResolver) {
return new PeerServiceAttributesExtractor<>(attributesGetter, peerServiceResolver);
}

@Override
Expand All @@ -53,27 +52,29 @@ public void onEnd(
@Nullable RESPONSE response,
@Nullable Throwable error) {

if (peerServiceMapping.isEmpty()) {
if (peerServiceResolver.isEmpty()) {
// optimization for common case
return;
}

String serverAddress = attributesGetter.getServerAddress(request);
String peerService = mapToPeerService(serverAddress);
Integer serverPort = attributesGetter.getServerPort(request);
String peerService = mapToPeerService(serverAddress, serverPort);
if (peerService == null) {
String serverSocketDomain = attributesGetter.getServerSocketDomain(request, response);
peerService = mapToPeerService(serverSocketDomain);
Integer serverSocketPort = attributesGetter.getServerSocketPort(request, response);
peerService = mapToPeerService(serverSocketDomain, serverSocketPort);
}
if (peerService != null) {
attributes.put(SemanticAttributes.PEER_SERVICE, peerService);
}
}

@Nullable
private String mapToPeerService(@Nullable String endpoint) {
if (endpoint == null) {
private String mapToPeerService(@Nullable String host, @Nullable Integer port) {
if (host == null) {
return null;
}
return peerServiceMapping.get(endpoint);
return peerServiceResolver.resolveService(host, port, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter.net;

import java.util.Map;
import javax.annotation.Nullable;

public interface PeerServiceResolver {

public boolean isEmpty();

@Nullable
public String resolveService(String host, @Nullable Integer port, @Nullable String path);

static PeerServiceResolver create(Map<String, String> mapping) {
return new PeerServiceResolverImpl(mapping);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter.net;

import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.nullsFirst;

import com.google.auto.value.AutoValue;
import io.opentelemetry.instrumentation.api.instrumenter.url.UrlParser;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;

class PeerServiceResolverImpl implements PeerServiceResolver {

private static final Comparator<ServiceMatcher> matcherComparator =
nullsFirst(
comparing(ServiceMatcher::getPort, nullsFirst(naturalOrder()))
.thenComparing(comparing(ServiceMatcher::getPath, nullsFirst(naturalOrder()))));

private final Map<String, Map<ServiceMatcher, String>> mapping = new HashMap<>();

PeerServiceResolverImpl(Map<String, String> peerServiceMapping) {
peerServiceMapping.forEach(
(key, serviceName) -> {
String url = "https://" + key;
String host = UrlParser.getHost(url);
Integer port = UrlParser.getPort(url);
String path = UrlParser.getPath(url);
Map<ServiceMatcher, String> matchers =
mapping.computeIfAbsent(host, x -> new HashMap<>());
matchers.putIfAbsent(ServiceMatcher.create(port, path), serviceName);
});
}

@Override
public boolean isEmpty() {
return mapping.isEmpty();
}

@Override
@Nullable
public String resolveService(String host, @Nullable Integer port, @Nullable String path) {
Map<ServiceMatcher, String> matchers = mapping.get(host);
if (matchers == null) {
return null;
}
return matchers.entrySet().stream()
.filter(entry -> entry.getKey().matches(port, path))
.max((o1, o2) -> matcherComparator.compare(o1.getKey(), o2.getKey()))
.map(Map.Entry::getValue)
.orElse(null);
}

@AutoValue
abstract static class ServiceMatcher {

static ServiceMatcher create(Integer port, String path) {
return new AutoValue_PeerServiceResolverImpl_ServiceMatcher(port, path);
}

@Nullable
abstract Integer getPort();

@Nullable
abstract String getPath();

public boolean matches(Integer port, String path) {
if (this.getPort() != null) {
if (!this.getPort().equals(port)) {
return false;
}
}
trask marked this conversation as resolved.
Show resolved Hide resolved
if (this.getPath() != null && this.getPath().length() > 0) {
if (path == null) {
return false;
}
if (!path.startsWith(this.getPath())) {
return false;
}
if (port != null) {
return port.equals(this.getPort());
}
trask marked this conversation as resolved.
Show resolved Hide resolved
}
return true;
}
}
}
Loading
Loading