Skip to content

Commit

Permalink
Move shutdown hook method, and remove init lifecycle listener.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Nov 21, 2023
1 parent cd170c1 commit 0347252
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> IGNORED_REMOTE_KEYS = Collections.singleton("enable_log_correlation");
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +39,7 @@
* under Apache license 2.0.
* </p>
*/
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;
Expand All @@ -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);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -680,12 +679,10 @@ public List<ActivationListener> getActivationListeners() {
void init(List<LifecycleListener> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

public abstract class AbstractLifecycleListener implements LifecycleListener {

@Override
public void init(Tracer tracer) throws Exception {
}

@Override
public void start() throws Exception {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -133,4 +134,9 @@ public <T, C> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -110,4 +111,8 @@ public <T, C> Transaction<?> startChildTransaction(@Nullable C headerCarrier, He
public ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader) {
return null;
}

@Override
public void addShutdownHook(Closeable job) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -80,4 +81,6 @@ public interface Tracer {

@Nullable
ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader);

void addShutdownHook(Closeable job);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,4 @@ public interface ReportingTracer extends Tracer {
DataWriter newWriter(int maxSerializedSize);

ScheduledExecutorService getSharedSingleThreadedPool();

void addShutdownHook(Closeable job);
}

0 comments on commit 0347252

Please sign in to comment.