Skip to content

Commit

Permalink
Move PeerServiceAttributesExtractor to javaagent-api and use reflecti…
Browse files Browse the repository at this point in the history
…on to add it
  • Loading branch information
Mateusz Rzeszutek committed May 21, 2021
1 parent 4a3cc13 commit 96730cf
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public abstract class InetSocketAddressNetAttributesExtractor<REQUEST, RESPONSE>

@Override
@Nullable
protected final String peerName(REQUEST request, @Nullable RESPONSE response) {
public final String peerName(REQUEST request, @Nullable RESPONSE response) {
InetSocketAddress address = getAddress(request, response);
if (address == null) {
return null;
Expand All @@ -42,7 +42,7 @@ protected final String peerName(REQUEST request, @Nullable RESPONSE response) {

@Override
@Nullable
protected final Long peerPort(REQUEST request, @Nullable RESPONSE response) {
public final Long peerPort(REQUEST request, @Nullable RESPONSE response) {
InetSocketAddress address = getAddress(request, response);
if (address == null) {
return null;
Expand All @@ -52,7 +52,7 @@ protected final Long peerPort(REQUEST request, @Nullable RESPONSE response) {

@Override
@Nullable
protected final String peerIp(REQUEST request, @Nullable RESPONSE response) {
public final String peerIp(REQUEST request, @Nullable RESPONSE response) {
InetSocketAddress address = getAddress(request, response);
if (address == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ protected final void onEnd(AttributesBuilder attributes, REQUEST request, RESPON
* phases of processing.
*/
@Nullable
protected abstract String peerName(REQUEST request, @Nullable RESPONSE response);
public abstract String peerName(REQUEST request, @Nullable RESPONSE response);

/**
* This method will be called twice: both when the request starts ({@code response} is always null
* then) and when the response ends. This way it is possible to capture net attributes in both
* phases of processing.
*/
@Nullable
protected abstract Long peerPort(REQUEST request, @Nullable RESPONSE response);
public abstract Long peerPort(REQUEST request, @Nullable RESPONSE response);

/**
* This method will be called twice: both when the request starts ({@code response} is always null
* then) and when the response ends. This way it is possible to capture net attributes in both
* phases of processing.
*/
@Nullable
protected abstract String peerIp(REQUEST request, @Nullable RESPONSE response);
public abstract String peerIp(REQUEST request, @Nullable RESPONSE response);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ protected String transport(Map<String, String> request) {
}

@Override
protected String peerName(Map<String, String> request, Map<String, String> response) {
public String peerName(Map<String, String> request, Map<String, String> response) {
if (response != null) {
return response.get("peerName");
}
return request.get("peerName");
}

@Override
protected Long peerPort(Map<String, String> request, Map<String, String> response) {
public Long peerPort(Map<String, String> request, Map<String, String> response) {
if (response != null) {
return Long.valueOf(response.get("peerPort"));
}
return Long.valueOf(request.get("peerPort"));
}

@Override
protected String peerIp(Map<String, String> request, Map<String, String> response) {
public String peerIp(Map<String, String> request, Map<String, String> response) {
if (response != null) {
return response.get("peerIp");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanStatusExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.PeerServiceAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.api.instrumenter.PeerServiceAttributesExtractor;
import org.apache.commons.httpclient.HttpMethod;

public final class ApacheHttpClientInstrumenters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ protected String transport(HttpMethod httpMethod) {
}

@Override
protected @Nullable String peerName(HttpMethod httpMethod, @Nullable Void unused) {
public @Nullable String peerName(HttpMethod httpMethod, @Nullable Void unused) {
HostConfiguration hostConfiguration = httpMethod.getHostConfiguration();
return hostConfiguration != null ? hostConfiguration.getHost() : null;
}

@Override
protected @Nullable Long peerPort(HttpMethod httpMethod, @Nullable Void unused) {
public @Nullable Long peerPort(HttpMethod httpMethod, @Nullable Void unused) {
HostConfiguration hostConfiguration = httpMethod.getHostConfiguration();
return hostConfiguration != null ? (long) hostConfiguration.getPort() : null;
}

@Override
protected @Nullable String peerIp(HttpMethod httpMethod, @Nullable Void unused) {
public @Nullable String peerIp(HttpMethod httpMethod, @Nullable Void unused) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,58 @@
package io.opentelemetry.javaagent.instrumentation.armeria.v1_3;

import com.linecorp.armeria.client.HttpClient;
import com.linecorp.armeria.common.RequestContext;
import com.linecorp.armeria.common.logging.RequestLog;
import com.linecorp.armeria.server.HttpService;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.net.NetAttributesExtractor;
import io.opentelemetry.instrumentation.armeria.v1_3.ArmeriaTracing;
import io.opentelemetry.instrumentation.armeria.v1_3.ArmeriaTracingBuilder;
import io.opentelemetry.javaagent.instrumentation.api.instrumenter.PeerServiceAttributesExtractor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Holds singleton references to decorators to match against during suppression.
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/903
public final class ArmeriaDecorators {
private static final Logger log = LoggerFactory.getLogger(ArmeriaDecorators.class);

private static final ArmeriaTracing TRACING = ArmeriaTracing.create(GlobalOpenTelemetry.get());
public static final Function<? super HttpClient, ? extends HttpClient> CLIENT_DECORATOR;

public static final Function<? super HttpClient, ? extends HttpClient> CLIENT_DECORATOR =
TRACING.newClientDecorator();
public static final Function<? super HttpService, ? extends HttpService> SERVER_DECORATOR;

public static final Function<? super HttpService, ? extends HttpService> SERVER_DECORATOR =
TRACING.newServiceDecorator();
static {
ArmeriaTracingBuilder builder = ArmeriaTracing.newBuilder(GlobalOpenTelemetry.get());

Constructor<? extends NetAttributesExtractor<RequestContext, RequestLog>> constructor = null;
try {
Class<? extends NetAttributesExtractor<RequestContext, RequestLog>>
netAttributesExtractorClass =
(Class<? extends NetAttributesExtractor<RequestContext, RequestLog>>)
Class.forName(
"io.opentelemetry.instrumentation.armeria.v1_3.ArmeriaNetAttributesExtractor");
constructor = netAttributesExtractorClass.getDeclaredConstructor();
constructor.setAccessible(true);
NetAttributesExtractor<RequestContext, RequestLog> netAttributesExtractor =
constructor.newInstance();
builder.addAttributeExtractor(PeerServiceAttributesExtractor.create(netAttributesExtractor));
} catch (ClassNotFoundException
| NoSuchMethodException
| IllegalAccessException
| InstantiationException
| InvocationTargetException e) {
log.warn("Could not add PeerServiceAttributesExtractor to armeria", e);
} finally {
if (constructor != null) {
constructor.setAccessible(false);
}
}

ArmeriaTracing tracing = builder.build();
CLIENT_DECORATOR = tracing.newClientDecorator();
SERVER_DECORATOR = tracing.newServiceDecorator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerMetrics;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanStatusExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.PeerServiceAttributesExtractor;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
Expand Down Expand Up @@ -64,9 +63,6 @@ public ArmeriaTracingBuilder addAttributeExtractor(
public ArmeriaTracing build() {
ArmeriaHttpAttributesExtractor httpAttributesExtractor = new ArmeriaHttpAttributesExtractor();
ArmeriaNetAttributesExtractor netAttributesExtractor = new ArmeriaNetAttributesExtractor();
AttributesExtractor<? super RequestContext, ? super RequestLog>
peerServiceAttributesExtractor =
PeerServiceAttributesExtractor.create(netAttributesExtractor);

SpanNameExtractor<? super RequestContext> spanNameExtractor =
HttpSpanNameExtractor.create(httpAttributesExtractor);
Expand All @@ -85,7 +81,6 @@ public ArmeriaTracing build() {
.setSpanStatusExtractor(spanStatusExtractor)
.addAttributesExtractor(httpAttributesExtractor)
.addAttributesExtractor(netAttributesExtractor)
.addAttributesExtractor(peerServiceAttributesExtractor)
.addAttributesExtractors(additionalExtractors));

serverInstrumenterBuilder.addRequestMetrics(HttpServerMetrics.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.db.DbAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.db.DbSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.PeerServiceAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.api.instrumenter.PeerServiceAttributesExtractor;

public final class JdbcInstrumenters {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.javaagent.jdbc";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ protected String transport(DbRequest dbRequest) {

@Nullable
@Override
protected String peerName(DbRequest request, @Nullable Void response) {
public String peerName(DbRequest request, @Nullable Void response) {
return request.getDbInfo().getHost();
}

@Nullable
@Override
protected Long peerPort(DbRequest request, @Nullable Void response) {
public Long peerPort(DbRequest request, @Nullable Void response) {
Integer port = request.getDbInfo().getPort();
return port == null ? null : port.longValue();
}

@Nullable
@Override
protected String peerIp(DbRequest request, @Nullable Void response) {
public String peerIp(DbRequest request, @Nullable Void response) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

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

import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.NetAttributesExtractor;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.Map;

Expand Down

0 comments on commit 96730cf

Please sign in to comment.