From 77ad1cfccd1821a1e3498a541e6390eab687303b Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Mon, 27 Mar 2023 22:15:04 +0200 Subject: [PATCH 1/6] Factor out reference counting to specified API. --- ...> ReferencedCountedConcurrentHashMap.java} | 12 +++--- .../WeakConcurrentProviderImpl.java | 6 +-- .../collections/WeakMapReferenceCounter.java | 38 +++++++++++++++++++ .../apm/agent/impl/ElasticApmTracer.java | 9 +++++ ...ferencedCountedConcurrentHashMapTest.java} | 14 +++---- ...bstractAsyncHttpClientInstrumentation.java | 5 +-- .../agent/dubbo/AlibabaCallbackHolder.java | 6 +-- .../co/elastic/apm/agent/grpc/GrpcHelper.java | 25 ++++++------ .../apm/agent/concurrent/JavaConcurrent.java | 6 +-- .../apm/agent/process/ProcessHelper.java | 12 +++--- .../apm/agent/process/ProcessHelperTest.java | 3 +- .../apm/agent/reactor/TracedSubscriber.java | 5 ++- .../lettuce/Lettuce34Instrumentation.java | 5 +-- .../Lettuce5StartSpanInstrumentation.java | 5 +-- .../FutureInstrumentation.java | 5 +-- .../springwebclient/WebClientSubscriber.java | 6 +-- .../TransactionAwareSubscriber.java | 6 +-- .../HttpUrlConnectionInstrumentation.java | 3 +- .../apm/agent/vertx/v3/web/WebHelper.java | 3 +- .../apm/agent/tracer/AbstractSpan.java | 8 +--- .../apm/agent/tracer/GlobalTracer.java | 7 ++++ .../elastic/apm/agent/tracer/NoopTracer.java | 7 ++++ .../co/elastic/apm/agent/tracer/Tracer.java | 4 ++ .../tracer/reference/ReferenceCounted.java | 8 ++++ .../tracer/reference/ReferenceCounter.java | 16 ++++++++ 25 files changed, 155 insertions(+), 69 deletions(-) rename apm-agent-core/src/main/java/co/elastic/apm/agent/collections/{SpanConcurrentHashMap.java => ReferencedCountedConcurrentHashMap.java} (83%) create mode 100644 apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakMapReferenceCounter.java rename apm-agent-core/src/test/java/co/elastic/apm/agent/collections/{SpanConcurrentHashMapTest.java => ReferencedCountedConcurrentHashMapTest.java} (95%) create mode 100644 apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounted.java create mode 100644 apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounter.java diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/SpanConcurrentHashMap.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/ReferencedCountedConcurrentHashMap.java similarity index 83% rename from apm-agent-core/src/main/java/co/elastic/apm/agent/collections/SpanConcurrentHashMap.java rename to apm-agent-core/src/main/java/co/elastic/apm/agent/collections/ReferencedCountedConcurrentHashMap.java index bf44500987..272ef741f7 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/SpanConcurrentHashMap.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/ReferencedCountedConcurrentHashMap.java @@ -18,14 +18,14 @@ */ package co.elastic.apm.agent.collections; -import co.elastic.apm.agent.tracer.AbstractSpan; +import co.elastic.apm.agent.tracer.reference.ReferenceCounted; import javax.annotation.Nullable; import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; /** - * Hash map dedicated to storage of in-flight spans and transactions, reference count is being incremented/decremented + * Hash map dedicated to storage of reference counted objects where counts are being incremented/decremented * when entry is added/removed. Usage of this map is intended for providing GC-based storage of context associated * to a framework-level object key, when the latter is collected by GC it allows to decrement and then recycle the * span/transaction. @@ -33,9 +33,9 @@ * @param key type * @param context type */ -public class SpanConcurrentHashMap> extends ConcurrentHashMap { +public class ReferencedCountedConcurrentHashMap extends ConcurrentHashMap { - SpanConcurrentHashMap() { + ReferencedCountedConcurrentHashMap() { } @Nullable @@ -71,7 +71,7 @@ public void clear() { super.clear(); } - private void onPut(@Nullable AbstractSpan previous, AbstractSpan value) { + private void onPut(@Nullable ReferenceCounted previous, ReferenceCounted value) { if (previous == null) { // new entry value.incrementReferences(); @@ -82,7 +82,7 @@ private void onPut(@Nullable AbstractSpan previous, AbstractSpan value) { } } - private void onRemove(@Nullable AbstractSpan removed) { + private void onRemove(@Nullable ReferenceCounted removed) { if (removed == null) { return; } diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakConcurrentProviderImpl.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakConcurrentProviderImpl.java index bbeb057509..593d3c280a 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakConcurrentProviderImpl.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakConcurrentProviderImpl.java @@ -22,7 +22,7 @@ import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.sdk.weakconcurrent.WeakSet; -import co.elastic.apm.agent.tracer.AbstractSpan; +import co.elastic.apm.agent.tracer.reference.ReferenceCounted; import com.blogspot.mydailyjava.weaklockfree.AbstractWeakConcurrentMap; import com.blogspot.mydailyjava.weaklockfree.WeakConcurrentSet; @@ -37,8 +37,8 @@ public class WeakConcurrentProviderImpl implements WeakConcurrent.WeakConcurrent private static final WeakConcurrentSet> registeredMaps = new WeakConcurrentSet<>(WeakConcurrentSet.Cleaner.INLINE); - public static > WeakMap createWeakSpanMap() { - SpanConcurrentHashMap, V> map = new SpanConcurrentHashMap<>(); + public static WeakMap createWeakReferenceCountedMap() { + ReferencedCountedConcurrentHashMap, V> map = new ReferencedCountedConcurrentHashMap<>(); NullSafeWeakConcurrentMap result = new NullSafeWeakConcurrentMap<>(map); registeredMaps.add(result); return result; diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakMapReferenceCounter.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakMapReferenceCounter.java new file mode 100644 index 0000000000..17f633dc2f --- /dev/null +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakMapReferenceCounter.java @@ -0,0 +1,38 @@ +package co.elastic.apm.agent.collections; + +import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.reference.ReferenceCounted; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; + +import javax.annotation.Nullable; + +public class WeakMapReferenceCounter implements ReferenceCounter { + + private final WeakMap map; + + public WeakMapReferenceCounter(WeakMap map) { + this.map = map; + } + + @Override + @Nullable + public V get(K key) { + return map.get(key); + } + + @Override + public boolean contains(K key) { + return map.containsKey(key); + } + + @Override + public void put(K key, V value) { + map.put(key, value); + } + + @Override + @Nullable + public V remove(K key) { + return map.remove(key); + } +} diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java index 20ca144b66..d1c1e00218 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java @@ -18,6 +18,8 @@ */ package co.elastic.apm.agent.impl; +import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; +import co.elastic.apm.agent.collections.WeakMapReferenceCounter; import co.elastic.apm.agent.common.JvmRuntimeInfo; import co.elastic.apm.agent.configuration.CoreConfiguration; import co.elastic.apm.agent.configuration.ServiceInfo; @@ -47,6 +49,8 @@ import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.GlobalTracer; +import co.elastic.apm.agent.tracer.reference.ReferenceCounted; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import co.elastic.apm.agent.util.DependencyInjectingServiceLoader; import co.elastic.apm.agent.util.ExecutorUtils; import co.elastic.apm.agent.tracer.Scope; @@ -580,6 +584,11 @@ public ObjectPoolFactory getObjectPoolFactory() { return objectPoolFactory; } + @Override + public ReferenceCounter createReferenceCounter() { + return new WeakMapReferenceCounter<>(WeakConcurrentProviderImpl.createWeakReferenceCountedMap()); + } + @Override @Nullable public AbstractSpan getActive() { diff --git a/apm-agent-core/src/test/java/co/elastic/apm/agent/collections/SpanConcurrentHashMapTest.java b/apm-agent-core/src/test/java/co/elastic/apm/agent/collections/ReferencedCountedConcurrentHashMapTest.java similarity index 95% rename from apm-agent-core/src/test/java/co/elastic/apm/agent/collections/SpanConcurrentHashMapTest.java rename to apm-agent-core/src/test/java/co/elastic/apm/agent/collections/ReferencedCountedConcurrentHashMapTest.java index bddbb01c32..b75311eb4e 100644 --- a/apm-agent-core/src/test/java/co/elastic/apm/agent/collections/SpanConcurrentHashMapTest.java +++ b/apm-agent-core/src/test/java/co/elastic/apm/agent/collections/ReferencedCountedConcurrentHashMapTest.java @@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; -class SpanConcurrentHashMapTest { +class ReferencedCountedConcurrentHashMapTest { @Nullable private Object key; @@ -45,7 +45,7 @@ void putRemove() { checkRefCount(testSpan, 0); key = new Object(); - WeakMap map = WeakConcurrentProviderImpl.createWeakSpanMap(); + WeakMap map = WeakConcurrentProviderImpl.createWeakReferenceCountedMap(); map.put(key, testSpan); checkRefCount(testSpan, 1); @@ -63,7 +63,7 @@ void putRemove() { void putTwice(PutOperation operation) { TestSpan testSpan = new TestSpan(); key = new Object(); - WeakMap map = WeakConcurrentProviderImpl.createWeakSpanMap(); + WeakMap map = WeakConcurrentProviderImpl.createWeakReferenceCountedMap(); checkRefCount(testSpan, 0); @@ -82,7 +82,7 @@ void swapValues() { TestSpan ts2 = new TestSpan(); key = new Object(); - WeakMap map = WeakConcurrentProviderImpl.createWeakSpanMap(); + WeakMap map = WeakConcurrentProviderImpl.createWeakReferenceCountedMap(); map.put(key, ts1); map.put(key, ts2); @@ -99,7 +99,7 @@ void testPutIfAbsent() { TestSpan ts2 = new TestSpan(); key = new Object(); - WeakMap map = WeakConcurrentProviderImpl.createWeakSpanMap(); + WeakMap map = WeakConcurrentProviderImpl.createWeakReferenceCountedMap(); map.putIfAbsent(key, ts1); map.putIfAbsent(key, ts2); @@ -130,7 +130,7 @@ void execute(WeakMap map, Object key, TestSpan value) { @Test void clear() { - WeakMap map = WeakConcurrentProviderImpl.createWeakSpanMap(); + WeakMap map = WeakConcurrentProviderImpl.createWeakReferenceCountedMap(); List> list = new ArrayList<>(); for (int i = 0; i < 5; i++) { @@ -153,7 +153,7 @@ void weakMapDecrementOnStaleKeyGC() { key = new Object(); TestSpan span = new TestSpan(); - WeakMap> map = WeakConcurrentProviderImpl.createWeakSpanMap(); + WeakMap> map = WeakConcurrentProviderImpl.createWeakReferenceCountedMap(); map.put(key, span); diff --git a/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java b/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java index db5416f51e..516c3109d4 100644 --- a/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java +++ b/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java @@ -19,14 +19,13 @@ package co.elastic.apm.agent.asynchttpclient; import co.elastic.apm.agent.bci.TracerAwareInstrumentation; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.httpclient.HttpClientHelper; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.sdk.DynamicTransformer; import co.elastic.apm.agent.sdk.ElasticApmInstrumentation; -import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; @@ -54,7 +53,7 @@ public abstract class AbstractAsyncHttpClientInstrumentation extends TracerAware public static class Helper { - static final WeakMap, Span> handlerSpanMap = WeakConcurrentProviderImpl.createWeakSpanMap(); + static final ReferenceCounter, Span> handlerSpanMap = tracer.createReferenceCounter(); public static final List> ASYNC_HANDLER_INSTRUMENTATIONS = Arrays.>asList( AsyncHandlerOnCompletedInstrumentation.class, diff --git a/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java b/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java index 482fc1221e..3e2f0b8adf 100644 --- a/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java +++ b/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java @@ -18,11 +18,11 @@ */ package co.elastic.apm.agent.dubbo; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.tracer.AbstractSpan; -import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.GlobalTracer; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import com.alibaba.dubbo.remoting.exchange.ResponseCallback; public class AlibabaCallbackHolder { - public static final WeakMap> callbackSpanMap = WeakConcurrentProviderImpl.createWeakSpanMap(); + public static final ReferenceCounter> callbackSpanMap = GlobalTracer.get().createReferenceCounter(); } diff --git a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java index bd0a6b1114..5e2cdf96c8 100644 --- a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java +++ b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java @@ -18,7 +18,6 @@ */ package co.elastic.apm.agent.grpc; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Outcome; @@ -31,6 +30,7 @@ import co.elastic.apm.agent.tracer.dispatch.AbstractHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import io.grpc.CallOptions; import io.grpc.ClientCall; import io.grpc.Metadata; @@ -58,27 +58,27 @@ public static GrpcHelper getInstance() { /** * Map of all in-flight {@link Span} with {@link ClientCall} instance as key. */ - private final WeakMap, Span> clientCallSpans; + private final ReferenceCounter, Span> clientCallSpans; /** * Map of all in-flight {@link Span} with {@link ClientCall} instance as key. */ - private final WeakMap, Span> delayedClientCallSpans; + private final ReferenceCounter, Span> delayedClientCallSpans; /** * Map of all in-flight {@link Span} with {@link ClientCall.Listener} instance as key. */ - private final WeakMap, Span> clientCallListenerSpans; + private final ReferenceCounter, Span> clientCallListenerSpans; /** * Map of all in-flight {@link Transaction} with {@link ServerCall.Listener} instance as key. */ - private final WeakMap, Transaction> serverListenerTransactions; + private final ReferenceCounter, Transaction> serverListenerTransactions; /** * Map of all in-flight {@link Transaction} with {@link ServerCall} instance as key. */ - private final WeakMap, Transaction> serverCallTransactions; + private final ReferenceCounter, Transaction> serverCallTransactions; /** * gRPC header cache used to minimize allocations @@ -89,12 +89,13 @@ public static GrpcHelper getInstance() { private final TextHeaderGetter headerGetter; public GrpcHelper() { - clientCallSpans = WeakConcurrentProviderImpl.createWeakSpanMap(); - delayedClientCallSpans = WeakConcurrentProviderImpl.createWeakSpanMap(); - clientCallListenerSpans = WeakConcurrentProviderImpl.createWeakSpanMap(); + Tracer tracer = GlobalTracer.get(); + clientCallSpans = tracer.createReferenceCounter(); + delayedClientCallSpans = tracer.createReferenceCounter(); + clientCallListenerSpans = tracer.createReferenceCounter(); - serverListenerTransactions = WeakConcurrentProviderImpl.createWeakSpanMap(); - serverCallTransactions = WeakConcurrentProviderImpl.createWeakSpanMap(); + serverListenerTransactions = tracer.createReferenceCounter(); + serverCallTransactions = tracer.createReferenceCounter(); headerCache = WeakConcurrent.buildMap(); @@ -476,7 +477,7 @@ public void clientCallStartExit(@Nullable Span spanFromEntry, ClientCall.List } public void cancelCall(ClientCall clientCall, @Nullable Throwable cause) { - WeakMap, Span> clientCallMap = (isDelayedClientCall(clientCall)) ? delayedClientCallSpans : clientCallSpans; + ReferenceCounter, Span> clientCallMap = (isDelayedClientCall(clientCall)) ? delayedClientCallSpans : clientCallSpans; // we can't remove yet, in order to avoid reference decrement prematurely Span span = clientCallMap.get(clientCall); if (span != null) { diff --git a/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java b/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java index 63a7bfa1a7..cdcfd588c7 100644 --- a/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java +++ b/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java @@ -18,14 +18,14 @@ */ package co.elastic.apm.agent.concurrent; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.common.ThreadUtils; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.sdk.DynamicTransformer; import co.elastic.apm.agent.sdk.ElasticApmInstrumentation; import co.elastic.apm.agent.sdk.state.GlobalState; -import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Tracer; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import javax.annotation.Nullable; import java.util.ArrayList; @@ -42,7 +42,7 @@ @GlobalState public class JavaConcurrent { - private static final WeakMap> contextMap = WeakConcurrentProviderImpl.createWeakSpanMap(); + private static final ReferenceCounter> contextMap = GlobalTracer.get().createReferenceCounter(); private static final List> RUNNABLE_CALLABLE_FJTASK_INSTRUMENTATION = Collections. >singletonList(RunnableCallableForkJoinTaskInstrumentation.class); diff --git a/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java b/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java index cae224ae09..c839b4aef2 100644 --- a/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java +++ b/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java @@ -18,12 +18,12 @@ */ package co.elastic.apm.agent.process; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.tracer.AbstractSpan; +import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.sdk.state.GlobalVariables; -import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import javax.annotation.Nonnull; import java.util.List; @@ -34,7 +34,7 @@ */ class ProcessHelper { - private static final ProcessHelper INSTANCE = new ProcessHelper(WeakConcurrentProviderImpl.>createWeakSpanMap()); + private static final ProcessHelper INSTANCE = new ProcessHelper(GlobalTracer.get().>createReferenceCounter()); /** * A thread local used to indicate whether the currently invoked instrumented method is invoked by the plugin itself. @@ -44,9 +44,9 @@ class ProcessHelper { */ private static final ThreadLocal inTracingContext = GlobalVariables.get(ProcessHelper.class, "inTracingContext", new ThreadLocal()); - private final WeakMap> inFlightSpans; + private final ReferenceCounter> inFlightSpans; - ProcessHelper(WeakMap> inFlightSpans) { + ProcessHelper(ReferenceCounter> inFlightSpans) { this.inFlightSpans = inFlightSpans; } @@ -74,7 +74,7 @@ static void endProcessSpan(@Nonnull Process process, int exitValue) { * @param processName process name */ void doStartProcess(@Nonnull AbstractSpan parentContext, @Nonnull Process process, @Nonnull String processName) { - if (inFlightSpans.containsKey(process)) { + if (inFlightSpans.contains(process)) { return; } diff --git a/apm-agent-plugins/apm-process-plugin/src/test/java/co/elastic/apm/agent/process/ProcessHelperTest.java b/apm-agent-plugins/apm-process-plugin/src/test/java/co/elastic/apm/agent/process/ProcessHelperTest.java index ad6c06f4c0..63daf10437 100644 --- a/apm-agent-plugins/apm-process-plugin/src/test/java/co/elastic/apm/agent/process/ProcessHelperTest.java +++ b/apm-agent-plugins/apm-process-plugin/src/test/java/co/elastic/apm/agent/process/ProcessHelperTest.java @@ -20,6 +20,7 @@ import co.elastic.apm.agent.AbstractInstrumentationTest; import co.elastic.apm.agent.TransactionUtils; +import co.elastic.apm.agent.collections.WeakMapReferenceCounter; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.impl.transaction.Span; @@ -57,7 +58,7 @@ void before() { TransactionUtils.fillTransaction(transaction); storageMap = WeakConcurrent.buildMap(); - helper = new ProcessHelper(storageMap); + helper = new ProcessHelper(new WeakMapReferenceCounter<>(storageMap)); } @Test diff --git a/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java b/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java index 3b6b64b505..ac31a082e7 100644 --- a/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java +++ b/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java @@ -18,14 +18,15 @@ */ package co.elastic.apm.agent.reactor; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; import co.elastic.apm.agent.sdk.state.GlobalVariables; import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Tracer; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; @@ -46,7 +47,7 @@ public class TracedSubscriber implements CoreSubscriber { private static final AtomicBoolean isRegistered = GlobalVariables.get(ReactorInstrumentation.class, "reactor-hook-enabled", new AtomicBoolean(false)); - private static final WeakMap, AbstractSpan> contextMap = WeakConcurrentProviderImpl.createWeakSpanMap(); + private static final ReferenceCounter, AbstractSpan> contextMap = GlobalTracer.get().createReferenceCounter(); private static final String HOOK_KEY = "elastic-apm"; diff --git a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java index 8dbe9a99e0..66410fe137 100644 --- a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java +++ b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java @@ -19,9 +19,8 @@ package co.elastic.apm.agent.lettuce; import co.elastic.apm.agent.bci.TracerAwareInstrumentation; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import com.lambdaworks.redis.protocol.RedisCommand; import net.bytebuddy.matcher.ElementMatcher; @@ -32,7 +31,7 @@ public abstract class Lettuce34Instrumentation extends TracerAwareInstrumentation { - static final WeakMap, Span> commandToSpan = WeakConcurrentProviderImpl.createWeakSpanMap(); + static final ReferenceCounter, Span> commandToSpan = tracer.createReferenceCounter(); /** * We don't support Lettuce up to version 3.3, as the {@link RedisCommand#getType()} method is missing diff --git a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java index 6a32641b89..9f9a616ee0 100644 --- a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java +++ b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java @@ -19,10 +19,9 @@ package co.elastic.apm.agent.lettuce; import co.elastic.apm.agent.bci.TracerAwareInstrumentation; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.redis.RedisSpanUtils; -import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import io.lettuce.core.protocol.RedisCommand; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; @@ -46,7 +45,7 @@ */ public class Lettuce5StartSpanInstrumentation extends TracerAwareInstrumentation { - static final WeakMap, Span> commandToSpan = WeakConcurrentProviderImpl.createWeakSpanMap(); + static final ReferenceCounter, Span> commandToSpan = tracer.createReferenceCounter(); @Override public ElementMatcher getTypeMatcher() { diff --git a/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java b/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java index 5fa4cd7bc5..255ad89be4 100644 --- a/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java +++ b/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java @@ -19,9 +19,8 @@ package co.elastic.apm.agent.scalaconcurrent; import co.elastic.apm.agent.bci.TracerAwareInstrumentation; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.tracer.AbstractSpan; -import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; @@ -39,7 +38,7 @@ public abstract class FutureInstrumentation extends TracerAwareInstrumentation { @SuppressWarnings("WeakerAccess") - public static final WeakMap> promisesToContext = WeakConcurrentProviderImpl.createWeakSpanMap(); + public static final ReferenceCounter> promisesToContext = tracer.createReferenceCounter(); @Nonnull @Override diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java index 53d9a90529..c69ad07163 100755 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java @@ -18,11 +18,11 @@ */ package co.elastic.apm.agent.springwebclient; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.impl.context.web.ResultUtil; +import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.Tracer; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import org.reactivestreams.Subscription; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,7 +35,7 @@ public class WebClientSubscriber implements CoreSubscriber, Subscription { private static final Logger logger = LoggerFactory.getLogger(WebClientSubscriber.class); - private static final WeakMap, Span> spanMap = WeakConcurrentProviderImpl.createWeakSpanMap(); + private static final ReferenceCounter, Span> spanMap = GlobalTracer.get().createReferenceCounter(); private final Tracer tracer; private final CoreSubscriber subscriber; diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-plugin/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-plugin/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java index 12b9cac98d..cff574a217 100644 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-plugin/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-plugin/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java @@ -18,10 +18,10 @@ */ package co.elastic.apm.agent.springwebflux; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.tracer.AbstractSpan; +import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Transaction; -import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import co.elastic.apm.agent.sdk.logging.Logger; @@ -41,7 +41,7 @@ class TransactionAwareSubscriber implements CoreSubscriber, Subscription { private static final Logger log = LoggerFactory.getLogger(TransactionAwareSubscriber.class); - private static final WeakMap, Transaction> transactionMap = WeakConcurrentProviderImpl.createWeakSpanMap(); + private static final ReferenceCounter, Transaction> transactionMap = GlobalTracer.get().createReferenceCounter(); private final CoreSubscriber subscriber; diff --git a/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java b/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java index dfcc125770..1a00636832 100644 --- a/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java +++ b/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java @@ -28,6 +28,7 @@ import co.elastic.apm.agent.sdk.state.CallDepth; import co.elastic.apm.agent.sdk.state.GlobalState; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -50,7 +51,7 @@ @GlobalState public abstract class HttpUrlConnectionInstrumentation extends TracerAwareInstrumentation { - public static final WeakMap inFlightSpans = WeakConcurrentProviderImpl.createWeakSpanMap(); + public static final ReferenceCounter> inFlightSpans = tracer.createReferenceCounter(); public static final CallDepth callDepth = CallDepth.get(HttpUrlConnectionInstrumentation.class); @Override diff --git a/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java b/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java index a7f787a44c..013baa193c 100644 --- a/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java +++ b/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java @@ -25,6 +25,7 @@ import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import co.elastic.apm.agent.vertx.AbstractVertxWebHelper; import io.vertx.core.Handler; import io.vertx.core.http.HttpServerRequest; @@ -38,7 +39,7 @@ public class WebHelper extends AbstractVertxWebHelper { private static final WebHelper INSTANCE = new WebHelper(GlobalTracer.get()); - static final WeakMap> transactionMap = WeakConcurrentProviderImpl.createWeakSpanMap(); + static final ReferenceCounter> transactionMap = GlobalTracer.get().createReferenceCounter(); public static WebHelper getInstance() { return INSTANCE; diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java index 2767ee206c..64fa766aad 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java @@ -20,13 +20,13 @@ import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderSetter; -import co.elastic.apm.agent.tracer.dispatch.HeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; +import co.elastic.apm.agent.tracer.reference.ReferenceCounted; import javax.annotation.Nullable; -public interface AbstractSpan> extends ElasticContext { +public interface AbstractSpan> extends ElasticContext, ReferenceCounted { AbstractContext getContext(); @@ -166,8 +166,4 @@ public interface AbstractSpan> extends ElasticContext< T withOutcome(Outcome outcome); T withSync(boolean sync); - - void incrementReferences(); - - void decrementReferences(); } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java index 0c96307ffd..e1fbeb3cb8 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java @@ -21,6 +21,8 @@ import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; +import co.elastic.apm.agent.tracer.reference.ReferenceCounted; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import javax.annotation.Nullable; @@ -81,6 +83,11 @@ public ObjectPoolFactory getObjectPoolFactory() { return tracer.getObjectPoolFactory(); } + @Override + public ReferenceCounter createReferenceCounter() { + return tracer.createReferenceCounter(); + } + @Nullable @Override public AbstractSpan getActive() { diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java index 3e9c980ec4..0aa0652572 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java @@ -21,6 +21,8 @@ import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; +import co.elastic.apm.agent.tracer.reference.ReferenceCounted; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import javax.annotation.Nullable; @@ -57,6 +59,11 @@ public ObjectPoolFactory getObjectPoolFactory() { throw new IllegalStateException(); } + @Override + public ReferenceCounter createReferenceCounter() { + throw new IllegalStateException(); + } + @Nullable @Override public AbstractSpan getActive() { diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java index abb6373bbe..2e7d67de9a 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java @@ -22,6 +22,8 @@ import co.elastic.apm.agent.tracer.dispatch.HeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; +import co.elastic.apm.agent.tracer.reference.ReferenceCounted; +import co.elastic.apm.agent.tracer.reference.ReferenceCounter; import javax.annotation.Nullable; @@ -38,6 +40,8 @@ public interface Tracer { ObjectPoolFactory getObjectPoolFactory(); + ReferenceCounter createReferenceCounter(); + @Nullable AbstractSpan getActive(); diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounted.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounted.java new file mode 100644 index 0000000000..a8865d3c87 --- /dev/null +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounted.java @@ -0,0 +1,8 @@ +package co.elastic.apm.agent.tracer.reference; + +public interface ReferenceCounted { + + void incrementReferences(); + + void decrementReferences(); +} diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounter.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounter.java new file mode 100644 index 0000000000..2f29523a99 --- /dev/null +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounter.java @@ -0,0 +1,16 @@ +package co.elastic.apm.agent.tracer.reference; + +import javax.annotation.Nullable; + +public interface ReferenceCounter { + + @Nullable + V get(K key); + + boolean contains(K key); + + void put(K key, V value); + + @Nullable + V remove(K key); +} From e2bd066c481608f43391ae36fb36aeb588df471d Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Tue, 28 Mar 2023 20:13:06 +0200 Subject: [PATCH 2/6] Add missing generic variables. --- .../main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java index d1c1e00218..bbeb287645 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java @@ -586,7 +586,7 @@ public ObjectPoolFactory getObjectPoolFactory() { @Override public ReferenceCounter createReferenceCounter() { - return new WeakMapReferenceCounter<>(WeakConcurrentProviderImpl.createWeakReferenceCountedMap()); + return new WeakMapReferenceCounter<>(WeakConcurrentProviderImpl.createWeakReferenceCountedMap()); } @Override From 0df1d8e2c8aac4981e2c3cb8529f19509acec5b6 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Tue, 28 Mar 2023 20:34:15 +0200 Subject: [PATCH 3/6] Add missing licenses and prepare test. --- .../collections/WeakMapReferenceCounter.java | 18 ++++++++++++++++++ .../elastic/apm/agent/grpc/GrpcHelperTest.java | 3 ++- .../tracer/reference/ReferenceCounted.java | 18 ++++++++++++++++++ .../tracer/reference/ReferenceCounter.java | 18 ++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakMapReferenceCounter.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakMapReferenceCounter.java index 17f633dc2f..a109707b2f 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakMapReferenceCounter.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakMapReferenceCounter.java @@ -1,3 +1,21 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package co.elastic.apm.agent.collections; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; diff --git a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/test/java/co/elastic/apm/agent/grpc/GrpcHelperTest.java b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/test/java/co/elastic/apm/agent/grpc/GrpcHelperTest.java index d0f59ddc86..755d62530b 100644 --- a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/test/java/co/elastic/apm/agent/grpc/GrpcHelperTest.java +++ b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/test/java/co/elastic/apm/agent/grpc/GrpcHelperTest.java @@ -18,6 +18,7 @@ */ package co.elastic.apm.agent.grpc; +import co.elastic.apm.agent.AbstractInstrumentationTest; import co.elastic.apm.agent.tracer.Outcome; import io.grpc.Status; import org.junit.jupiter.api.Test; @@ -26,7 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat; -class GrpcHelperTest { +class GrpcHelperTest extends AbstractInstrumentationTest { @ParameterizedTest @EnumSource(Status.Code.class) diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounted.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounted.java index a8865d3c87..23ca749632 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounted.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounted.java @@ -1,3 +1,21 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package co.elastic.apm.agent.tracer.reference; public interface ReferenceCounted { diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounter.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounter.java index 2f29523a99..19243c5300 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounter.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounter.java @@ -1,3 +1,21 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package co.elastic.apm.agent.tracer.reference; import javax.annotation.Nullable; From 71ab7321367a928ff5ed0b1a458062baec2029c3 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Fri, 26 May 2023 15:01:37 +0200 Subject: [PATCH 4/6] Fixing helper. --- .../src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java index f9f59e7447..f8d076d7ed 100644 --- a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java +++ b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java @@ -91,7 +91,7 @@ public static GrpcHelper getInstance() { private final Tracer tracer; public GrpcHelper() { - Tracer tracer = GlobalTracer.get(); + tracer = GlobalTracer.get(); clientCallSpans = tracer.createReferenceCounter(); delayedClientCallSpans = tracer.createReferenceCounter(); clientCallListenerSpans = tracer.createReferenceCounter(); @@ -103,8 +103,6 @@ public GrpcHelper() { headerSetter = new GrpcHeaderSetter(); headerGetter = new GrpcHeaderGetter(); - - tracer = GlobalTracer.get(); } // transaction management (server part) From d2a30f39ae7c00870b631a5a7bc6d6b65558e8c3 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Fri, 2 Jun 2023 00:04:21 +0200 Subject: [PATCH 5/6] Change names and assure that weak reference counted map always applies reference counting. --- ...nter.java => WeakReferenceCountedMap.java} | 10 +++---- .../apm/agent/impl/ElasticApmTracer.java | 11 +++----- ...bstractAsyncHttpClientInstrumentation.java | 4 +-- .../agent/dubbo/AlibabaCallbackHolder.java | 4 +-- .../co/elastic/apm/agent/grpc/GrpcHelper.java | 14 +++++----- .../apm/agent/concurrent/JavaConcurrent.java | 4 +-- .../apm/agent/process/ProcessHelper.java | 6 ++--- .../apm/agent/process/ProcessHelperTest.java | 27 +++++++++++++++++-- .../apm/agent/reactor/TracedSubscriber.java | 4 +-- .../lettuce/Lettuce34Instrumentation.java | 4 +-- .../Lettuce5StartSpanInstrumentation.java | 4 +-- .../FutureInstrumentation.java | 4 +-- .../springwebclient/WebClientSubscriber.java | 4 +-- .../TransactionAwareSubscriber.java | 4 +-- .../HttpUrlConnectionInstrumentation.java | 6 ++--- .../apm/agent/vertx/v3/web/WebHelper.java | 6 ++--- .../apm/agent/tracer/GlobalTracer.java | 4 +-- .../elastic/apm/agent/tracer/NoopTracer.java | 4 +-- .../co/elastic/apm/agent/tracer/Tracer.java | 4 +-- ...eCounter.java => ReferenceCountedMap.java} | 2 +- 20 files changed, 71 insertions(+), 59 deletions(-) rename apm-agent-core/src/main/java/co/elastic/apm/agent/collections/{WeakMapReferenceCounter.java => WeakReferenceCountedMap.java} (82%) rename apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/{ReferenceCounter.java => ReferenceCountedMap.java} (93%) diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakMapReferenceCounter.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakReferenceCountedMap.java similarity index 82% rename from apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakMapReferenceCounter.java rename to apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakReferenceCountedMap.java index a109707b2f..3b23b7f826 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakMapReferenceCounter.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/collections/WeakReferenceCountedMap.java @@ -20,17 +20,13 @@ import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.reference.ReferenceCounted; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import javax.annotation.Nullable; -public class WeakMapReferenceCounter implements ReferenceCounter { +public class WeakReferenceCountedMap implements ReferenceCountedMap { - private final WeakMap map; - - public WeakMapReferenceCounter(WeakMap map) { - this.map = map; - } + private final WeakMap map = WeakConcurrentProviderImpl.createWeakReferenceCountedMap(); @Override @Nullable diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java index d3f59ebe85..1ac331b8cb 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java @@ -18,8 +18,7 @@ */ package co.elastic.apm.agent.impl; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; -import co.elastic.apm.agent.collections.WeakMapReferenceCounter; +import co.elastic.apm.agent.collections.WeakReferenceCountedMap; import co.elastic.apm.agent.common.JvmRuntimeInfo; import co.elastic.apm.agent.common.util.WildcardMatcher; import co.elastic.apm.agent.configuration.CoreConfiguration; @@ -52,14 +51,12 @@ import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.reference.ReferenceCounted; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import co.elastic.apm.agent.util.DependencyInjectingServiceLoader; import co.elastic.apm.agent.util.ExecutorUtils; import co.elastic.apm.agent.tracer.Scope; import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; -import co.elastic.apm.agent.util.DependencyInjectingServiceLoader; -import co.elastic.apm.agent.util.ExecutorUtils; import co.elastic.apm.agent.util.PrivilegedActionUtils; import co.elastic.apm.agent.util.VersionUtils; import org.stagemonitor.configuration.ConfigurationOption; @@ -604,8 +601,8 @@ public ObjectPoolFactory getObjectPoolFactory() { } @Override - public ReferenceCounter createReferenceCounter() { - return new WeakMapReferenceCounter<>(WeakConcurrentProviderImpl.createWeakReferenceCountedMap()); + public ReferenceCountedMap createReferenceCounter() { + return new WeakReferenceCountedMap<>(); } @Override diff --git a/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java b/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java index 516c3109d4..d7bdd542e7 100644 --- a/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java +++ b/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java @@ -25,7 +25,7 @@ import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.sdk.DynamicTransformer; import co.elastic.apm.agent.sdk.ElasticApmInstrumentation; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; @@ -53,7 +53,7 @@ public abstract class AbstractAsyncHttpClientInstrumentation extends TracerAware public static class Helper { - static final ReferenceCounter, Span> handlerSpanMap = tracer.createReferenceCounter(); + static final ReferenceCountedMap, Span> handlerSpanMap = tracer.createReferenceCounter(); public static final List> ASYNC_HANDLER_INSTRUMENTATIONS = Arrays.>asList( AsyncHandlerOnCompletedInstrumentation.class, diff --git a/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java b/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java index 3e2f0b8adf..928d307a4c 100644 --- a/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java +++ b/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java @@ -20,9 +20,9 @@ import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.GlobalTracer; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import com.alibaba.dubbo.remoting.exchange.ResponseCallback; public class AlibabaCallbackHolder { - public static final ReferenceCounter> callbackSpanMap = GlobalTracer.get().createReferenceCounter(); + public static final ReferenceCountedMap> callbackSpanMap = GlobalTracer.get().createReferenceCounter(); } diff --git a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java index f8d076d7ed..0a6195d8a8 100644 --- a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java +++ b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java @@ -30,7 +30,7 @@ import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import io.grpc.CallOptions; import io.grpc.ClientCall; import io.grpc.Metadata; @@ -58,27 +58,27 @@ public static GrpcHelper getInstance() { /** * Map of all in-flight {@link Span} with {@link ClientCall} instance as key. */ - private final ReferenceCounter, Span> clientCallSpans; + private final ReferenceCountedMap, Span> clientCallSpans; /** * Map of all in-flight {@link Span} with {@link ClientCall} instance as key. */ - private final ReferenceCounter, Span> delayedClientCallSpans; + private final ReferenceCountedMap, Span> delayedClientCallSpans; /** * Map of all in-flight {@link Span} with {@link ClientCall.Listener} instance as key. */ - private final ReferenceCounter, Span> clientCallListenerSpans; + private final ReferenceCountedMap, Span> clientCallListenerSpans; /** * Map of all in-flight {@link Transaction} with {@link ServerCall.Listener} instance as key. */ - private final ReferenceCounter, Transaction> serverListenerTransactions; + private final ReferenceCountedMap, Transaction> serverListenerTransactions; /** * Map of all in-flight {@link Transaction} with {@link ServerCall} instance as key. */ - private final ReferenceCounter, Transaction> serverCallTransactions; + private final ReferenceCountedMap, Transaction> serverCallTransactions; /** * gRPC header cache used to minimize allocations @@ -479,7 +479,7 @@ public void clientCallStartExit(@Nullable Span spanFromEntry, ClientCall.List } public void cancelCall(ClientCall clientCall, @Nullable Throwable cause) { - ReferenceCounter, Span> clientCallMap = (isDelayedClientCall(clientCall)) ? delayedClientCallSpans : clientCallSpans; + ReferenceCountedMap, Span> clientCallMap = (isDelayedClientCall(clientCall)) ? delayedClientCallSpans : clientCallSpans; // we can't remove yet, in order to avoid reference decrement prematurely Span span = clientCallMap.get(clientCall); if (span != null) { diff --git a/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java b/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java index cdcfd588c7..c9eb9ad912 100644 --- a/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java +++ b/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java @@ -25,7 +25,7 @@ import co.elastic.apm.agent.sdk.state.GlobalState; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Tracer; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import javax.annotation.Nullable; import java.util.ArrayList; @@ -42,7 +42,7 @@ @GlobalState public class JavaConcurrent { - private static final ReferenceCounter> contextMap = GlobalTracer.get().createReferenceCounter(); + private static final ReferenceCountedMap> contextMap = GlobalTracer.get().createReferenceCounter(); private static final List> RUNNABLE_CALLABLE_FJTASK_INSTRUMENTATION = Collections. >singletonList(RunnableCallableForkJoinTaskInstrumentation.class); diff --git a/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java b/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java index c839b4aef2..98949a4fad 100644 --- a/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java +++ b/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java @@ -23,7 +23,7 @@ import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.sdk.state.GlobalVariables; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import javax.annotation.Nonnull; import java.util.List; @@ -44,9 +44,9 @@ class ProcessHelper { */ private static final ThreadLocal inTracingContext = GlobalVariables.get(ProcessHelper.class, "inTracingContext", new ThreadLocal()); - private final ReferenceCounter> inFlightSpans; + private final ReferenceCountedMap> inFlightSpans; - ProcessHelper(ReferenceCounter> inFlightSpans) { + ProcessHelper(ReferenceCountedMap> inFlightSpans) { this.inFlightSpans = inFlightSpans; } diff --git a/apm-agent-plugins/apm-process-plugin/src/test/java/co/elastic/apm/agent/process/ProcessHelperTest.java b/apm-agent-plugins/apm-process-plugin/src/test/java/co/elastic/apm/agent/process/ProcessHelperTest.java index 63daf10437..84f95db191 100644 --- a/apm-agent-plugins/apm-process-plugin/src/test/java/co/elastic/apm/agent/process/ProcessHelperTest.java +++ b/apm-agent-plugins/apm-process-plugin/src/test/java/co/elastic/apm/agent/process/ProcessHelperTest.java @@ -20,15 +20,16 @@ import co.elastic.apm.agent.AbstractInstrumentationTest; import co.elastic.apm.agent.TransactionUtils; -import co.elastic.apm.agent.collections.WeakMapReferenceCounter; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.impl.transaction.Span; import co.elastic.apm.agent.impl.transaction.Transaction; import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import javax.annotation.Nullable; import java.nio.file.Paths; import static org.assertj.core.api.Assertions.assertThat; @@ -58,7 +59,29 @@ void before() { TransactionUtils.fillTransaction(transaction); storageMap = WeakConcurrent.buildMap(); - helper = new ProcessHelper(new WeakMapReferenceCounter<>(storageMap)); + helper = new ProcessHelper(new ReferenceCountedMap<>() { + @Nullable + @Override + public co.elastic.apm.agent.tracer.Span get(Process key) { + return storageMap.get(key); + } + + @Override + public boolean contains(Process key) { + return storageMap.containsKey(key); + } + + @Override + public void put(Process key, co.elastic.apm.agent.tracer.Span value) { + storageMap.put(key, value); + } + + @Nullable + @Override + public co.elastic.apm.agent.tracer.Span remove(Process key) { + return storageMap.remove(key); + } + }); } @Test diff --git a/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java b/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java index ac31a082e7..bf9460f50c 100644 --- a/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java +++ b/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java @@ -26,7 +26,7 @@ import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Tracer; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; @@ -47,7 +47,7 @@ public class TracedSubscriber implements CoreSubscriber { private static final AtomicBoolean isRegistered = GlobalVariables.get(ReactorInstrumentation.class, "reactor-hook-enabled", new AtomicBoolean(false)); - private static final ReferenceCounter, AbstractSpan> contextMap = GlobalTracer.get().createReferenceCounter(); + private static final ReferenceCountedMap, AbstractSpan> contextMap = GlobalTracer.get().createReferenceCounter(); private static final String HOOK_KEY = "elastic-apm"; diff --git a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java index 66410fe137..e8e4e3422e 100644 --- a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java +++ b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java @@ -20,7 +20,7 @@ import co.elastic.apm.agent.bci.TracerAwareInstrumentation; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import com.lambdaworks.redis.protocol.RedisCommand; import net.bytebuddy.matcher.ElementMatcher; @@ -31,7 +31,7 @@ public abstract class Lettuce34Instrumentation extends TracerAwareInstrumentation { - static final ReferenceCounter, Span> commandToSpan = tracer.createReferenceCounter(); + static final ReferenceCountedMap, Span> commandToSpan = tracer.createReferenceCounter(); /** * We don't support Lettuce up to version 3.3, as the {@link RedisCommand#getType()} method is missing diff --git a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java index 9f9a616ee0..bfc2a67262 100644 --- a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java +++ b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java @@ -21,7 +21,7 @@ import co.elastic.apm.agent.bci.TracerAwareInstrumentation; import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.redis.RedisSpanUtils; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import io.lettuce.core.protocol.RedisCommand; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; @@ -45,7 +45,7 @@ */ public class Lettuce5StartSpanInstrumentation extends TracerAwareInstrumentation { - static final ReferenceCounter, Span> commandToSpan = tracer.createReferenceCounter(); + static final ReferenceCountedMap, Span> commandToSpan = tracer.createReferenceCounter(); @Override public ElementMatcher getTypeMatcher() { diff --git a/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java b/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java index 255ad89be4..44a2ea6734 100644 --- a/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java +++ b/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java @@ -20,7 +20,7 @@ import co.elastic.apm.agent.bci.TracerAwareInstrumentation; import co.elastic.apm.agent.tracer.AbstractSpan; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; @@ -38,7 +38,7 @@ public abstract class FutureInstrumentation extends TracerAwareInstrumentation { @SuppressWarnings("WeakerAccess") - public static final ReferenceCounter> promisesToContext = tracer.createReferenceCounter(); + public static final ReferenceCountedMap> promisesToContext = tracer.createReferenceCounter(); @Nonnull @Override diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java index c69ad07163..457aa2ba58 100755 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java @@ -22,7 +22,7 @@ import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.tracer.Tracer; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import org.reactivestreams.Subscription; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,7 +35,7 @@ public class WebClientSubscriber implements CoreSubscriber, Subscription { private static final Logger logger = LoggerFactory.getLogger(WebClientSubscriber.class); - private static final ReferenceCounter, Span> spanMap = GlobalTracer.get().createReferenceCounter(); + private static final ReferenceCountedMap, Span> spanMap = GlobalTracer.get().createReferenceCounter(); private final Tracer tracer; private final CoreSubscriber subscriber; diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java index cff574a217..1c986b84d0 100644 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java @@ -21,7 +21,7 @@ import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Transaction; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import co.elastic.apm.agent.sdk.logging.Logger; @@ -41,7 +41,7 @@ class TransactionAwareSubscriber implements CoreSubscriber, Subscription { private static final Logger log = LoggerFactory.getLogger(TransactionAwareSubscriber.class); - private static final ReferenceCounter, Transaction> transactionMap = GlobalTracer.get().createReferenceCounter(); + private static final ReferenceCountedMap, Transaction> transactionMap = GlobalTracer.get().createReferenceCounter(); private final CoreSubscriber subscriber; diff --git a/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java b/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java index 8025937f0c..a2e8862508 100644 --- a/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java +++ b/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java @@ -19,16 +19,14 @@ package co.elastic.apm.agent.urlconnection; import co.elastic.apm.agent.bci.TracerAwareInstrumentation; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.httpclient.HttpClientHelper; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.sdk.state.CallDepth; import co.elastic.apm.agent.sdk.state.GlobalState; -import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -51,7 +49,7 @@ @GlobalState public abstract class HttpUrlConnectionInstrumentation extends TracerAwareInstrumentation { - public static final ReferenceCounter> inFlightSpans = tracer.createReferenceCounter(); + public static final ReferenceCountedMap> inFlightSpans = tracer.createReferenceCounter(); public static final CallDepth callDepth = CallDepth.get(HttpUrlConnectionInstrumentation.class); @Override diff --git a/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java b/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java index 013baa193c..a82b9ef4c6 100644 --- a/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java +++ b/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java @@ -18,14 +18,12 @@ */ package co.elastic.apm.agent.vertx.v3.web; -import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; -import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import co.elastic.apm.agent.vertx.AbstractVertxWebHelper; import io.vertx.core.Handler; import io.vertx.core.http.HttpServerRequest; @@ -39,7 +37,7 @@ public class WebHelper extends AbstractVertxWebHelper { private static final WebHelper INSTANCE = new WebHelper(GlobalTracer.get()); - static final ReferenceCounter> transactionMap = GlobalTracer.get().createReferenceCounter(); + static final ReferenceCountedMap> transactionMap = GlobalTracer.get().createReferenceCounter(); public static WebHelper getInstance() { return INSTANCE; diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java index 42228d529e..24e90dda63 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java @@ -22,7 +22,7 @@ import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import co.elastic.apm.agent.tracer.reference.ReferenceCounted; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import javax.annotation.Nullable; import java.util.Set; @@ -85,7 +85,7 @@ public ObjectPoolFactory getObjectPoolFactory() { } @Override - public ReferenceCounter createReferenceCounter() { + public ReferenceCountedMap createReferenceCounter() { return tracer.createReferenceCounter(); } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java index 1bf5d61d02..695179ba56 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java @@ -22,7 +22,7 @@ import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import co.elastic.apm.agent.tracer.reference.ReferenceCounted; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import javax.annotation.Nullable; import java.util.Collections; @@ -62,7 +62,7 @@ public ObjectPoolFactory getObjectPoolFactory() { } @Override - public ReferenceCounter createReferenceCounter() { + public ReferenceCountedMap createReferenceCounter() { throw new IllegalStateException(); } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java index f346e83e21..5f4077f166 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java @@ -23,7 +23,7 @@ import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import co.elastic.apm.agent.tracer.reference.ReferenceCounted; -import co.elastic.apm.agent.tracer.reference.ReferenceCounter; +import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import javax.annotation.Nullable; import java.util.Set; @@ -41,7 +41,7 @@ public interface Tracer { ObjectPoolFactory getObjectPoolFactory(); - ReferenceCounter createReferenceCounter(); + ReferenceCountedMap createReferenceCounter(); Set getTraceHeaderNames(); diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounter.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCountedMap.java similarity index 93% rename from apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounter.java rename to apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCountedMap.java index 19243c5300..42ff17cc58 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCounter.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reference/ReferenceCountedMap.java @@ -20,7 +20,7 @@ import javax.annotation.Nullable; -public interface ReferenceCounter { +public interface ReferenceCountedMap { @Nullable V get(K key); From 5d32ca46acd3ef903e785d3ea2366caf9676f857 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Fri, 2 Jun 2023 14:52:38 +0200 Subject: [PATCH 6/6] Change factory method for reference counted map. --- .../co/elastic/apm/agent/impl/ElasticApmTracer.java | 2 +- .../AbstractAsyncHttpClientInstrumentation.java | 2 +- .../elastic/apm/agent/dubbo/AlibabaCallbackHolder.java | 2 +- .../java/co/elastic/apm/agent/grpc/GrpcHelper.java | 10 +++++----- .../elastic/apm/agent/concurrent/JavaConcurrent.java | 2 +- .../co/elastic/apm/agent/process/ProcessHelper.java | 2 +- .../co/elastic/apm/agent/reactor/TracedSubscriber.java | 2 +- .../apm/agent/lettuce/Lettuce34Instrumentation.java | 2 +- .../lettuce/Lettuce5StartSpanInstrumentation.java | 2 +- .../agent/scalaconcurrent/FutureInstrumentation.java | 2 +- .../apm/agent/springwebclient/WebClientSubscriber.java | 2 +- .../springwebflux/TransactionAwareSubscriber.java | 2 +- .../HttpUrlConnectionInstrumentation.java | 2 +- .../co/elastic/apm/agent/vertx/v3/web/WebHelper.java | 2 +- .../java/co/elastic/apm/agent/tracer/GlobalTracer.java | 4 ++-- .../java/co/elastic/apm/agent/tracer/NoopTracer.java | 2 +- .../main/java/co/elastic/apm/agent/tracer/Tracer.java | 2 +- 17 files changed, 22 insertions(+), 22 deletions(-) diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java index 1ac331b8cb..d669efa840 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java @@ -601,7 +601,7 @@ public ObjectPoolFactory getObjectPoolFactory() { } @Override - public ReferenceCountedMap createReferenceCounter() { + public ReferenceCountedMap newReferenceCountedMap() { return new WeakReferenceCountedMap<>(); } diff --git a/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java b/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java index d7bdd542e7..125713ba69 100644 --- a/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java +++ b/apm-agent-plugins/apm-asynchttpclient-plugin/src/main/java/co/elastic/apm/agent/asynchttpclient/AbstractAsyncHttpClientInstrumentation.java @@ -53,7 +53,7 @@ public abstract class AbstractAsyncHttpClientInstrumentation extends TracerAware public static class Helper { - static final ReferenceCountedMap, Span> handlerSpanMap = tracer.createReferenceCounter(); + static final ReferenceCountedMap, Span> handlerSpanMap = tracer.newReferenceCountedMap(); public static final List> ASYNC_HANDLER_INSTRUMENTATIONS = Arrays.>asList( AsyncHandlerOnCompletedInstrumentation.class, diff --git a/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java b/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java index 928d307a4c..faac9a8617 100644 --- a/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java +++ b/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/AlibabaCallbackHolder.java @@ -24,5 +24,5 @@ import com.alibaba.dubbo.remoting.exchange.ResponseCallback; public class AlibabaCallbackHolder { - public static final ReferenceCountedMap> callbackSpanMap = GlobalTracer.get().createReferenceCounter(); + public static final ReferenceCountedMap> callbackSpanMap = GlobalTracer.get().newReferenceCountedMap(); } diff --git a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java index 0a6195d8a8..9178a983f7 100644 --- a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java +++ b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java @@ -92,12 +92,12 @@ public static GrpcHelper getInstance() { public GrpcHelper() { tracer = GlobalTracer.get(); - clientCallSpans = tracer.createReferenceCounter(); - delayedClientCallSpans = tracer.createReferenceCounter(); - clientCallListenerSpans = tracer.createReferenceCounter(); + clientCallSpans = tracer.newReferenceCountedMap(); + delayedClientCallSpans = tracer.newReferenceCountedMap(); + clientCallListenerSpans = tracer.newReferenceCountedMap(); - serverListenerTransactions = tracer.createReferenceCounter(); - serverCallTransactions = tracer.createReferenceCounter(); + serverListenerTransactions = tracer.newReferenceCountedMap(); + serverCallTransactions = tracer.newReferenceCountedMap(); headerCache = WeakConcurrent.buildMap(); diff --git a/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java b/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java index c9eb9ad912..9507eec38d 100644 --- a/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java +++ b/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/JavaConcurrent.java @@ -42,7 +42,7 @@ @GlobalState public class JavaConcurrent { - private static final ReferenceCountedMap> contextMap = GlobalTracer.get().createReferenceCounter(); + private static final ReferenceCountedMap> contextMap = GlobalTracer.get().newReferenceCountedMap(); private static final List> RUNNABLE_CALLABLE_FJTASK_INSTRUMENTATION = Collections. >singletonList(RunnableCallableForkJoinTaskInstrumentation.class); diff --git a/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java b/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java index 98949a4fad..8d94043238 100644 --- a/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java +++ b/apm-agent-plugins/apm-process-plugin/src/main/java/co/elastic/apm/agent/process/ProcessHelper.java @@ -34,7 +34,7 @@ */ class ProcessHelper { - private static final ProcessHelper INSTANCE = new ProcessHelper(GlobalTracer.get().>createReferenceCounter()); + private static final ProcessHelper INSTANCE = new ProcessHelper(GlobalTracer.get().>newReferenceCountedMap()); /** * A thread local used to indicate whether the currently invoked instrumented method is invoked by the plugin itself. diff --git a/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java b/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java index bf9460f50c..7c6b6f3206 100644 --- a/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java +++ b/apm-agent-plugins/apm-reactor-plugin/src/main/java/co/elastic/apm/agent/reactor/TracedSubscriber.java @@ -47,7 +47,7 @@ public class TracedSubscriber implements CoreSubscriber { private static final AtomicBoolean isRegistered = GlobalVariables.get(ReactorInstrumentation.class, "reactor-hook-enabled", new AtomicBoolean(false)); - private static final ReferenceCountedMap, AbstractSpan> contextMap = GlobalTracer.get().createReferenceCounter(); + private static final ReferenceCountedMap, AbstractSpan> contextMap = GlobalTracer.get().newReferenceCountedMap(); private static final String HOOK_KEY = "elastic-apm"; diff --git a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java index e8e4e3422e..a96832a580 100644 --- a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java +++ b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce34Instrumentation.java @@ -31,7 +31,7 @@ public abstract class Lettuce34Instrumentation extends TracerAwareInstrumentation { - static final ReferenceCountedMap, Span> commandToSpan = tracer.createReferenceCounter(); + static final ReferenceCountedMap, Span> commandToSpan = tracer.newReferenceCountedMap(); /** * We don't support Lettuce up to version 3.3, as the {@link RedisCommand#getType()} method is missing diff --git a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java index bfc2a67262..7cbcf9c314 100644 --- a/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java +++ b/apm-agent-plugins/apm-redis-plugin/apm-lettuce-plugin/src/main/java/co/elastic/apm/agent/lettuce/Lettuce5StartSpanInstrumentation.java @@ -45,7 +45,7 @@ */ public class Lettuce5StartSpanInstrumentation extends TracerAwareInstrumentation { - static final ReferenceCountedMap, Span> commandToSpan = tracer.createReferenceCounter(); + static final ReferenceCountedMap, Span> commandToSpan = tracer.newReferenceCountedMap(); @Override public ElementMatcher getTypeMatcher() { diff --git a/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java b/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java index 44a2ea6734..8302093ddf 100644 --- a/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java +++ b/apm-agent-plugins/apm-scala-concurrent-plugin/src/main/java/co/elastic/apm/agent/scalaconcurrent/FutureInstrumentation.java @@ -38,7 +38,7 @@ public abstract class FutureInstrumentation extends TracerAwareInstrumentation { @SuppressWarnings("WeakerAccess") - public static final ReferenceCountedMap> promisesToContext = tracer.createReferenceCounter(); + public static final ReferenceCountedMap> promisesToContext = tracer.newReferenceCountedMap(); @Nonnull @Override diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java index 457aa2ba58..fbbd4467ce 100755 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java @@ -35,7 +35,7 @@ public class WebClientSubscriber implements CoreSubscriber, Subscription { private static final Logger logger = LoggerFactory.getLogger(WebClientSubscriber.class); - private static final ReferenceCountedMap, Span> spanMap = GlobalTracer.get().createReferenceCounter(); + private static final ReferenceCountedMap, Span> spanMap = GlobalTracer.get().newReferenceCountedMap(); private final Tracer tracer; private final CoreSubscriber subscriber; diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java index 1c986b84d0..56a5983ed6 100644 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/TransactionAwareSubscriber.java @@ -41,7 +41,7 @@ class TransactionAwareSubscriber implements CoreSubscriber, Subscription { private static final Logger log = LoggerFactory.getLogger(TransactionAwareSubscriber.class); - private static final ReferenceCountedMap, Transaction> transactionMap = GlobalTracer.get().createReferenceCounter(); + private static final ReferenceCountedMap, Transaction> transactionMap = GlobalTracer.get().newReferenceCountedMap(); private final CoreSubscriber subscriber; diff --git a/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java b/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java index a2e8862508..6ccb56b3a9 100644 --- a/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java +++ b/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java @@ -49,7 +49,7 @@ @GlobalState public abstract class HttpUrlConnectionInstrumentation extends TracerAwareInstrumentation { - public static final ReferenceCountedMap> inFlightSpans = tracer.createReferenceCounter(); + public static final ReferenceCountedMap> inFlightSpans = tracer.newReferenceCountedMap(); public static final CallDepth callDepth = CallDepth.get(HttpUrlConnectionInstrumentation.class); @Override diff --git a/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java b/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java index a82b9ef4c6..4f25c02a1a 100644 --- a/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java +++ b/apm-agent-plugins/apm-vertx/apm-vertx3-plugin/src/main/java/co/elastic/apm/agent/vertx/v3/web/WebHelper.java @@ -37,7 +37,7 @@ public class WebHelper extends AbstractVertxWebHelper { private static final WebHelper INSTANCE = new WebHelper(GlobalTracer.get()); - static final ReferenceCountedMap> transactionMap = GlobalTracer.get().createReferenceCounter(); + static final ReferenceCountedMap> transactionMap = GlobalTracer.get().newReferenceCountedMap(); public static WebHelper getInstance() { return INSTANCE; diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java index 24e90dda63..e012e0f04b 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java @@ -85,8 +85,8 @@ public ObjectPoolFactory getObjectPoolFactory() { } @Override - public ReferenceCountedMap createReferenceCounter() { - return tracer.createReferenceCounter(); + public ReferenceCountedMap newReferenceCountedMap() { + return tracer.newReferenceCountedMap(); } @Override diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java index 695179ba56..4a0897397a 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java @@ -62,7 +62,7 @@ public ObjectPoolFactory getObjectPoolFactory() { } @Override - public ReferenceCountedMap createReferenceCounter() { + public ReferenceCountedMap newReferenceCountedMap() { throw new IllegalStateException(); } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java index 5f4077f166..f3650830fc 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java @@ -41,7 +41,7 @@ public interface Tracer { ObjectPoolFactory getObjectPoolFactory(); - ReferenceCountedMap createReferenceCounter(); + ReferenceCountedMap newReferenceCountedMap(); Set getTraceHeaderNames();