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
@@ -0,0 +1,46 @@
package datadog.trace.bootstrap;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Utility to track nested instrumentation.
*
* <p>For example, this can be used to track nested calls to super() in constructors by calling
* #incrementCallDepth at the beginning of each constructor.
*/
public class CallDepthThreadLocalMap {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you want to remove the CallDepthThreadLocalMap from the classloader instrumentation and replace it with this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍 Yeah, I'll go ahead and do that.

private static final ThreadLocal<Map<Object, CallDepthThreadLocalMap>> INSTANCES =
new ThreadLocal<>();

public static CallDepthThreadLocalMap get(Object o) {
if (INSTANCES.get() == null) {
INSTANCES.set(new WeakHashMap<Object, CallDepthThreadLocalMap>());
}
if (!INSTANCES.get().containsKey(o)) {
INSTANCES.get().put(o, new CallDepthThreadLocalMap());
}
return INSTANCES.get().get(o);
}

private final ThreadLocal<AtomicInteger> tls = new ThreadLocal<>();

private CallDepthThreadLocalMap() {}

public int incrementCallDepth() {
AtomicInteger depth = tls.get();
if (depth == null) {
depth = new AtomicInteger(0);
tls.set(depth);
return 0;
} else {
return depth.incrementAndGet();
}
}

public void reset() {
tls.remove();
INSTANCES.get().remove(this);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import datadog.opentracing.DDTracer;
import datadog.trace.agent.tooling.DDAdvice;
import datadog.trace.agent.tooling.DDTransformers;
import datadog.trace.agent.tooling.HelperInjector;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import io.opentracing.util.GlobalTracer;
import java.lang.reflect.Field;
import net.bytebuddy.agent.builder.AgentBuilder;
Expand All @@ -34,9 +34,6 @@ public AgentBuilder apply(final AgentBuilder agentBuilder) {
.type(
failSafe(isSubTypeOf(ClassLoader.class)),
classLoaderHasClasses("io.opentracing.util.GlobalTracer"))
.transform(
new HelperInjector(
"datadog.trace.instrumentation.classloaders.CallDepthThreadLocalMap"))
.transform(DDTransformers.defaultTransformers())
.transform(DDAdvice.create().advice(isConstructor(), ClassloaderAdvice.class.getName()))
.asDecorator();
Expand All @@ -48,15 +45,15 @@ public static class ClassloaderAdvice {
public static int constructorEnter() {
// We use this to make sure we only apply the exit instrumentation
// after the constructors are done calling their super constructors.
return CallDepthThreadLocalMap.incrementCallDepth();
return CallDepthThreadLocalMap.get(ClassLoader.class).incrementCallDepth();
}

// Not sure why, but adding suppress causes a verify error.
@Advice.OnMethodExit // (suppress = Throwable.class)
public static void constructorExit(
@Advice.This final ClassLoader cl, @Advice.Enter final int depth) {
if (depth == 0) {
CallDepthThreadLocalMap.reset();
CallDepthThreadLocalMap.get(ClassLoader.class).reset();

try {
final Field field = GlobalTracer.class.getDeclaredField("tracer");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import datadog.trace.agent.tooling.DDAdvice;
import datadog.trace.agent.tooling.DDTransformers;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import datadog.trace.bootstrap.JDBCMaps;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;

Expand Down Expand Up @@ -52,9 +54,19 @@ public static void addDBInfo(
}

public static class ConnectionConstructorAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static int constructorEnter() {
// We use this to make sure we only apply the exit instrumentation
// after the constructors are done calling their super constructors.
return CallDepthThreadLocalMap.get(Connection.class).incrementCallDepth();
}

@Advice.OnMethodExit(suppress = Throwable.class)
public static void addDBInfo(@Advice.This final Connection connection) {
try {
public static void constructorExit(
@Advice.Enter final int depth, @Advice.This final Connection connection)
throws SQLException {
if (depth == 0) {
CallDepthThreadLocalMap.get(Connection.class).reset();
final String url = connection.getMetaData().getURL();
if (url != null) {
// Remove end of url to prevent passwords from leaking:
Expand All @@ -66,9 +78,6 @@ public static void addDBInfo(@Advice.This final Connection connection) {
}
JDBCMaps.connectionInfo.put(connection, new JDBCMaps.DBInfo(sanitizedURL, type, user));
}
} catch (final Throwable t) {
// object may not be fully initialized.
// calling constructor will populate map
}
}
}
Expand Down