Add BaseTracer#inject() method
* Add BaseTracer#inject() method
And avoid using GlobalOpenTelemetry#getPropagators()
* Code review comments
MODIFY
instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/BaseTracer.java
instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/BaseTracer.java
@@ -16,6 +16,8 @@ import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapGetter;
+import io.opentelemetry.context.propagation.TextMapPropagator;
+import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.instrumentation.api.InstrumentationVersion;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.api.context.ContextPropagationDebug;
@@ -76,10 +78,6 @@ public abstract class BaseTracer {
this.propagators = openTelemetry.getPropagators();
}
- public ContextPropagators getPropagators() {
- return propagators;
- }
-
public Context startSpan(Class<?> clazz) {
return startSpan(spanNameForClass(clazz));
}
@@ -237,6 +235,16 @@ public abstract class BaseTracer {
return propagators.getTextMapPropagator().extract(Context.root(), carrier, getter);
}
+ /**
+ * Injects {@code context} data into {@code carrier} using the propagator embedded in this tracer.
+ * This method can be used to propagate passed {@code context} to downstream services.
+ *
+ * @see TextMapPropagator#inject(Context, Object, TextMapSetter)
+ */
+ public <C> void inject(Context context, C carrier, TextMapSetter<C> setter) {
+ propagators.getTextMapPropagator().inject(context, carrier, setter);
+ }
+
/** Returns span of type SERVER from the current context or <code>null</code> if not found. */
public static Span getCurrentServerSpan() {
return getCurrentServerSpan(Context.current());

MODIFY
instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/HttpClientTracer.java
instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/HttpClientTracer.java
@@ -101,7 +101,7 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
throw new IllegalStateException(
"getSetter() not defined but calling inject(), either getSetter must be implemented or the scope should be setup manually");
}
- propagators.getTextMapPropagator().inject(context, carrier, setter);
+ inject(context, carrier, setter);
}
public void end(Context context, RESPONSE response) {

MODIFY
instrumentation/apache-dubbo/apache-dubbo-2.7/library/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryFilter.java
instrumentation/apache-dubbo/apache-dubbo-2.7/library/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryFilter.java
@@ -8,7 +8,6 @@ package io.opentelemetry.instrumentation.apachedubbo.v2_7;
import static io.opentelemetry.api.trace.SpanKind.CLIENT;
import static io.opentelemetry.api.trace.SpanKind.SERVER;
-import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
@@ -42,9 +41,7 @@ public class OpenTelemetryFilter implements Filter {
final Context context;
if (kind.equals(CLIENT)) {
context = tracer.startClientSpan(interfaceName, methodName);
- GlobalOpenTelemetry.getPropagators()
- .getTextMapPropagator()
- .inject(context, (RpcInvocation) invocation, DubboInjectAdapter.SETTER);
+ tracer.inject(context, (RpcInvocation) invocation, DubboInjectAdapter.SETTER);
} else {
context = tracer.startServerSpan(interfaceName, methodName, (RpcInvocation) invocation);
}

MODIFY
instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientInstrumentation.java
instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientInstrumentation.java
@@ -110,10 +110,7 @@ public class ApacheHttpAsyncClientInstrumentation implements TypeInstrumentation
@Override
public HttpRequest generateRequest() throws IOException, HttpException {
HttpRequest request = delegate.generateRequest();
- tracer()
- .getPropagators()
- .getTextMapPropagator()
- .inject(context, request, tracer().getSetter());
+ tracer().inject(context, request);
Span span = Span.fromContext(context);
span.updateName(tracer().spanNameForRequest(request));
tracer().onRequest(span, request);

MODIFY
instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientTracer.java
instrumentation/apache-httpasyncclient-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientTracer.java
@@ -103,11 +103,18 @@ public class ApacheHttpAsyncClientTracer
return "io.opentelemetry.javaagent.apache-httpasyncclient-4.1";
}
+ /** This method is overridden to allow other classes in this package to call it. */
@Override
public String spanNameForRequest(HttpRequest httpRequest) {
return super.spanNameForRequest(httpRequest);
}
+ /** This method is overridden to allow other classes in this package to call it. */
+ @Override
+ protected void inject(Context context, HttpRequest httpRequest) {
+ super.inject(context, httpRequest);
+ }
+
/** This method is overridden to allow other classes in this package to call it. */
@Override
protected void onRequest(Span span, HttpRequest httpRequest) {

MODIFY
instrumentation/grpc-1.5/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_5/client/TracingClientInterceptor.java
instrumentation/grpc-1.5/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_5/client/TracingClientInterceptor.java
@@ -18,7 +18,6 @@ import io.grpc.Grpc;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.Status;
-import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
@@ -97,7 +96,7 @@ public class TracingClientInterceptor implements ClientInterceptor {
@Override
public void start(Listener<RESPONSE> responseListener, Metadata headers) {
- GlobalOpenTelemetry.getPropagators().getTextMapPropagator().inject(context, headers, SETTER);
+ tracer.inject(context, headers, SETTER);
try (Scope ignored = context.makeCurrent()) {
super.start(new TracingClientCallListener<>(responseListener, context, tracer), headers);
} catch (Throwable e) {

MODIFY
instrumentation/java-httpclient/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpclient/JdkHttpClientTracer.java
instrumentation/java-httpclient/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpclient/JdkHttpClientTracer.java
package io.opentelemetry.javaagent.instrumentation.httpclient;
-import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapSetter;
@@ -87,9 +86,7 @@ public class JdkHttpClientTracer
public HttpHeaders inject(HttpHeaders original) {
Map<String, List<String>> headerMap = new HashMap<>();
- GlobalOpenTelemetry.getPropagators()
- .getTextMapPropagator()
- .inject(
+ inject(
Context.current(),
headerMap,
(carrier, key, value) -> carrier.put(key, Collections.singletonList(value)));

MODIFY
instrumentation/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JmsTracer.java
instrumentation/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JmsTracer.java
@@ -70,7 +70,7 @@ public class JmsTracer extends BaseTracer {
}
public Scope startProducerScope(Context context, Message message) {
- GlobalOpenTelemetry.getPropagators().getTextMapPropagator().inject(context, message, SETTER);
+ inject(context, message, SETTER);
return context.makeCurrent();
}

MODIFY
instrumentation/kafka-clients-0.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/kafkaclients/KafkaProducerInstrumentation.java
instrumentation/kafka-clients-0.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/kafkaclients/KafkaProducerInstrumentation.java
@@ -62,9 +62,7 @@ public class KafkaProducerInstrumentation implements TypeInstrumentation {
if (tracer().shouldPropagate(apiVersions)) {
try {
- Java8BytecodeBridge.getGlobalPropagators()
- .getTextMapPropagator()
- .inject(context, record.headers(), SETTER);
+ tracer().inject(context, record.headers(), SETTER);
} catch (IllegalStateException e) {
// headers must be read-only from reused record. try again with new one.
record =
@@ -76,9 +74,7 @@ public class KafkaProducerInstrumentation implements TypeInstrumentation {
record.value(),
record.headers());
- Java8BytecodeBridge.getGlobalPropagators()
- .getTextMapPropagator()
- .inject(context, record.headers(), SETTER);
+ tracer().inject(context, record.headers(), SETTER);
}
}

MODIFY
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyHttpClientTracer.java
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyHttpClientTracer.java
@@ -9,7 +9,6 @@ import static io.opentelemetry.api.trace.SpanKind.CLIENT;
import static io.opentelemetry.javaagent.instrumentation.netty.v3_8.client.NettyResponseInjectAdapter.SETTER;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.HOST;
-import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapSetter;
@@ -43,9 +42,7 @@ public class NettyHttpClientTracer
NetPeerUtils.INSTANCE.setNetPeer(span, (InetSocketAddress) ctx.getChannel().getRemoteAddress());
Context context = withClientSpan(parentContext, span);
- GlobalOpenTelemetry.getPropagators()
- .getTextMapPropagator()
- .inject(context, request.headers(), SETTER);
+ inject(context, request.headers(), SETTER);
return context;
}

MODIFY
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/NettyHttpClientTracer.java
instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/NettyHttpClientTracer.java
@@ -13,7 +13,6 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
-import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapSetter;
@@ -43,9 +42,7 @@ public class NettyHttpClientTracer
NetPeerUtils.INSTANCE.setNetPeer(span, (InetSocketAddress) ctx.channel().remoteAddress());
Context context = withClientSpan(parentContext, span);
- GlobalOpenTelemetry.getPropagators()
- .getTextMapPropagator()
- .inject(context, request.headers(), SETTER);
+ inject(context, request.headers(), SETTER);
return context;
}

MODIFY
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/client/NettyHttpClientTracer.java
instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/client/NettyHttpClientTracer.java
@@ -13,7 +13,6 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
-import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapSetter;
@@ -43,9 +42,7 @@ public class NettyHttpClientTracer
NetPeerUtils.INSTANCE.setNetPeer(span, (InetSocketAddress) ctx.channel().remoteAddress());
Context context = withClientSpan(parentContext, span);
- GlobalOpenTelemetry.getPropagators()
- .getTextMapPropagator()
- .inject(context, request.headers(), SETTER);
+ inject(context, request.headers(), SETTER);
return context;
}

MODIFY
instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitChannelInstrumentation.java
instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitChannelInstrumentation.java
@@ -130,7 +130,8 @@ public class RabbitChannelInstrumentation implements TypeInstrumentation {
@Advice.Argument(1) String routingKey,
@Advice.Argument(value = 4, readOnly = false) AMQP.BasicProperties props,
@Advice.Argument(5) byte[] body) {
- Span span = Java8BytecodeBridge.currentSpan();
+ Context context = Java8BytecodeBridge.currentContext();
+ Span span = Java8BytecodeBridge.spanFromContext(context);
if (span.getSpanContext().isValid()) {
tracer().onPublish(span, exchange, routingKey);
@@ -152,11 +153,7 @@ public class RabbitChannelInstrumentation implements TypeInstrumentation {
Map<String, Object> headers = props.getHeaders();
headers = (headers == null) ? new HashMap<>() : new HashMap<>(headers);
- Context context = Java8BytecodeBridge.currentContext().with(span);
-
- Java8BytecodeBridge.getGlobalPropagators()
- .getTextMapPropagator()
- .inject(context, headers, TextMapInjectAdapter.SETTER);
+ tracer().inject(context, headers, TextMapInjectAdapter.SETTER);
props =
new AMQP.BasicProperties(

MODIFY
instrumentation/rmi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rmi/context/ContextPayload.java
instrumentation/rmi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rmi/context/ContextPayload.java
package io.opentelemetry.javaagent.instrumentation.rmi.context;
-import io.opentelemetry.api.GlobalOpenTelemetry;
+import static io.opentelemetry.javaagent.instrumentation.rmi.client.RmiClientTracer.tracer;
+
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapSetter;
@@ -36,7 +37,7 @@ public class ContextPayload {
public static ContextPayload from(Context context) {
ContextPayload payload = new ContextPayload();
- GlobalOpenTelemetry.getPropagators().getTextMapPropagator().inject(context, payload, SETTER);
+ tracer().inject(context, payload, SETTER);
return payload;
}

Add BaseTracer#inject() method (#2453)

* Add BaseTracer#inject() method

And avoid using GlobalOpenTelemetry#getPropagators()

* Code review comments