Fix PeerServiceAttributesExtractor#createUsingReflection() (#3378)

* Fix PeerServiceAttributesExtractor#createUsingReflection()

* move armeria & grpc NetAttributesExtractors to internal package
This commit is contained in:
Mateusz Rzeszutek 2021-06-23 11:25:35 +02:00 committed by GitHub
parent c1388c9c4c
commit 256f2c992e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 25 additions and 91 deletions

View File

@ -10,9 +10,8 @@ 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.AttributesExtractor;
import io.opentelemetry.instrumentation.armeria.v1_3.ArmeriaTracing;
import io.opentelemetry.instrumentation.armeria.v1_3.ArmeriaTracingBuilder;
import io.opentelemetry.instrumentation.armeria.v1_3.internal.ArmeriaNetAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.api.instrumenter.PeerServiceAttributesExtractor;
import java.util.function.Function;
@ -24,16 +23,14 @@ public final class ArmeriaSingletons {
public static final Function<? super HttpService, ? extends HttpService> SERVER_DECORATOR;
static {
ArmeriaTracingBuilder builder = ArmeriaTracing.newBuilder(GlobalOpenTelemetry.get());
PeerServiceAttributesExtractor<RequestContext, RequestLog> peerServiceAttributesExtractor =
PeerServiceAttributesExtractor.create(new ArmeriaNetAttributesExtractor());
AttributesExtractor<RequestContext, RequestLog> peerServiceAttributesExtractor =
PeerServiceAttributesExtractor.createUsingReflection(
"io.opentelemetry.instrumentation.armeria.v1_3.ArmeriaNetAttributesExtractor");
if (peerServiceAttributesExtractor != null) {
builder.addAttributeExtractor(peerServiceAttributesExtractor);
}
ArmeriaTracing tracing =
ArmeriaTracing.newBuilder(GlobalOpenTelemetry.get())
.addAttributeExtractor(peerServiceAttributesExtractor)
.build();
ArmeriaTracing tracing = builder.build();
CLIENT_DECORATOR = tracing.newClientDecorator();
SERVER_DECORATOR = tracing.newServiceDecorator();
}

View File

@ -18,6 +18,7 @@ import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor;
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.armeria.v1_3.internal.ArmeriaNetAttributesExtractor;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

View File

@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.armeria.v1_3;
package io.opentelemetry.instrumentation.armeria.v1_3.internal;
import com.linecorp.armeria.common.RequestContext;
import com.linecorp.armeria.common.logging.RequestLog;
@ -13,7 +13,7 @@ import java.net.InetSocketAddress;
import java.net.SocketAddress;
import org.checkerframework.checker.nullness.qual.Nullable;
final class ArmeriaNetAttributesExtractor
public final class ArmeriaNetAttributesExtractor
extends InetSocketAddressNetAttributesExtractor<RequestContext, RequestLog> {
@Override

View File

@ -13,8 +13,8 @@ import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.grpc.v1_6.GrpcRequest;
import io.opentelemetry.instrumentation.grpc.v1_6.GrpcTracing;
import io.opentelemetry.instrumentation.grpc.v1_6.GrpcTracingBuilder;
import io.opentelemetry.instrumentation.grpc.v1_6.internal.ContextStorageBridge;
import io.opentelemetry.instrumentation.grpc.v1_6.internal.GrpcNetAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.api.instrumenter.PeerServiceAttributesExtractor;
// Holds singleton references.
@ -27,21 +27,18 @@ public final class GrpcSingletons {
public static final Context.Storage STORAGE = new ContextStorageBridge();
static {
GrpcTracingBuilder builder =
boolean experimentalSpanAttributes =
Config.get()
.getBooleanProperty("otel.instrumentation.grpc.experimental-span-attributes", false);
PeerServiceAttributesExtractor<GrpcRequest, Status> peerServiceAttributesExtractor =
PeerServiceAttributesExtractor.create(new GrpcNetAttributesExtractor());
GrpcTracing tracing =
GrpcTracing.newBuilder(GlobalOpenTelemetry.get())
.setCaptureExperimentalSpanAttributes(
Config.get()
.getBooleanProperty(
"otel.instrumentation.grpc.experimental-span-attributes", false));
.setCaptureExperimentalSpanAttributes(experimentalSpanAttributes)
.addAttributeExtractor(peerServiceAttributesExtractor)
.build();
PeerServiceAttributesExtractor<GrpcRequest, Status> peerServiceExtractor =
PeerServiceAttributesExtractor.createUsingReflection(
"io.opentelemetry.instrumentation.grpc.v1_6.GrpcNetAttributesExtractor");
if (peerServiceExtractor != null) {
builder.addAttributeExtractor(peerServiceExtractor);
}
GrpcTracing tracing = builder.build();
CLIENT_INTERCEPTOR = tracing.newClientInterceptor();
SERVER_INTERCEPTOR = tracing.newServerInterceptor();
}

View File

@ -11,6 +11,7 @@ import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.grpc.v1_6.internal.GrpcNetAttributesExtractor;
import java.util.ArrayList;
import java.util.List;

View File

@ -3,16 +3,17 @@
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.grpc.v1_6;
package io.opentelemetry.instrumentation.grpc.v1_6.internal;
import io.grpc.Status;
import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetAttributesExtractor;
import io.opentelemetry.instrumentation.grpc.v1_6.GrpcRequest;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import org.checkerframework.checker.nullness.qual.Nullable;
final class GrpcNetAttributesExtractor
public final class GrpcNetAttributesExtractor
extends InetSocketAddressNetAttributesExtractor<GrpcRequest, Status> {
@Override
@Nullable

View File

@ -11,7 +11,6 @@ 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;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Extractor of the {@code peer.service} span attribute, described in <a
@ -48,19 +47,6 @@ public final class PeerServiceAttributesExtractor<REQUEST, RESPONSE>
JAVAAGENT_PEER_SERVICE_MAPPING, netAttributesExtractor);
}
/**
* Returns a new {@link PeerServiceAttributesExtractor} that will create a new instance of type
* {@code netAttributesExtractorImplClassName} (which must extend {@link NetAttributesExtractor})
* and use it to determine the value of the {@code peer.service} attribute.
*/
@Nullable
public static <REQUEST, RESPONSE>
PeerServiceAttributesExtractor<REQUEST, RESPONSE> createUsingReflection(
String netAttributesExtractorImplClassName) {
return ReflectionPeerServiceAttributesExtractorFactory.create(
netAttributesExtractorImplClassName);
}
@Override
protected void onStart(AttributesBuilder attributes, REQUEST request) {
onEnd(attributes, request, null);

View File

@ -1,49 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.api.instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.net.NetAttributesExtractor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final class ReflectionPeerServiceAttributesExtractorFactory {
private static final Logger log =
LoggerFactory.getLogger(ReflectionPeerServiceAttributesExtractorFactory.class);
@Nullable
static <REQUEST, RESPONSE> PeerServiceAttributesExtractor<REQUEST, RESPONSE> create(
String netAttributesImplClassName) {
Constructor<? extends NetAttributesExtractor<REQUEST, RESPONSE>> constructor = null;
try {
Class<? extends NetAttributesExtractor<REQUEST, RESPONSE>> netAttributesExtractorClass =
(Class<? extends NetAttributesExtractor<REQUEST, RESPONSE>>)
Class.forName(netAttributesImplClassName);
constructor = netAttributesExtractorClass.getDeclaredConstructor();
constructor.setAccessible(true);
NetAttributesExtractor<REQUEST, RESPONSE> netAttributesExtractor = constructor.newInstance();
return PeerServiceAttributesExtractor.create(netAttributesExtractor);
} catch (ClassNotFoundException
| NoSuchMethodException
| IllegalAccessException
| InstantiationException
| InvocationTargetException e) {
log.warn(
"Could not add PeerServiceAttributesExtractor wrapping {}",
netAttributesImplClassName,
e);
return null;
} finally {
if (constructor != null) {
constructor.setAccessible(false);
}
}
}
private ReflectionPeerServiceAttributesExtractorFactory() {}
}