Skip to content

Commit

Permalink
Introduce API for metrics collection that does not depend on agent-co…
Browse files Browse the repository at this point in the history
…re (#3522)

* Remove logging API.

* Clean up core dependencies from POMs that do not need it.

* Add API for handling lambda instrumentation and apply to the AWS plugin.

* Move metrics API to tracer.

* Factor out JSON DSL for metrics.

* Fix import issue.

* Fix imports.

* Fix imports.

* fix formatting

---------

Co-authored-by: Sylvain Juge <[email protected]>
  • Loading branch information
raphw and SylvainJuge authored Mar 12, 2024
1 parent 464b9c1 commit 4858631
Show file tree
Hide file tree
Showing 68 changed files with 444 additions and 298 deletions.
5 changes: 0 additions & 5 deletions apm-agent-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,6 @@
<version>${version.jetty-server}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.dslplatform</groupId>
<artifactId>dsl-json</artifactId>
<version>1.9.3</version>
</dependency>
<!--Used to test old class file versions-->
<dependency>
<groupId>commons-math</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
package co.elastic.apm.agent.bci;

import co.elastic.apm.agent.bci.bytebuddy.MatcherTimer;
import co.elastic.apm.agent.context.AbstractLifecycleListener;
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.Tracer;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -31,7 +31,7 @@ public class InstrumentationStatsLifecycleListener extends AbstractLifecycleList
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,8 +18,8 @@
*/
package co.elastic.apm.agent.collections;

import co.elastic.apm.agent.context.AbstractLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.tracer.Tracer;
import co.elastic.apm.agent.util.ExecutorUtils;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
Expand All @@ -41,7 +41,7 @@ public WeakMapCleaner() {
}

@Override
public void start(ElasticApmTracer tracer) {
public void start(Tracer tracer) {
scheduler.scheduleWithFixedDelay(this, 1, 1, TimeUnit.SECONDS);
}

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.LifecycleListener;
import co.elastic.apm.agent.tracer.LifecycleListener;
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.Tracer;
import co.elastic.apm.agent.util.ExecutorUtils;
import com.dslplatform.json.DslJson;
import com.dslplatform.json.JsonReader;
Expand Down Expand Up @@ -111,17 +112,17 @@ public void reload() {
}

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

@Override
public void start(final ElasticApmTracer tracer) {
public void start(final Tracer tracer) {
threadPool = ExecutorUtils.createSingleThreadDaemonPool("remote-config-poller", 1);
threadPool.execute(new Runnable() {
@Override
public void run() {
pollConfig(tracer.getConfigurationRegistry());
pollConfig(tracer.require(ElasticApmTracer.class).getConfigurationRegistry());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*/
package co.elastic.apm.agent.configuration;

import co.elastic.apm.agent.tracer.Tracer;
import co.elastic.apm.agent.tracer.configuration.TimeDuration;
import co.elastic.apm.agent.context.AbstractLifecycleListener;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
import co.elastic.apm.agent.util.VersionUtils;
Expand Down Expand Up @@ -56,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
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
*/
package co.elastic.apm.agent.context;

import java.io.Closeable;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.tracer.LifecycleListener;

public class ClosableLifecycleListenerAdapter extends AbstractLifecycleListener {

private final Closeable closeable;
private final AutoCloseable closeable;

public static LifecycleListener of(Closeable closeable) {
public static LifecycleListener of(AutoCloseable closeable) {
return new ClosableLifecycleListenerAdapter(closeable);
}

private ClosableLifecycleListenerAdapter(Closeable closeable) {
private ClosableLifecycleListenerAdapter(AutoCloseable closeable) {
this.closeable = closeable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
import co.elastic.apm.agent.impl.metadata.NameAndIdField;
import co.elastic.apm.agent.impl.metadata.ServiceFactory;
import co.elastic.apm.agent.sdk.internal.util.LoggerUtils;
import co.elastic.apm.agent.tracer.metrics.DoubleSupplier;
import co.elastic.apm.agent.tracer.metrics.Labels;
import co.elastic.apm.agent.tracer.service.Service;
import co.elastic.apm.agent.tracer.service.ServiceInfo;
import co.elastic.apm.agent.configuration.SpanConfiguration;
import co.elastic.apm.agent.context.ClosableLifecycleListenerAdapter;
import co.elastic.apm.agent.context.LifecycleListener;
import co.elastic.apm.agent.tracer.LifecycleListener;
import co.elastic.apm.agent.impl.baggage.Baggage;
import co.elastic.apm.agent.impl.baggage.W3CBaggagePropagation;
import co.elastic.apm.agent.impl.error.ErrorCapture;
Expand Down Expand Up @@ -71,12 +73,12 @@
import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap;
import co.elastic.apm.agent.util.DependencyInjectingServiceLoader;
import co.elastic.apm.agent.util.ExecutorUtils;
import com.dslplatform.json.JsonWriter;
import org.stagemonitor.configuration.ConfigurationOption;
import org.stagemonitor.configuration.ConfigurationOptionProvider;
import org.stagemonitor.configuration.ConfigurationRegistry;

import javax.annotation.Nullable;
import java.io.Closeable;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -954,7 +956,8 @@ public ScheduledThreadPoolExecutor getSharedSingleThreadedPool() {
return sharedPool;
}

public void addShutdownHook(Closeable closeable) {
@Override
public void addShutdownHook(AutoCloseable closeable) {
lifecycleListeners.add(ClosableLifecycleListenerAdapter.of(closeable));
}

Expand Down Expand Up @@ -1044,4 +1047,30 @@ public void completeMetaData(String name, String version, String id, String regi
region
));
}

@Override
public void removeGauge(String name, Labels.Immutable labels) {
metricRegistry.removeGauge(name, labels);
}

@Override
public void addGauge(String name, Labels.Immutable labels, DoubleSupplier supplier) {
metricRegistry.add(name, labels, supplier);
}

@Override
public void submit(Runnable job) {
sharedPool.submit(job);
}

@Override
public void schedule(Runnable job, long interval, TimeUnit timeUnit) {
sharedPool.scheduleAtFixedRate(job, 0, interval, timeUnit);
}

@Override
public void reportMetric(JsonWriter metrics) {
reporter.reportMetrics(metrics);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import co.elastic.apm.agent.configuration.source.ConfigSources;
import co.elastic.apm.agent.configuration.source.SystemPropertyConfigurationSource;
import co.elastic.apm.agent.context.ClosableLifecycleListenerAdapter;
import co.elastic.apm.agent.context.LifecycleListener;
import co.elastic.apm.agent.tracer.LifecycleListener;
import co.elastic.apm.agent.impl.metadata.MetaData;
import co.elastic.apm.agent.impl.metadata.MetaDataFuture;
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*/
package co.elastic.apm.agent.impl.circuitbreaker;

import co.elastic.apm.agent.context.AbstractLifecycleListener;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.tracer.Tracer;
import co.elastic.apm.agent.util.ExecutorUtils;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
Expand Down Expand Up @@ -50,7 +51,7 @@ public CircuitBreaker(ElasticApmTracer tracer) {
}

@Override
public void start(ElasticApmTracer tracer) {
public void start(Tracer tracer) {
// failsafe loading of stress monitors in isolation
loadGCStressMonitor(tracer);
loadSystemCpuStressMonitor(tracer);
Expand All @@ -63,15 +64,15 @@ public void run() {
});
}

private void loadGCStressMonitor(ElasticApmTracer tracer) {
private void loadGCStressMonitor(Tracer tracer) {
try {
stressMonitors.add(new GCStressMonitor(tracer));
} catch (Throwable throwable) {
logger.error("Failed to load the GC stress monitor. Circuit breaker will not be triggered based on GC events.", throwable);
}
}

private void loadSystemCpuStressMonitor(ElasticApmTracer tracer) {
private void loadSystemCpuStressMonitor(Tracer tracer) {
try {
stressMonitors.add(new SystemCpuStressMonitor(tracer));
} catch (Throwable throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import co.elastic.apm.agent.tracer.Tracer;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
Expand All @@ -35,7 +36,7 @@ class GCStressMonitor extends StressMonitor {
private final List<MemoryPoolMXBean> heapMBeans = new ArrayList<>();
private final StringBuilder latestStressDetectionInfo = new StringBuilder("No stress has been detected so far.");

GCStressMonitor(ElasticApmTracer tracer) {
GCStressMonitor(Tracer tracer) {
super(tracer);
discoverMBeans();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
*/
package co.elastic.apm.agent.impl.circuitbreaker;

import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.tracer.Tracer;

abstract class StressMonitor {

protected final CircuitBreakerConfiguration circuitBreakerConfiguration;

public StressMonitor(ElasticApmTracer tracer) {
public StressMonitor(Tracer tracer) {
circuitBreakerConfiguration = tracer.getConfig(CircuitBreakerConfiguration.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package co.elastic.apm.agent.impl.circuitbreaker;

import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.tracer.Tracer;
import co.elastic.apm.agent.util.JmxUtils;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
Expand All @@ -45,7 +45,7 @@ public class SystemCpuStressMonitor extends StressMonitor {
@Nullable
private final Method systemCpuUsageMethod;

SystemCpuStressMonitor(ElasticApmTracer tracer) {
SystemCpuStressMonitor(Tracer tracer) {
super(tracer);
operatingSystemBean = ManagementFactory.getOperatingSystemMXBean();
systemCpuUsageMethod = JmxUtils.getOperatingSystemMBeanMethod(operatingSystemBean, "getSystemCpuLoad");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import co.elastic.apm.agent.impl.context.Response;
import co.elastic.apm.agent.impl.context.TransactionContext;
import co.elastic.apm.agent.impl.sampling.Sampler;
import co.elastic.apm.agent.metrics.Labels;
import co.elastic.apm.agent.tracer.metrics.Labels;
import co.elastic.apm.agent.metrics.MetricRegistry;
import co.elastic.apm.agent.metrics.Timer;
import co.elastic.apm.agent.tracer.Outcome;
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.AbstractLifecycleListener;
import co.elastic.apm.agent.context.LifecycleListener;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.tracer.LifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.report.Reporter;
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 @@ -103,8 +104,8 @@ public void append(LogEvent event) {
public LifecycleListener getInitListener() {
return new AbstractLifecycleListener() {
@Override
public void init(ElasticApmTracer tracer) throws Exception {
initStreaming(tracer.getConfig(LoggingConfiguration.class), tracer.getReporter());
public void init(Tracer tracer) throws Exception {
initStreaming(tracer.getConfig(LoggingConfiguration.class), tracer.require(ElasticApmTracer.class).getReporter());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package co.elastic.apm.agent.metrics;

import co.elastic.apm.agent.tracer.metrics.Labels;

public interface MetricCollector {

void addMetricValue(String metric, Labels labels, double value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import co.elastic.apm.agent.report.ReporterConfiguration;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import co.elastic.apm.agent.tracer.metrics.DoubleSupplier;
import co.elastic.apm.agent.tracer.metrics.Labels;
import org.HdrHistogram.WriterReaderPhaser;

import javax.annotation.Nonnull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package co.elastic.apm.agent.metrics;

import co.elastic.apm.agent.tracer.metrics.DoubleSupplier;
import co.elastic.apm.agent.tracer.metrics.Labels;
import co.elastic.apm.agent.tracer.pooling.Recyclable;

import javax.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
package co.elastic.apm.agent.metrics.builtin;

import co.elastic.apm.agent.configuration.MetricsConfiguration;
import co.elastic.apm.agent.context.AbstractLifecycleListener;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.metrics.Labels;
import co.elastic.apm.agent.tracer.Tracer;
import co.elastic.apm.agent.tracer.metrics.Labels;
import co.elastic.apm.agent.metrics.MetricCollector;
import co.elastic.apm.agent.metrics.MetricRegistry;
import co.elastic.apm.agent.metrics.MetricsProvider;
Expand Down Expand Up @@ -113,8 +114,8 @@ public AgentOverheadMetrics() {
}

@Override
public void start(ElasticApmTracer tracer) throws Exception {
MetricRegistry metricRegistry = tracer.getMetricRegistry();
public void start(Tracer tracer) throws Exception {
MetricRegistry metricRegistry = tracer.require(ElasticApmTracer.class).getMetricRegistry();
MetricsConfiguration config = tracer.getConfig(MetricsConfiguration.class);
bindTo(metricRegistry, config);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package co.elastic.apm.agent.metrics.builtin;

import co.elastic.apm.agent.configuration.MetricsConfiguration;
import co.elastic.apm.agent.metrics.Labels;
import co.elastic.apm.agent.tracer.metrics.Labels;
import co.elastic.apm.agent.metrics.MetricCollector;
import co.elastic.apm.agent.metrics.MetricRegistry;
import co.elastic.apm.agent.metrics.MetricsProvider;
Expand Down
Loading

0 comments on commit 4858631

Please sign in to comment.