Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -6,9 +6,15 @@
package com.example.javaagent.instrumentation;

import com.example.javaagent.bootstrap.AgentApi;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.ServletResponse;

public final class DemoServlet3HelperClass {

public static final VirtualField<ServletResponse, AtomicInteger> VIRTUAL_FIELD =
VirtualField.find(ServletResponse.class, AtomicInteger.class);

public static void doSomething(int number) {
// call the api in bootstrap class loader
AgentApi.doSomething(number);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand Down Expand Up @@ -45,12 +44,10 @@ public static class DemoServlet3Advice {
public static void onEnter(@Advice.Argument(value = 1) ServletResponse response) {
// VirtualField depends on muzzle-generation. Using it here to verify that muzzle-generation
// was set up.
VirtualField<ServletResponse, AtomicInteger> virtualField =
VirtualField.find(ServletResponse.class, AtomicInteger.class);
AtomicInteger counter = virtualField.get(response);
AtomicInteger counter = DemoServlet3HelperClass.VIRTUAL_FIELD.get(response);
if (counter == null) {
counter = new AtomicInteger();
virtualField.set(response, counter);
DemoServlet3HelperClass.VIRTUAL_FIELD.set(response, counter);
}
DemoServlet3HelperClass.doSomething(counter.incrementAndGet());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

public final class Helpers {

private static final VirtualField<ChannelHandler, ChannelHandler> CHANNEL_HANDLER =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);

private Helpers() {}

public static final Local<Context> CONTEXT_LOCAL = new Local<>();
Expand All @@ -52,8 +55,7 @@ protected void initChannel(C channel) throws Exception {
channel.pipeline().context(Http2StreamFrameToHttpObjectCodec.class);
if (codecCtx != null) {
if (channel.pipeline().get(HttpServerTracingHandler.class) == null) {
VirtualField<ChannelHandler, ChannelHandler> virtualField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);

ChannelHandler ourHandler =
NettyServerSingletons.serverTelemetry()
.createCombinedHandler(NettyHttpServerResponseBeforeCommitHandler.INSTANCE);
Expand All @@ -62,7 +64,7 @@ protected void initChannel(C channel) throws Exception {
.pipeline()
.addAfter(codecCtx.name(), ourHandler.getClass().getName(), ourHandler);
// attach this in this way to match up with how netty instrumentation expects things
virtualField.set(codecCtx.handler(), ourHandler);
CHANNEL_HANDLER.set(codecCtx.handler(), ourHandler);
}
}
}
Expand Down Expand Up @@ -93,15 +95,13 @@ protected void initChannel(C channel) throws Exception {
channel.pipeline().context(Http2StreamFrameToHttpObjectCodec.class);
if (codecCtx != null) {
if (channel.pipeline().get(HttpClientTracingHandler.class) == null) {
VirtualField<ChannelHandler, ChannelHandler> virtualField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
ChannelHandler ourHandler = clientHandlerFactory().createCombinedHandler();

channel
.pipeline()
.addAfter(codecCtx.name(), ourHandler.getClass().getName(), ourHandler);
// attach this in this way to match up with how netty instrumentation expects things
virtualField.set(codecCtx.handler(), ourHandler);
CHANNEL_HANDLER.set(codecCtx.handler(), ourHandler);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.netty.v3_8.VirtualFieldHelper.CONNECTION_CONTEXT;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;

public class ChannelFutureListenerInstrumentation implements TypeInstrumentation {
Expand Down Expand Up @@ -58,10 +57,7 @@ public static Scope activateScope(@Advice.Argument(0) ChannelFuture future) {
return null;
}

VirtualField<Channel, NettyConnectionContext> virtualField =
VirtualField.find(Channel.class, NettyConnectionContext.class);

NettyConnectionContext connectionContext = virtualField.get(future.getChannel());
NettyConnectionContext connectionContext = CONNECTION_CONTEXT.get(future.getChannel());
if (connectionContext == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.netty.v3_8.VirtualFieldHelper.CONNECTION_CONTEXT;
import static io.opentelemetry.javaagent.instrumentation.netty.v3_8.client.NettyClientSingletons.connectionInstrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
Expand All @@ -16,7 +17,6 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.internal.InstrumenterUtil;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.instrumentation.netty.common.internal.NettyConnectionRequest;
import io.opentelemetry.instrumentation.netty.common.internal.Timer;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
Expand Down Expand Up @@ -65,12 +65,10 @@ public static NettyScope onEnter(
return null;
}

VirtualField<Channel, NettyConnectionContext> virtualField =
VirtualField.find(Channel.class, NettyConnectionContext.class);
if (virtualField.get(channel) != null) {
if (CONNECTION_CONTEXT.get(channel) != null) {
return null;
}
virtualField.set(channel, new NettyConnectionContext(parentContext));
CONNECTION_CONTEXT.set(channel, new NettyConnectionContext(parentContext));

NettyConnectionRequest request = NettyConnectionRequest.connect(remoteAddress);
Timer timer = Timer.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
new NettyChannelPipelineInstrumentation(),
new DefaultChannelPipelineInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.netty.v3_8;

import io.opentelemetry.instrumentation.api.util.VirtualField;
import org.jboss.netty.channel.Channel;

public class VirtualFieldHelper {

public static final VirtualField<Channel, NettyConnectionContext> CONNECTION_CONTEXT =
VirtualField.find(Channel.class, NettyConnectionContext.class);

private VirtualFieldHelper() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.javaagent.instrumentation.netty.v4_0;

import static io.opentelemetry.javaagent.instrumentation.netty.v4.common.VirtualFieldHelper.CHANNEL_HANDLER;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_0.client.NettyClientSingletons.sslInstrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
Expand All @@ -19,7 +20,6 @@
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpServerCodec;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.instrumentation.netty.common.v4_0.internal.client.NettySslInstrumentationHandler;
import io.opentelemetry.javaagent.bootstrap.CallDepth;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand Down Expand Up @@ -70,11 +70,8 @@ public static void addHandler(
return;
}

VirtualField<ChannelHandler, ChannelHandler> instrumentationHandlerField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);

// don't add another instrumentation handler if there already is one attached
if (instrumentationHandlerField.get(handler) != null) {
if (CHANNEL_HANDLER.get(handler) != null) {
return;
}

Expand Down Expand Up @@ -103,7 +100,7 @@ public static void addHandler(
try {
pipeline.addLast(ourHandler.getClass().getName(), ourHandler);
// associate our handle with original handler so they could be removed together
instrumentationHandlerField.set(handler, ourHandler);
CHANNEL_HANDLER.set(handler, ourHandler);
} catch (IllegalArgumentException e) {
// Prevented adding duplicate handlers.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
new NettyChannelPipelineInstrumentation(),
new AbstractChannelHandlerContextInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.javaagent.instrumentation.netty.v4_1;

import static io.opentelemetry.javaagent.instrumentation.netty.v4.common.VirtualFieldHelper.CHANNEL_HANDLER;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_1.NettyClientSingletons.clientHandlerFactory;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_1.NettyClientSingletons.sslInstrumenter;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_1.NettyServerSingletons.serverTelemetry;
Expand All @@ -22,7 +23,6 @@
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpServerCodec;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.instrumentation.netty.common.v4_0.internal.client.NettySslInstrumentationHandler;
import io.opentelemetry.javaagent.bootstrap.CallDepth;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand Down Expand Up @@ -78,11 +78,8 @@ public static void addHandler(
return;
}

VirtualField<ChannelHandler, ChannelHandler> instrumentationHandlerField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);

// don't add another instrumentation handler if there already is one attached
if (instrumentationHandlerField.get(handler) != null) {
if (CHANNEL_HANDLER.get(handler) != null) {
return;
}

Expand Down Expand Up @@ -126,7 +123,7 @@ public static void addHandler(
try {
pipeline.addAfter(name, ourHandler.getClass().getName(), ourHandler);
// associate our handle with original handler so they could be removed together
instrumentationHandlerField.set(handler, ourHandler);
CHANNEL_HANDLER.set(handler, ourHandler);
} catch (IllegalArgumentException e) {
// Prevented adding duplicate handlers.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
new AbstractChannelHandlerContextInstrumentation(),
new SingleThreadEventExecutorInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.netty.v4.common.VirtualFieldHelper.CHANNEL_HANDLER;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
Expand All @@ -16,7 +17,6 @@

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.util.Iterator;
Expand Down Expand Up @@ -74,14 +74,12 @@ public static class RemoveAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void removeHandler(
@Advice.This ChannelPipeline pipeline, @Advice.Argument(0) ChannelHandler handler) {
VirtualField<ChannelHandler, ChannelHandler> virtualField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
ChannelHandler ourHandler = virtualField.get(handler);
ChannelHandler ourHandler = CHANNEL_HANDLER.get(handler);
if (ourHandler != null) {
if (pipeline.context(ourHandler) != null) {
pipeline.remove(ourHandler);
}
virtualField.set(handler, null);
CHANNEL_HANDLER.set(handler, null);
}
}
}
Expand All @@ -97,14 +95,12 @@ public static void removeHandler(
return;
}

VirtualField<ChannelHandler, ChannelHandler> virtualField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
ChannelHandler ourHandler = virtualField.get(handler);
ChannelHandler ourHandler = CHANNEL_HANDLER.get(handler);
if (ourHandler != null) {
if (pipeline.context(ourHandler) != null) {
pipeline.remove(ourHandler);
}
virtualField.set(handler, null);
CHANNEL_HANDLER.set(handler, null);
}
}
}
Expand All @@ -121,14 +117,12 @@ public static void removeHandler(
return;
}

VirtualField<ChannelHandler, ChannelHandler> virtualField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
ChannelHandler ourHandler = virtualField.get(handler);
ChannelHandler ourHandler = CHANNEL_HANDLER.get(handler);
if (ourHandler != null) {
if (pipeline.context(ourHandler) != null) {
pipeline.remove(ourHandler);
}
virtualField.set(handler, null);
CHANNEL_HANDLER.set(handler, null);
}
}
}
Expand All @@ -139,14 +133,12 @@ public static class RemoveFirstAdvice {
@Advice.OnMethodExit(suppress = Throwable.class)
public static void removeHandler(
@Advice.This ChannelPipeline pipeline, @Advice.Return ChannelHandler handler) {
VirtualField<ChannelHandler, ChannelHandler> virtualField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
ChannelHandler ourHandler = virtualField.get(handler);
ChannelHandler ourHandler = CHANNEL_HANDLER.get(handler);
if (ourHandler != null) {
if (pipeline.context(ourHandler) != null) {
pipeline.remove(ourHandler);
}
virtualField.set(handler, null);
CHANNEL_HANDLER.set(handler, null);
}
}
}
Expand All @@ -158,19 +150,17 @@ public static class RemoveLastAdvice {
@Advice.AssignReturned.ToReturned
public static ChannelHandler removeHandler(
@Advice.This ChannelPipeline pipeline, @Advice.Return ChannelHandler returnHandler) {
VirtualField<ChannelHandler, ChannelHandler> virtualField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
// TODO remove this extra variable when migrating to "indy only" instrumentation.
ChannelHandler handler = returnHandler;
ChannelHandler ourHandler = virtualField.get(handler);
ChannelHandler ourHandler = CHANNEL_HANDLER.get(handler);
if (ourHandler != null) {
// Context is null when our handler has already been removed. This happens when calling
// removeLast first removed our handler and we called removeLast again to remove the http
// handler.
if (pipeline.context(ourHandler) != null) {
pipeline.remove(ourHandler);
}
virtualField.set(handler, null);
CHANNEL_HANDLER.set(handler, null);
} else {
String handlerClassName = handler.getClass().getName();
if (handlerClassName.endsWith("TracingHandler")
Expand All @@ -197,9 +187,7 @@ public static String addAfterHandler(
String name = nameArg;
ChannelHandler handler = pipeline.get(name);
if (handler != null) {
VirtualField<ChannelHandler, ChannelHandler> virtualField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
ChannelHandler ourHandler = virtualField.get(handler);
ChannelHandler ourHandler = CHANNEL_HANDLER.get(handler);
if (ourHandler != null) {
name = ourHandler.getClass().getName();
}
Expand All @@ -213,8 +201,6 @@ public static class ToMapAdvice {

@Advice.OnMethodExit(suppress = Throwable.class)
public static void wrapIterator(@Advice.Return Map<String, ChannelHandler> map) {
VirtualField<ChannelHandler, ChannelHandler> virtualField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
for (Iterator<ChannelHandler> iterator = map.values().iterator(); iterator.hasNext(); ) {
ChannelHandler handler = iterator.next();
String handlerClassName = handler.getClass().getName();
Expand Down
Loading
Loading