From 5d0a5f74f61c04b3bfb354b7b038a75eb378a229 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Wed, 22 Feb 2023 14:35:44 +0100 Subject: [PATCH] #3030: Removing unused error capture and make use of a tracer subtype in one module to demonstrate expandability. --- .../elastic/apm/agent/impl/BasicTracer.java | 31 ++++++++++------ .../elastic/apm/agent/impl/GlobalTracer.java | 37 ++++++------------- .../apm/agent/impl/MetricsAwareTracer.java | 13 +++++++ .../co/elastic/apm/agent/impl/NoopTracer.java | 17 +++++---- .../elastic/apm/plugin/spi/GlobalTracer.java | 35 ++++++------------ .../co/elastic/apm/plugin/spi/NoopTracer.java | 21 ++++++----- .../co/elastic/apm/plugin/spi/Tracer.java | 10 ++--- .../APIGatewayProxyV1TransactionHelper.java | 8 ++-- .../APIGatewayProxyV2TransactionHelper.java | 8 ++-- .../AbstractAPIGatewayTransactionHelper.java | 6 +-- .../AbstractLambdaTransactionHelper.java | 6 +-- ...AbstractMessageBasedTransactionHelper.java | 4 +- .../helper/PlainTransactionHelper.java | 9 ++--- .../awslambda/helper/S3TransactionHelper.java | 8 ++-- .../helper/SNSTransactionHelper.java | 8 ++-- .../helper/SQSTransactionHelper.java | 6 +-- .../specs/OutcomeGrpcStepsDefinitions.java | 8 ++-- .../apm/agent/java_ldap/LdapClientAdvice.java | 18 ++++----- .../AbstractEcsReformattingHelper.java | 4 +- .../AbstractMicrometerInstrumentation.java | 3 +- .../micrometer/MicrometerMetricsReporter.java | 7 ++-- .../ApmSpanBuilderInstrumentation.java | 3 +- 22 files changed, 135 insertions(+), 135 deletions(-) diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/BasicTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/BasicTracer.java index 7fe6ef034f..b0d849e977 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/BasicTracer.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/BasicTracer.java @@ -36,6 +36,7 @@ import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.plugin.spi.*; +import co.elastic.apm.plugin.spi.Tracer; import javax.annotation.Nullable; import java.util.Collections; @@ -487,12 +488,6 @@ public String captureAndReportException(long epochMicros, @Nullable Throwable e, return captureAndReportException(epochMicros, e, (AbstractSpan) parent); } - @Nullable - @Override - public co.elastic.apm.plugin.spi.ErrorCapture captureException(@Nullable Throwable e, @Nullable co.elastic.apm.plugin.spi.AbstractSpan parent, @Nullable ClassLoader initiatingClassLoader) { - return captureException(e, (AbstractSpan) parent, initiatingClassLoader); - } - @Override public void endSpan(co.elastic.apm.plugin.spi.Span span) { endSpan((Span) span); @@ -503,11 +498,6 @@ public void endTransaction(co.elastic.apm.plugin.spi.Transaction transaction) endTransaction((Transaction) transaction); } - @Override - public void endError(co.elastic.apm.plugin.spi.ErrorCapture errorCapture) { - endError((ErrorCapture) errorCapture); - } - @Override public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm.plugin.spi.ServiceInfo serviceInfo) { setServiceInfoForClassLoader(classLoader, serviceInfo.isMultiServiceContainer() @@ -519,4 +509,23 @@ public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm public ServiceInfo autoDetectedServiceName() { return ServiceInfo.autoDetected(); } + + @Nullable + @Override + public T probe(Class type) { + if (type.isInstance(this)) { + return type.cast(this); + } else { + return null; + } + } + + @Override + public T require(Class type) { + T tracer = probe(type); + if (tracer == null) { + throw new IllegalStateException(this + " tracer does not support features of " + type.getName()); + } + return tracer; + } } diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/GlobalTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/GlobalTracer.java index 6b5cbdc340..661b1ddd12 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/GlobalTracer.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/GlobalTracer.java @@ -51,22 +51,9 @@ public static Tracer get() { return INSTANCE; } - @Nullable - public static T get(Class type) { - Tracer tracer = INSTANCE.tracer; - if (type.isInstance(tracer)) { - return type.cast(tracer); - } - return null; - } - - public static T require(Class type) { - return Objects.requireNonNull(get(type), "Registered tracer is not an instance of " + type.getName()); - } - @Nullable public static ElasticApmTracer getTracerImpl() { - return get(ElasticApmTracer.class); + return get().probe(ElasticApmTracer.class); } public static ElasticApmTracer requireTracerImpl() { @@ -302,12 +289,6 @@ public String captureAndReportException(long epochMicros, @Nullable Throwable e, return tracer.captureAndReportException(epochMicros, e, parent); } - @Nullable - @Override - public co.elastic.apm.plugin.spi.ErrorCapture captureException(@Nullable Throwable e, @Nullable co.elastic.apm.plugin.spi.AbstractSpan parent, @Nullable ClassLoader initiatingClassLoader) { - return tracer.captureException(e, parent, initiatingClassLoader); - } - @Override public void endSpan(co.elastic.apm.plugin.spi.Span span) { tracer.endSpan(span); @@ -318,11 +299,6 @@ public void endTransaction(co.elastic.apm.plugin.spi.Transaction transaction) tracer.endTransaction(transaction); } - @Override - public void endError(co.elastic.apm.plugin.spi.ErrorCapture errorCapture) { - tracer.endError(errorCapture); - } - @Override public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm.plugin.spi.ServiceInfo serviceInfo) { tracer.setServiceInfoForClassLoader(classLoader, serviceInfo); @@ -332,4 +308,15 @@ public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm public ServiceInfo autoDetectedServiceName() { return tracer.autoDetectedServiceName(); } + + @Nullable + @Override + public T probe(Class type) { + return tracer.probe(type); + } + + @Override + public T require(Class type) { + return tracer.require(type); + } } diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/MetricsAwareTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/MetricsAwareTracer.java index f5792d8998..1e2dbbf8dd 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/MetricsAwareTracer.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/MetricsAwareTracer.java @@ -18,9 +18,22 @@ */ package co.elastic.apm.agent.impl; +import co.elastic.apm.agent.impl.metadata.MetaDataFuture; import co.elastic.apm.agent.metrics.MetricRegistry; +import co.elastic.apm.agent.report.Reporter; + +import java.io.Closeable; +import java.util.concurrent.ScheduledExecutorService; public interface MetricsAwareTracer extends SpanAwareTracer { MetricRegistry getMetricRegistry(); + + Reporter getReporter(); + + void addShutdownHook(Closeable closeable); + + ScheduledExecutorService getSharedSingleThreadedPool(); + + MetaDataFuture getMetaDataFuture(); } diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/NoopTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/NoopTracer.java index 21d59fb092..8f928dec8e 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/NoopTracer.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/NoopTracer.java @@ -205,30 +205,31 @@ public String captureAndReportException(long epochMicros, @Nullable Throwable e, return null; } - @Nullable @Override - public co.elastic.apm.plugin.spi.ErrorCapture captureException(@Nullable Throwable e, @Nullable co.elastic.apm.plugin.spi.AbstractSpan parent, @Nullable ClassLoader initiatingClassLoader) { - return null; + public void endSpan(co.elastic.apm.plugin.spi.Span span) { } @Override - public void endSpan(co.elastic.apm.plugin.spi.Span span) { + public void endTransaction(co.elastic.apm.plugin.spi.Transaction transaction) { } @Override - public void endTransaction(co.elastic.apm.plugin.spi.Transaction transaction) { + public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm.plugin.spi.ServiceInfo serviceInfo) { } @Override - public void endError(co.elastic.apm.plugin.spi.ErrorCapture errorCapture) { + public ServiceInfo autoDetectedServiceName() { + return null; } + @Nullable @Override - public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm.plugin.spi.ServiceInfo serviceInfo) { + public T probe(Class type) { + return null; } @Override - public ServiceInfo autoDetectedServiceName() { + public T require(Class type) { return null; } } diff --git a/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/GlobalTracer.java b/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/GlobalTracer.java index 71e598bc18..0d7c181519 100644 --- a/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/GlobalTracer.java +++ b/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/GlobalTracer.java @@ -19,7 +19,6 @@ package co.elastic.apm.plugin.spi; import javax.annotation.Nullable; -import java.util.Objects; public class GlobalTracer implements Tracer { @@ -33,19 +32,6 @@ public static Tracer get() { return INSTANCE; } - @Nullable - public static T get(Class type) { - Tracer tracer = INSTANCE.tracer; - if (type.isInstance(tracer)) { - return type.cast(tracer); - } - return null; - } - - public static T require(Class type) { - return Objects.requireNonNull(get(type), "Registered tracer is not an instance of " + type.getName()); - } - public static synchronized void init(Tracer tracer) { if (!isNoop()) { throw new IllegalStateException("Tracer is already initialized"); @@ -110,11 +96,6 @@ public String captureAndReportException(long epochMicros, @Nullable Throwable e, return tracer.captureAndReportException(epochMicros, e, parent); } - @Nullable - public ErrorCapture captureException(@Nullable Throwable e, @Nullable AbstractSpan parent, @Nullable ClassLoader initiatingClassLoader) { - return tracer.captureException(e, parent, initiatingClassLoader); - } - @Nullable @Override public Span getActiveExitSpan() { @@ -137,11 +118,6 @@ public void endTransaction(Transaction transaction) { tracer.endTransaction(transaction); } - @Override - public void endError(ErrorCapture errorCapture) { - tracer.endError(errorCapture); - } - @Override public T getConfig(Class configuration) { return tracer.getConfig(configuration); @@ -171,4 +147,15 @@ public ServiceInfo getServiceInfoForClassLoader(ClassLoader classLoader) { public ServiceInfo autoDetectedServiceName() { return tracer.autoDetectedServiceName(); } + + @Nullable + @Override + public T probe(Class type) { + return tracer.probe(type); + } + + @Override + public T require(Class type) { + return tracer.require(type); + } } diff --git a/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/NoopTracer.java b/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/NoopTracer.java index d292dc0a28..3fb4f0511d 100644 --- a/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/NoopTracer.java +++ b/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/NoopTracer.java @@ -67,12 +67,6 @@ public String captureAndReportException(long epochMicros, @Nullable Throwable e, return null; } - @Nullable - @Override - public ErrorCapture captureException(@Nullable Throwable e, @Nullable AbstractSpan parent, @Nullable ClassLoader initiatingClassLoader) { - return null; - } - @Nullable @Override public Span getActiveExitSpan() { @@ -93,10 +87,6 @@ public void endSpan(Span span) { public void endTransaction(Transaction transaction) { } - @Override - public void endError(ErrorCapture errorCapture) { - } - @Override public T getConfig(Class configuration) { return null; @@ -125,4 +115,15 @@ public ServiceInfo getServiceInfoForClassLoader(ClassLoader classLoader) { public ServiceInfo autoDetectedServiceName() { return null; } + + @Nullable + @Override + public T probe(Class type) { + return null; + } + + @Override + public T require(Class type) { + return null; + } } diff --git a/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/Tracer.java b/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/Tracer.java index c91cb04402..5ff29f6119 100644 --- a/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/Tracer.java +++ b/apm-agent-plugin-spi/src/main/java/co/elastic/apm/plugin/spi/Tracer.java @@ -51,9 +51,6 @@ public interface Tracer { @Nullable String captureAndReportException(long epochMicros, @Nullable Throwable e, @Nullable AbstractSpan parent); - @Nullable - ErrorCapture captureException(@Nullable Throwable e, @Nullable AbstractSpan parent, @Nullable ClassLoader initiatingClassLoader); - @Nullable Span getActiveExitSpan(); @@ -64,8 +61,6 @@ public interface Tracer { void endTransaction(Transaction transaction); - void endError(ErrorCapture errorCapture); - T getConfig(Class configuration); ObjectPoolFactory getObjectPoolFactory(); @@ -77,4 +72,9 @@ public interface Tracer { ServiceInfo getServiceInfoForClassLoader(ClassLoader classLoader); ServiceInfo autoDetectedServiceName(); + + @Nullable + T probe(Class type); + + T require(Class type); } diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/APIGatewayProxyV1TransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/APIGatewayProxyV1TransactionHelper.java index f9d7d8985c..5f1906c344 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/APIGatewayProxyV1TransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/APIGatewayProxyV1TransactionHelper.java @@ -19,10 +19,10 @@ package co.elastic.apm.agent.awslambda.helper; import co.elastic.apm.agent.awslambda.MapTextHeaderGetter; -import co.elastic.apm.agent.impl.ElasticApmTracer; -import co.elastic.apm.agent.impl.GlobalTracer; +import co.elastic.apm.agent.impl.MetricsAwareTracer; import co.elastic.apm.agent.impl.transaction.Transaction; import co.elastic.apm.agent.util.PrivilegedActionUtils; +import co.elastic.apm.plugin.spi.GlobalTracer; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; @@ -35,13 +35,13 @@ public class APIGatewayProxyV1TransactionHelper extends AbstractAPIGatewayTransa @Nullable private static APIGatewayProxyV1TransactionHelper INSTANCE; - private APIGatewayProxyV1TransactionHelper(ElasticApmTracer tracer) { + private APIGatewayProxyV1TransactionHelper(MetricsAwareTracer tracer) { super(tracer); } public static APIGatewayProxyV1TransactionHelper getInstance() { if (INSTANCE == null) { - INSTANCE = new APIGatewayProxyV1TransactionHelper(GlobalTracer.requireTracerImpl()); + INSTANCE = new APIGatewayProxyV1TransactionHelper(GlobalTracer.get().require(MetricsAwareTracer.class)); } return INSTANCE; } diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/APIGatewayProxyV2TransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/APIGatewayProxyV2TransactionHelper.java index eab0ecab6f..718107884c 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/APIGatewayProxyV2TransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/APIGatewayProxyV2TransactionHelper.java @@ -19,10 +19,10 @@ package co.elastic.apm.agent.awslambda.helper; import co.elastic.apm.agent.awslambda.MapTextHeaderGetter; -import co.elastic.apm.agent.impl.ElasticApmTracer; -import co.elastic.apm.agent.impl.GlobalTracer; +import co.elastic.apm.agent.impl.MetricsAwareTracer; import co.elastic.apm.agent.impl.transaction.Transaction; import co.elastic.apm.agent.util.PrivilegedActionUtils; +import co.elastic.apm.plugin.spi.GlobalTracer; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse; @@ -33,13 +33,13 @@ public class APIGatewayProxyV2TransactionHelper extends AbstractAPIGatewayTransa @Nullable private static APIGatewayProxyV2TransactionHelper INSTANCE; - private APIGatewayProxyV2TransactionHelper(ElasticApmTracer tracer) { + private APIGatewayProxyV2TransactionHelper(MetricsAwareTracer tracer) { super(tracer); } public static APIGatewayProxyV2TransactionHelper getInstance() { if (INSTANCE == null) { - INSTANCE = new APIGatewayProxyV2TransactionHelper(GlobalTracer.requireTracerImpl()); + INSTANCE = new APIGatewayProxyV2TransactionHelper(GlobalTracer.get().require(MetricsAwareTracer.class)); } return INSTANCE; } diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractAPIGatewayTransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractAPIGatewayTransactionHelper.java index 6f98c79874..5a6e51ca6c 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractAPIGatewayTransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractAPIGatewayTransactionHelper.java @@ -18,7 +18,8 @@ */ package co.elastic.apm.agent.awslambda.helper; -import co.elastic.apm.agent.impl.ElasticApmTracer; +import co.elastic.apm.agent.common.util.WildcardMatcher; +import co.elastic.apm.agent.impl.MetricsAwareTracer; import co.elastic.apm.agent.impl.context.CloudOrigin; import co.elastic.apm.agent.impl.context.Request; import co.elastic.apm.agent.impl.context.Response; @@ -26,7 +27,6 @@ import co.elastic.apm.agent.impl.context.web.ResultUtil; import co.elastic.apm.agent.impl.transaction.AbstractSpan; import co.elastic.apm.agent.impl.transaction.Transaction; -import co.elastic.apm.agent.common.util.WildcardMatcher; import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; import com.amazonaws.services.lambda.runtime.Context; @@ -47,7 +47,7 @@ public abstract class AbstractAPIGatewayTransactionHelper extends Abstract private static final Set METHODS_WITH_BODY = new HashSet<>(Arrays.asList("POST", "PUT", "PATCH", "DELETE")); private static final String CONTENT_TYPE_FROM_URLENCODED = "application/x-www-form-urlencoded"; - protected AbstractAPIGatewayTransactionHelper(ElasticApmTracer tracer) { + protected AbstractAPIGatewayTransactionHelper(MetricsAwareTracer tracer) { super(tracer); } diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractLambdaTransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractLambdaTransactionHelper.java index dae3713629..3c5756c514 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractLambdaTransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractLambdaTransactionHelper.java @@ -22,7 +22,7 @@ import co.elastic.apm.agent.bci.InstrumentationStats; import co.elastic.apm.agent.configuration.CoreConfiguration; import co.elastic.apm.agent.configuration.ServerlessConfiguration; -import co.elastic.apm.agent.impl.ElasticApmTracer; +import co.elastic.apm.agent.impl.MetricsAwareTracer; import co.elastic.apm.agent.impl.context.web.WebConfiguration; import co.elastic.apm.agent.impl.metadata.FaaSMetaDataExtension; import co.elastic.apm.agent.impl.metadata.Framework; @@ -43,13 +43,13 @@ public abstract class AbstractLambdaTransactionHelper { private static final Logger logger = LoggerFactory.getLogger(AbstractLambdaTransactionHelper.class); private static final Logger enabledInstrumentationsLogger = LoggerUtils.logOnce(logger); - protected final ElasticApmTracer tracer; + protected final MetricsAwareTracer tracer; protected final ServerlessConfiguration serverlessConfiguration; protected final CoreConfiguration coreConfiguration; protected final WebConfiguration webConfiguration; - protected AbstractLambdaTransactionHelper(ElasticApmTracer tracer) { + protected AbstractLambdaTransactionHelper(MetricsAwareTracer tracer) { this.tracer = tracer; this.coreConfiguration = tracer.getConfig(CoreConfiguration.class); this.webConfiguration = tracer.getConfig(WebConfiguration.class); diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractMessageBasedTransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractMessageBasedTransactionHelper.java index fac1e586ca..d59f6458a4 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractMessageBasedTransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractMessageBasedTransactionHelper.java @@ -18,7 +18,7 @@ */ package co.elastic.apm.agent.awslambda.helper; -import co.elastic.apm.agent.impl.ElasticApmTracer; +import co.elastic.apm.agent.impl.MetricsAwareTracer; import co.elastic.apm.agent.impl.context.CloudOrigin; import co.elastic.apm.agent.impl.context.ServiceOrigin; import co.elastic.apm.agent.impl.transaction.AbstractSpan; @@ -31,7 +31,7 @@ public abstract class AbstractMessageBasedTransactionHelper extends AbstractLambdaTransactionHelper { protected static final String TRANSACTION_TYPE = "messaging"; - protected AbstractMessageBasedTransactionHelper(ElasticApmTracer tracer) { + protected AbstractMessageBasedTransactionHelper(MetricsAwareTracer tracer) { super(tracer); } diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/PlainTransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/PlainTransactionHelper.java index 503279f1ff..d0a8b3d726 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/PlainTransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/PlainTransactionHelper.java @@ -18,11 +18,10 @@ */ package co.elastic.apm.agent.awslambda.helper; -import co.elastic.apm.agent.impl.ElasticApmTracer; -import co.elastic.apm.agent.impl.GlobalTracer; -import co.elastic.apm.agent.impl.Tracer; +import co.elastic.apm.agent.impl.MetricsAwareTracer; import co.elastic.apm.agent.impl.transaction.Transaction; import co.elastic.apm.agent.util.PrivilegedActionUtils; +import co.elastic.apm.plugin.spi.GlobalTracer; import com.amazonaws.services.lambda.runtime.Context; import javax.annotation.Nullable; @@ -34,13 +33,13 @@ public class PlainTransactionHelper extends AbstractLambdaTransactionHelper mapFunction) { diff --git a/apm-agent-plugins/apm-java-ldap-plugin/src/main/java/co/elastic/apm/agent/java_ldap/LdapClientAdvice.java b/apm-agent-plugins/apm-java-ldap-plugin/src/main/java/co/elastic/apm/agent/java_ldap/LdapClientAdvice.java index cfb86808c4..9caff2f2b5 100644 --- a/apm-agent-plugins/apm-java-ldap-plugin/src/main/java/co/elastic/apm/agent/java_ldap/LdapClientAdvice.java +++ b/apm-agent-plugins/apm-java-ldap-plugin/src/main/java/co/elastic/apm/agent/java_ldap/LdapClientAdvice.java @@ -19,8 +19,8 @@ package co.elastic.apm.agent.java_ldap; import co.elastic.apm.plugin.spi.*; -// TODO Rafael import com.sun.jndi.ldap.Connection; -// TODO Rafael import com.sun.jndi.ldap.LdapResult; +import com.sun.jndi.ldap.Connection; +import com.sun.jndi.ldap.LdapResult; import net.bytebuddy.asm.Advice; import net.bytebuddy.implementation.bytecode.assign.Assigner; @@ -31,7 +31,7 @@ public class LdapClientAdvice { private static final Tracer tracer = GlobalTracer.get(); @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) - public static Object onEnter(@Advice.Origin("#m") String methodName, @Advice.FieldValue(value = "conn", typing = Assigner.Typing.DYNAMIC) Void connection) { + public static Object onEnter(@Advice.Origin("#m") String methodName, @Advice.FieldValue(value = "conn", typing = Assigner.Typing.DYNAMIC) Connection connection) { AbstractSpan parent = tracer.getActive(); if (parent == null) { return null; @@ -47,20 +47,20 @@ public static Object onEnter(@Advice.Origin("#m") String methodName, @Advice.Fie .withSubtype("ldap"); if (connection != null) { - //span.getContext().getDestination().withAddress(connection.host).withPort(connection.port); - //span.getContext().getServiceTarget().withType("ldap").withHostPortName(connection.host, connection.port); + span.getContext().getDestination().withAddress(connection.host).withPort(connection.port); + span.getContext().getServiceTarget().withType("ldap").withHostPortName(connection.host, connection.port); } return span.activate(); } @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) - public static void onExit(@Advice.Enter @Nullable Object spanObj, @Nullable @Advice.Return Void ldapResult, @Nullable @Advice.Thrown Throwable t) { + public static void onExit(@Advice.Enter @Nullable Object spanObj, @Nullable @Advice.Return LdapResult ldapResult, @Nullable @Advice.Thrown Throwable t) { Span span = (Span) spanObj; if (span != null) { - //span.withOutcome((ldapResult != null && ldapResult.status == 0 /* LDAP_SUCCESS */) ? DefaultOutcome.SUCCESS : DefaultOutcome.FAILURE) -// .captureException(t) - // .deactivate().end(); + span.withOutcome((ldapResult != null && ldapResult.status == 0 /* LDAP_SUCCESS */) ? DefaultOutcome.SUCCESS : DefaultOutcome.FAILURE) + .captureException(t) + .deactivate().end(); } } } diff --git a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java index add0c4157f..ab312bc43c 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java @@ -23,10 +23,12 @@ import co.elastic.apm.agent.configuration.ServerlessConfiguration; import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.impl.GlobalTracer; +import co.elastic.apm.agent.impl.MetricsAwareTracer; import co.elastic.apm.agent.impl.metadata.Service; import co.elastic.apm.agent.impl.metadata.ServiceFactory; import co.elastic.apm.agent.logging.LogEcsReformatting; import co.elastic.apm.agent.logging.LoggingConfiguration; +import co.elastic.apm.plugin.spi.Tracer; import co.elastic.apm.plugin.spi.WildcardMatcher; import co.elastic.apm.agent.report.Reporter; import co.elastic.apm.agent.sdk.logging.Logger; @@ -169,7 +171,7 @@ public abstract class AbstractEcsReformattingHelper { private final Reporter reporter; public AbstractEcsReformattingHelper() { - ElasticApmTracer tracer = GlobalTracer.requireTracerImpl(); + MetricsAwareTracer tracer = GlobalTracer.get().require(MetricsAwareTracer.class); loggingConfiguration = tracer.getConfig(LoggingConfiguration.class); additionalFields = loggingConfiguration.getLogEcsReformattingAdditionalFields(); Service service = new ServiceFactory().createService( diff --git a/apm-agent-plugins/apm-micrometer-plugin/src/main/java/co/elastic/apm/agent/micrometer/AbstractMicrometerInstrumentation.java b/apm-agent-plugins/apm-micrometer-plugin/src/main/java/co/elastic/apm/agent/micrometer/AbstractMicrometerInstrumentation.java index 80dbe0cac9..728a190223 100644 --- a/apm-agent-plugins/apm-micrometer-plugin/src/main/java/co/elastic/apm/agent/micrometer/AbstractMicrometerInstrumentation.java +++ b/apm-agent-plugins/apm-micrometer-plugin/src/main/java/co/elastic/apm/agent/micrometer/AbstractMicrometerInstrumentation.java @@ -20,13 +20,14 @@ import co.elastic.apm.agent.bci.TracerAwareInstrumentation; import co.elastic.apm.agent.impl.GlobalTracer; +import co.elastic.apm.agent.impl.MetricsAwareTracer; import java.util.Collection; import java.util.Collections; public abstract class AbstractMicrometerInstrumentation extends TracerAwareInstrumentation { - static final MicrometerMetricsReporter reporter = new MicrometerMetricsReporter(GlobalTracer.requireTracerImpl()); + static final MicrometerMetricsReporter reporter = new MicrometerMetricsReporter(GlobalTracer.get().require(MetricsAwareTracer.class)); public Collection getInstrumentationGroupNames() { return Collections.singletonList("micrometer"); diff --git a/apm-agent-plugins/apm-micrometer-plugin/src/main/java/co/elastic/apm/agent/micrometer/MicrometerMetricsReporter.java b/apm-agent-plugins/apm-micrometer-plugin/src/main/java/co/elastic/apm/agent/micrometer/MicrometerMetricsReporter.java index 0ce8e68a86..fe45648d68 100644 --- a/apm-agent-plugins/apm-micrometer-plugin/src/main/java/co/elastic/apm/agent/micrometer/MicrometerMetricsReporter.java +++ b/apm-agent-plugins/apm-micrometer-plugin/src/main/java/co/elastic/apm/agent/micrometer/MicrometerMetricsReporter.java @@ -20,6 +20,7 @@ import co.elastic.apm.agent.configuration.MetricsConfiguration; import co.elastic.apm.agent.impl.ElasticApmTracer; +import co.elastic.apm.agent.impl.MetricsAwareTracer; import co.elastic.apm.agent.impl.Tracer; import co.elastic.apm.agent.common.util.WildcardMatcher; import co.elastic.apm.agent.report.Reporter; @@ -91,16 +92,16 @@ public class MicrometerMetricsReporter implements Runnable, Closeable { private final WeakMap configMap = WeakConcurrent.buildMap(); private final MicrometerMeterRegistrySerializer serializer; private final Reporter reporter; - private final ElasticApmTracer tracer; + private final MetricsAwareTracer tracer; private final AtomicBoolean scheduledReporting = new AtomicBoolean(); private final boolean disableScheduler; - public MicrometerMetricsReporter(ElasticApmTracer tracer) { + public MicrometerMetricsReporter(MetricsAwareTracer tracer) { this(tracer, false); } //constructor split up to have this available for testing - MicrometerMetricsReporter(ElasticApmTracer tracer, boolean disableSchedulerThread) { + MicrometerMetricsReporter(MetricsAwareTracer tracer, boolean disableSchedulerThread) { this.tracer = tracer; this.reporter = tracer.getReporter(); tracer.addShutdownHook(this); diff --git a/apm-agent-plugins/apm-opentracing-plugin/src/main/java/co/elastic/apm/agent/opentracingimpl/ApmSpanBuilderInstrumentation.java b/apm-agent-plugins/apm-opentracing-plugin/src/main/java/co/elastic/apm/agent/opentracingimpl/ApmSpanBuilderInstrumentation.java index 919ccfd330..93598dc809 100644 --- a/apm-agent-plugins/apm-opentracing-plugin/src/main/java/co/elastic/apm/agent/opentracingimpl/ApmSpanBuilderInstrumentation.java +++ b/apm-agent-plugins/apm-opentracing-plugin/src/main/java/co/elastic/apm/agent/opentracingimpl/ApmSpanBuilderInstrumentation.java @@ -19,7 +19,6 @@ package co.elastic.apm.agent.opentracingimpl; import co.elastic.apm.agent.impl.ElasticApmTracer; -import co.elastic.apm.agent.impl.GlobalTracer; import co.elastic.apm.agent.impl.sampling.ConstantSampler; import co.elastic.apm.agent.impl.sampling.Sampler; import co.elastic.apm.agent.impl.transaction.AbstractSpan; @@ -90,7 +89,7 @@ public static AbstractSpan doCreateTransactionOrSpan(@Nullable AbstractSpan> baggage, ClassLoader applicationClassLoader) { AbstractSpan result = null; - ElasticApmTracer tracer = GlobalTracer.getTracerImpl(); + ElasticApmTracer tracer = ApmSpanBuilderInstrumentation.tracer.probe(ElasticApmTracer.class); if (tracer != null) { if (parentContext == null) { result = createTransaction(tags, operationName, microseconds, baggage, tracer, applicationClassLoader);