Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce API for metric and log reporting within tracer-api. #3321

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.tracer.GlobalTracer;
import co.elastic.apm.agent.impl.Tracer;
import co.elastic.apm.agent.tracer.reporting.ReportingTracer;
import org.stagemonitor.configuration.ConfigurationOptionProvider;
import org.stagemonitor.configuration.ConfigurationRegistry;

Expand All @@ -38,7 +39,7 @@ public class ConfigurationExporter {

public static void main(String[] args) throws Exception {
ElasticApmTracer tracer = mock(ElasticApmTracer.class);
doReturn(tracer).when(tracer).require(ElasticApmTracer.class);
doReturn(tracer).when(tracer).require(ReportingTracer.class);
doReturn(Tracer.TracerState.UNINITIALIZED).when(tracer).getState();
GlobalTracer.init(tracer);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import co.elastic.apm.agent.tracer.GlobalTracer;
import co.elastic.apm.agent.impl.Tracer;
import co.elastic.apm.agent.sdk.ElasticApmInstrumentation;
import co.elastic.apm.agent.tracer.reporting.ReportingTracer;
import co.elastic.apm.agent.util.DependencyInjectingServiceLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
Expand Down Expand Up @@ -88,7 +89,7 @@ class ConfigurationExporterTest {
void setUp() {
renderedDocumentationPath = Paths.get("../../docs/configuration.asciidoc");
ElasticApmTracer tracer = mock(ElasticApmTracer.class);
doReturn(tracer).when(tracer).require(ElasticApmTracer.class);
doReturn(tracer).when(tracer).require(ReportingTracer.class);
doReturn(Tracer.TracerState.UNINITIALIZED).when(tracer).getState();
GlobalTracer.init(tracer);
configurationRegistry = ConfigurationRegistry.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@
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.LifecycleListener;
import co.elastic.apm.agent.tracer.Tracer;

import java.util.ArrayList;
import java.util.Collections;

public class InstrumentationStatsLifecycleListener extends AbstractLifecycleListener {
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,8 +18,7 @@
*/
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.util.ExecutorUtils;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
Expand All @@ -41,7 +40,7 @@ public WeakMapCleaner() {
}

@Override
public void start(ElasticApmTracer tracer) {
public void start() {
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.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 Down Expand Up @@ -62,6 +63,9 @@ public class ApmServerConfigurationSource extends AbstractConfigurationSource im
private final byte[] buffer = new byte[4096];
private final DslJsonSerializer.Writer payloadSerializer;
private final ApmServerClient apmServerClient;

@Nullable
private ElasticApmTracer tracer;
@Nullable
private String etag;
private volatile Map<String, String> config = Collections.emptyMap();
Expand Down Expand Up @@ -111,12 +115,12 @@ public void reload() {
}

@Override
public void init(ElasticApmTracer tracer) throws Exception {
// noop
public void init(Tracer tracer) {
this.tracer = tracer.require(ElasticApmTracer.class);
}

@Override
public void start(final ElasticApmTracer tracer) {
public void start() {
threadPool = ExecutorUtils.createSingleThreadDaemonPool("remote-config-poller", 1);
threadPool.execute(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ public ConfigurationOption<String> getServiceNameConfig() {
}

@Nullable
@Override
public String getServiceNodeName() {
String nodeName = serviceNodeName.get();
if (nodeName == null || nodeName.trim().isEmpty()) {
Expand All @@ -889,6 +890,7 @@ public long getDelayTracerStartMs() {
}

@Nullable
@Override
public String getServiceVersion() {
return serviceVersion.get();
}
Expand All @@ -899,6 +901,7 @@ public String getHostname() {
}

@Nullable
@Override
public String getEnvironment() {
return environment.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
import org.stagemonitor.configuration.ConfigurationOption;
import org.stagemonitor.configuration.ConfigurationOptionProvider;

import javax.annotation.Nullable;

public class ServerlessConfiguration extends ConfigurationOptionProvider implements co.elastic.apm.agent.tracer.configuration.ServerlessConfiguration {
public static final String SERVERLESS_CATEGORY = "Serverless";

private final boolean runsOnAwsLambda;

@Nullable
private final String awsLambdaLogStreamName = PrivilegedActionUtils.getEnv("AWS_LAMBDA_LOG_STREAM_NAME");

public ServerlessConfiguration() {
String lambdaName = PrivilegedActionUtils.getEnv("AWS_LAMBDA_FUNCTION_NAME");
this.runsOnAwsLambda = null != lambdaName && !lambdaName.isEmpty();
Expand Down Expand Up @@ -65,4 +70,9 @@ public boolean runsOnAwsLambda() {
return runsOnAwsLambda;
}

@Override
@Nullable
public String awsLambdaLogStreamName() {
return awsLambdaLogStreamName;
}
}
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,6 +18,9 @@
*/
package co.elastic.apm.agent.context;

import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.tracer.LifecycleListener;

import java.io.Closeable;

public class ClosableLifecycleListenerAdapter extends AbstractLifecycleListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
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.report.serialize.DslJsonDataWriter;
import co.elastic.apm.agent.tracer.reporting.DataWriter;
import co.elastic.apm.agent.tracer.reporting.DoubleSupplier;
import co.elastic.apm.agent.tracer.reporting.Labels;
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 @@ -62,6 +66,7 @@
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.DslJson;
import org.stagemonitor.configuration.ConfigurationOption;
import org.stagemonitor.configuration.ConfigurationOptionProvider;
import org.stagemonitor.configuration.ConfigurationRegistry;
Expand Down Expand Up @@ -106,6 +111,8 @@ public class ElasticApmTracer implements Tracer {

private static volatile boolean classloaderCheckOk = false;

private final DslJson<Object> dslJson = new DslJson<>(new DslJson.Settings<>());

private final ConfigurationRegistry configurationRegistry;
private final StacktraceConfiguration stacktraceConfiguration;
private final ApmServerClient apmServerClient;
Expand Down Expand Up @@ -732,7 +739,7 @@ private synchronized void startSync() {
reporter.start();
for (LifecycleListener lifecycleListener : lifecycleListeners) {
try {
lifecycleListener.start(this);
lifecycleListener.start();
} catch (Exception e) {
logger.error("Failed to start " + lifecycleListener.getClass().getName(), e);
}
Expand Down Expand Up @@ -944,10 +951,37 @@ public MetaDataFuture getMetaDataFuture() {
return metaDataFuture;
}

@Override
public void addMetric(String name, Labels labels, DoubleSupplier metric) {
metricRegistry.add(name, labels, metric);
}

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

@Override
public void reportLog(String log) {
reporter.reportLog(log);
}

@Override
public void reportLog(byte[] log) {
reporter.reportLog(log);
}

@Override
public DataWriter newWriter(int maxSerializedSize) {
return new DslJsonDataWriter(dslJson.newWriter(maxSerializedSize), reporter);
}

@Override
public ScheduledThreadPoolExecutor getSharedSingleThreadedPool() {
return sharedPool;
}

@Override
public void addShutdownHook(Closeable closeable) {
lifecycleListeners.add(ClosableLifecycleListenerAdapter.of(closeable));
}
Expand Down
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 @@ -30,7 +30,9 @@

import javax.annotation.Nullable;

public interface Tracer extends co.elastic.apm.agent.tracer.service.ServiceAwareTracer, co.elastic.apm.agent.tracer.Tracer {
public interface Tracer extends co.elastic.apm.agent.tracer.Tracer,
co.elastic.apm.agent.tracer.reporting.ReportingTracer,
co.elastic.apm.agent.tracer.service.ServiceAwareTracer {

@Nullable
@Override
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.context.AbstractLifecycleListener;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.util.ExecutorUtils;
import co.elastic.apm.agent.sdk.logging.Logger;
Expand Down Expand Up @@ -50,7 +50,7 @@ public CircuitBreaker(ElasticApmTracer tracer) {
}

@Override
public void start(ElasticApmTracer tracer) {
public void start() {
// failsafe loading of stress monitors in isolation
loadGCStressMonitor(tracer);
loadSystemCpuStressMonitor(tracer);
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.reporting.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.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 @@ -101,12 +102,13 @@ public void append(LogEvent event) {
}

public LifecycleListener getInitListener() {
return new AbstractLifecycleListener() {
class InitListener extends AbstractLifecycleListener {
@Override
public void init(ElasticApmTracer tracer) throws Exception {
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();
}

private void initStreaming(LoggingConfiguration config, Reporter reporter) {
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.reporting.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.reporting.DoubleSupplier;
import co.elastic.apm.agent.tracer.reporting.Labels;
import org.HdrHistogram.WriterReaderPhaser;

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

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

import javax.annotation.Nullable;
import java.util.Map;
Expand Down
Loading