diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/InstrumentationStatsLifecycleListener.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/InstrumentationStatsLifecycleListener.java index 2e5e022870..d942f54641 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/InstrumentationStatsLifecycleListener.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/InstrumentationStatsLifecycleListener.java @@ -19,20 +19,20 @@ package co.elastic.apm.agent.bci; import co.elastic.apm.agent.bci.bytebuddy.MatcherTimer; -import co.elastic.apm.agent.context.InitLifecycleListener; -import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.tracer.AbstractLifecycleListener; import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; +import co.elastic.apm.agent.tracer.LifecycleListener; +import co.elastic.apm.agent.tracer.Tracer; import java.util.ArrayList; import java.util.Collections; -public class InstrumentationStatsLifecycleListener extends AbstractLifecycleListener implements InitLifecycleListener { +public class InstrumentationStatsLifecycleListener extends AbstractLifecycleListener implements LifecycleListener { private static final Logger logger = LoggerFactory.getLogger(InstrumentationStatsLifecycleListener.class); @Override - public void init(ElasticApmTracer tracer) { + public void init(Tracer tracer) { InstrumentationStats instrumentationStats = ElasticApmAgent.getInstrumentationStats(); instrumentationStats.reset(); instrumentationStats.setMeasureMatching(logger.isDebugEnabled()); diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ApmServerConfigurationSource.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ApmServerConfigurationSource.java index 7afda0b25b..c74539917d 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ApmServerConfigurationSource.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ApmServerConfigurationSource.java @@ -18,12 +18,13 @@ */ package co.elastic.apm.agent.configuration; -import co.elastic.apm.agent.context.InitLifecycleListener; import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.report.ApmServerClient; import co.elastic.apm.agent.report.serialize.DslJsonSerializer; import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; +import co.elastic.apm.agent.tracer.LifecycleListener; +import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.util.ExecutorUtils; import com.dslplatform.json.DslJson; import com.dslplatform.json.JsonReader; @@ -44,7 +45,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -public class ApmServerConfigurationSource extends AbstractConfigurationSource implements InitLifecycleListener { +public class ApmServerConfigurationSource extends AbstractConfigurationSource implements LifecycleListener { // log correlation is enabled by default in Java agent, thus removing it from warnings private static final Set IGNORED_REMOTE_KEYS = Collections.singleton("enable_log_correlation"); @@ -114,8 +115,8 @@ public void reload() { } @Override - public void init(ElasticApmTracer tracer) { - this.tracer = tracer; + public void init(Tracer tracer) { + this.tracer = tracer.require(ElasticApmTracer.class); } @Override diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/StartupInfo.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/StartupInfo.java index 27882d7610..4de8761af2 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/StartupInfo.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/StartupInfo.java @@ -18,7 +18,7 @@ */ package co.elastic.apm.agent.configuration; -import co.elastic.apm.agent.context.InitLifecycleListener; +import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.configuration.TimeDuration; import co.elastic.apm.agent.tracer.AbstractLifecycleListener; import co.elastic.apm.agent.impl.ElasticApmTracer; @@ -39,7 +39,7 @@ * under Apache license 2.0. *

*/ -public class StartupInfo extends AbstractLifecycleListener implements InitLifecycleListener { +public class StartupInfo extends AbstractLifecycleListener { private static final Logger logger = LoggerFactory.getLogger(StartupInfo.class); private final String elasticApmVersion; @@ -57,8 +57,8 @@ private static String getJvmAndOsVersionString() { } @Override - public void init(ElasticApmTracer tracer) { - ConfigurationRegistry configurationRegistry = tracer.getConfigurationRegistry(); + public void init(Tracer tracer) { + ConfigurationRegistry configurationRegistry = tracer.require(ElasticApmTracer.class).getConfigurationRegistry(); logConfiguration(configurationRegistry, logger); } diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/context/InitLifecycleListener.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/context/InitLifecycleListener.java deleted file mode 100644 index b92227e4d3..0000000000 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/context/InitLifecycleListener.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.context; - -import co.elastic.apm.agent.impl.ElasticApmTracer; -import co.elastic.apm.agent.tracer.LifecycleListener; - -public interface InitLifecycleListener extends LifecycleListener { - - /** - * Callback for tracer initialization. As opposed to {@link LifecycleListener#start()}, which may - * be called in a delay, this callback is called at the bootstrap of the JVM, before anything else starts. - * This may be useful for listeners that need to operate very early on, for example such that setup class loading - * requirement to support OSGi systems. - */ - void init(ElasticApmTracer tracer) throws Exception; -} 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 c36ac51f67..5116c2f089 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 @@ -26,7 +26,6 @@ import co.elastic.apm.agent.configuration.CoreConfiguration; import co.elastic.apm.agent.configuration.MetricsConfiguration; import co.elastic.apm.agent.configuration.ServerlessConfiguration; -import co.elastic.apm.agent.context.InitLifecycleListener; import co.elastic.apm.agent.report.serialize.DslJsonDataWriter; import co.elastic.apm.agent.tracer.reporting.DataWriter; import co.elastic.apm.agent.tracer.reporting.DoubleSupplier; @@ -680,12 +679,10 @@ public List getActivationListeners() { void init(List lifecycleListeners) { this.lifecycleListeners.addAll(lifecycleListeners); for (LifecycleListener lifecycleListener : lifecycleListeners) { - if (lifecycleListener instanceof InitLifecycleListener) { - try { - ((InitLifecycleListener) lifecycleListener).init(this); - } catch (Exception e) { - logger.error("Failed to init " + lifecycleListener.getClass().getName(), e); - } + try { + lifecycleListener.init(this); + } catch (Exception e) { + logger.error("Failed to init " + lifecycleListener.getClass().getName(), e); } } } diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/ApmServerLogAppender.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/ApmServerLogAppender.java index 5cf4fa486c..0e0a8e89eb 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/ApmServerLogAppender.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/ApmServerLogAppender.java @@ -18,10 +18,11 @@ */ package co.elastic.apm.agent.logging; -import co.elastic.apm.agent.context.InitLifecycleListener; import co.elastic.apm.agent.tracer.AbstractLifecycleListener; import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.report.Reporter; +import co.elastic.apm.agent.tracer.LifecycleListener; +import co.elastic.apm.agent.tracer.Tracer; import co.elastic.logging.log4j2.EcsLayout; import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.Core; @@ -100,11 +101,11 @@ public void append(LogEvent event) { sendLogEvent(event); } - public InitLifecycleListener getInitListener() { - class InitListener extends AbstractLifecycleListener implements InitLifecycleListener { + public LifecycleListener getInitListener() { + class InitListener extends AbstractLifecycleListener { @Override - public void init(ElasticApmTracer tracer) { - initStreaming(tracer.getConfig(LoggingConfiguration.class), tracer.getReporter()); + public void init(Tracer tracer) { + initStreaming(tracer.getConfig(LoggingConfiguration.class), tracer.require(ElasticApmTracer.class).getReporter()); } } return new InitListener(); diff --git a/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/LifecycleTest.java b/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/LifecycleTest.java index e2f4ec032f..f9b3eaf614 100644 --- a/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/LifecycleTest.java +++ b/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/LifecycleTest.java @@ -21,7 +21,6 @@ import co.elastic.apm.agent.MockReporter; import co.elastic.apm.agent.bci.ElasticApmAgent; import co.elastic.apm.agent.configuration.SpyConfiguration; -import co.elastic.apm.agent.context.InitLifecycleListener; import co.elastic.apm.agent.tracer.AbstractLifecycleListener; import co.elastic.apm.agent.objectpool.TestObjectPoolFactory; import net.bytebuddy.agent.ByteBuddyAgent; @@ -141,9 +140,9 @@ void testStartDisabled() throws Exception { ConfigurationRegistry configRegistry = SpyConfiguration.createSpyConfig(SimpleSource.forTest("enabled", "false")); final AtomicBoolean initialized = new AtomicBoolean(); final AtomicBoolean started = new AtomicBoolean(); - class TestListener extends AbstractLifecycleListener implements InitLifecycleListener { + class TestListener extends AbstractLifecycleListener { @Override - public void init(ElasticApmTracer tracer) { + public void init(co.elastic.apm.agent.tracer.Tracer tracer) { initialized.set(true); } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractLifecycleListener.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractLifecycleListener.java index c2d93adefb..6b8383e860 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractLifecycleListener.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractLifecycleListener.java @@ -20,6 +20,10 @@ public abstract class AbstractLifecycleListener implements LifecycleListener { + @Override + public void init(Tracer tracer) throws Exception { + } + @Override public void start() throws Exception { } 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 031f927381..897c7cf1b3 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 @@ -24,6 +24,7 @@ import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import javax.annotation.Nullable; +import java.io.Closeable; import java.util.Set; public class GlobalTracer implements Tracer { @@ -133,4 +134,9 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, He public ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader) { return tracer.captureException(e, initiatingClassLoader); } + + @Override + public void addShutdownHook(Closeable job) { + tracer.addShutdownHook(job); + } } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/LifecycleListener.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/LifecycleListener.java index 57f175b3fa..f305924942 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/LifecycleListener.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/LifecycleListener.java @@ -37,6 +37,14 @@ */ public interface LifecycleListener { + /** + * Callback for tracer initialization. As opposed to {@link LifecycleListener#start()}, which may + * be called in a delay, this callback is called at the bootstrap of the JVM, before anything else starts. + * This may be useful for listeners that need to operate very early on, for example such that setup class loading + * requirement to support OSGi systems. + */ + void init(Tracer tracer) throws Exception; + /** * Callback for when the {@link Tracer} starts. */ 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 6088e75683..356c77eb77 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 @@ -24,6 +24,7 @@ import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import javax.annotation.Nullable; +import java.io.Closeable; import java.util.Collections; import java.util.Set; @@ -110,4 +111,8 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, He public ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader) { return null; } + + @Override + public void addShutdownHook(Closeable job) { + } } 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 97f8e8bb87..369d4ef8a0 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 @@ -24,6 +24,7 @@ import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap; import javax.annotation.Nullable; +import java.io.Closeable; import java.util.Set; public interface Tracer { @@ -80,4 +81,6 @@ public interface Tracer { @Nullable ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader); + + void addShutdownHook(Closeable job); } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reporting/ReportingTracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reporting/ReportingTracer.java index 68caa70f80..9d0d195f1c 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reporting/ReportingTracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/reporting/ReportingTracer.java @@ -46,6 +46,4 @@ public interface ReportingTracer extends Tracer { DataWriter newWriter(int maxSerializedSize); ScheduledExecutorService getSharedSingleThreadedPool(); - - void addShutdownHook(Closeable job); }