Skip to content

Commit

Permalink
Introduce Tracer API for log capture. (#3515)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: Sylvain Juge <[email protected]>
  • Loading branch information
raphw and SylvainJuge authored Feb 9, 2024
1 parent 414860e commit d178642
Show file tree
Hide file tree
Showing 24 changed files with 203 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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.impl.metadata.ServiceFactory;
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;
Expand Down Expand Up @@ -973,4 +975,24 @@ public Set<String> getTraceHeaderNames() {
public ServiceInfo autoDetectedServiceInfo() {
return AutoDetectedServiceInfo.autoDetected();
}

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

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

@Nullable
@Override
public Service createService(String ephemeralId) {
return new ServiceFactory().createService(
coreConfiguration,
ephemeralId,
configurationRegistry.getConfig(ServerlessConfiguration.class).runsOnAwsLambda()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* A representation of a service node, ie JVM
*/
public class Node {
public class Node implements co.elastic.apm.agent.tracer.service.Node {

/**
* (Optional)
Expand All @@ -37,6 +37,7 @@ public Node(@Nullable String name) {
}

@Nullable
@Override
public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* Information about the instrumented Service
*/
public class Service {
public class Service implements co.elastic.apm.agent.tracer.service.Service {

/**
* Name and version of the Elastic APM agent
Expand Down Expand Up @@ -127,10 +127,8 @@ public Service withLanguage(Language language) {
return this;
}

/**
* Representation of a service node
*/
@Nullable
@Override
public Node getNode() {
return node;
}
Expand All @@ -143,11 +141,8 @@ public Service withNode(Node node) {
return this;
}

/**
* Immutable name of the service emitting this event
* (Required)
*/
@Nullable
@Override
public String getName() {
return name;
}
Expand All @@ -161,10 +156,8 @@ public Service withName(String name) {
return this;
}

/**
* Environment name of the service, e.g. "production" or "staging"
*/
@Nullable
@Override
public String getEnvironment() {
return environment;
}
Expand Down Expand Up @@ -193,10 +186,8 @@ public Service withRuntime(RuntimeInfo runtime) {
return this;
}

/**
* Version of the service emitting this event
*/
@Nullable
@Override
public String getVersion() {
return version;
}
Expand Down
7 changes: 0 additions & 7 deletions apm-agent-plugins/apm-ecs-logging-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@
</properties>

<dependencies>
<!-- This plugin requires further refactoring to avoid agent dependencies. -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apm-agent-core</artifactId>
<version>${project.version}</version>
</dependency>

<!-- log4j 1.x -->
<dependency>
<groupId>co.elastic.logging</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package co.elastic.apm.agent.jul.error;

import co.elastic.apm.agent.impl.Tracer;
import co.elastic.apm.agent.loginstr.error.AbstractLoggerErrorCapturingInstrumentation;
import co.elastic.apm.agent.loginstr.error.LoggerErrorHelper;
import net.bytebuddy.asm.Advice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import co.elastic.apm.agent.jul.sending.JulLogSenderHandler;
import co.elastic.apm.agent.loginstr.correlation.CorrelationIdMapAdapter;
import co.elastic.apm.agent.loginstr.reformatting.AbstractEcsReformattingHelper;
import co.elastic.apm.agent.report.Reporter;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import co.elastic.apm.agent.sdk.internal.util.LoggerUtils;
import co.elastic.apm.agent.tracer.Tracer;
import co.elastic.logging.AdditionalField;
import co.elastic.logging.jul.EcsFormatter;

Expand Down Expand Up @@ -151,8 +151,8 @@ private static FileHandler doCreateFileHandler(String pattern, int maxLogFileSiz
protected abstract boolean isFileHandler(Handler originalHandler);

@Override
protected T createAndStartLogSendingAppender(Reporter reporter, Formatter formatter) {
return (T) new JulLogSenderHandler(reporter, formatter);
protected T createAndStartLogSendingAppender(Tracer tracer, Formatter formatter) {
return (T) new JulLogSenderHandler(tracer, formatter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@
*/
package co.elastic.apm.agent.jul.sending;

import co.elastic.apm.agent.report.Reporter;
import co.elastic.apm.agent.tracer.Tracer;

import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class JulLogSenderHandler extends Handler {
private final Reporter reporter;
private final Tracer tracer;
private final Formatter formatter;

public JulLogSenderHandler(Reporter reporter, Formatter formatter) {
this.reporter = reporter;
public JulLogSenderHandler(Tracer tracer, Formatter formatter) {
this.tracer = tracer;
this.formatter = formatter;
}

@Override
public void publish(LogRecord record) {
reporter.reportLog(formatter.format(record));
tracer.reportLog(formatter.format(record));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import co.elastic.apm.agent.loginstr.reformatting.AbstractEcsReformattingHelper;
import co.elastic.apm.agent.loginstr.reformatting.Utils;

import co.elastic.apm.agent.report.Reporter;
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.logging.log4j.EcsLayout;
import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
Expand Down Expand Up @@ -112,8 +112,8 @@ protected void closeShadeAppender(WriterAppender shadeAppender) {
}

@Override
protected Appender createAndStartLogSendingAppender(Reporter reporter, Layout formatter) {
return new LogSenderAppender(reporter, formatter);
protected Appender createAndStartLogSendingAppender(Tracer tracer, Layout formatter) {
return new LogSenderAppender(tracer, formatter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
*/
package co.elastic.apm.agent.log4j1.sending;

import co.elastic.apm.agent.report.Reporter;
import co.elastic.apm.agent.tracer.Tracer;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;

public class LogSenderAppender extends AppenderSkeleton {
private final Reporter reporter;
private final Tracer tracer;
private final Layout formatter;

public LogSenderAppender(Reporter reporter, Layout formatter) {
this.reporter = reporter;
public LogSenderAppender(Tracer tracer, Layout formatter) {
this.tracer = tracer;
this.formatter = formatter;
}

Expand All @@ -39,7 +39,7 @@ public synchronized void doAppend(LoggingEvent event) {

@Override
protected void append(LoggingEvent event) {
reporter.reportLog(formatter.format(event));
tracer.reportLog(formatter.format(event));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package co.elastic.apm.agent.log4j2.correlation;

import co.elastic.apm.agent.tracer.GlobalTracer;
import co.elastic.apm.agent.impl.error.ErrorCapture;
import co.elastic.apm.agent.loginstr.correlation.AbstractLogCorrelationHelper;
import co.elastic.apm.agent.loginstr.correlation.CorrelationIdMapAdapter;
import co.elastic.apm.agent.tracer.Tracer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import co.elastic.apm.agent.log4j2.sending.Log4j2LogSenderAppender;
import co.elastic.apm.agent.loginstr.reformatting.AbstractEcsReformattingHelper;
import co.elastic.apm.agent.loginstr.reformatting.Utils;
import co.elastic.apm.agent.report.Reporter;
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.logging.log4j2.EcsLayout;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Layout;
Expand Down Expand Up @@ -155,8 +155,8 @@ protected void closeShadeAppender(Appender shadeAppender) {
}

@Override
protected Appender createAndStartLogSendingAppender(Reporter reporter, Layout<? extends Serializable> ecsLayout) {
Log4j2LogSenderAppender appender = new Log4j2LogSenderAppender(reporter, (StringLayout) ecsLayout);
protected Appender createAndStartLogSendingAppender(Tracer tracer, Layout<? extends Serializable> ecsLayout) {
Log4j2LogSenderAppender appender = new Log4j2LogSenderAppender(tracer, (StringLayout) ecsLayout);
appender.start();
return appender;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
*/
package co.elastic.apm.agent.log4j2.sending;

import co.elastic.apm.agent.report.Reporter;
import co.elastic.apm.agent.tracer.Tracer;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.StringLayout;
import org.apache.logging.log4j.core.appender.AbstractAppender;

public class Log4j2LogSenderAppender extends AbstractAppender {

private final Reporter reporter;
private final Tracer tracer;
private final StringLayout ecsLayout;

public Log4j2LogSenderAppender(Reporter reporter, StringLayout ecsLayout) {
public Log4j2LogSenderAppender(Tracer tracer, StringLayout ecsLayout) {
super("ElasticApmAppender", null, ecsLayout, true, null);
this.reporter = reporter;
this.tracer = tracer;
this.ecsLayout = ecsLayout;
}

@Override
public void append(LogEvent event) {
reporter.reportLog(ecsLayout.toSerializable(event));
tracer.reportLog(ecsLayout.toSerializable(event));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import co.elastic.apm.agent.loginstr.reformatting.AbstractEcsReformattingHelper;
import co.elastic.apm.agent.loginstr.reformatting.Utils;
import co.elastic.apm.agent.common.util.WildcardMatcher;
import co.elastic.apm.agent.report.Reporter;
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.logging.AdditionalField;
import co.elastic.logging.logback.EcsEncoder;

Expand Down Expand Up @@ -158,8 +158,8 @@ protected void closeShadeAppender(OutputStreamAppender<ILoggingEvent> shadeAppen
}

@Override
protected Appender<ILoggingEvent> createAndStartLogSendingAppender(Reporter reporter, Encoder<ILoggingEvent> formatter) {
LogbackLogSenderAppender appender = new LogbackLogSenderAppender(reporter, formatter);
protected Appender<ILoggingEvent> createAndStartLogSendingAppender(Tracer tracer, Encoder<ILoggingEvent> formatter) {
LogbackLogSenderAppender appender = new LogbackLogSenderAppender(tracer, formatter);
appender.start();
return appender;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import ch.qos.logback.core.encoder.Encoder;
import co.elastic.apm.agent.report.Reporter;
import co.elastic.apm.agent.tracer.Tracer;
import co.elastic.logging.logback.EcsEncoder;

public class LogbackLogSenderAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
private final Reporter reporter;
private final Tracer tracer;
private final EcsEncoder formatter;

public LogbackLogSenderAppender(Reporter reporter, Encoder<ILoggingEvent> formatter) {
this.reporter = reporter;
public LogbackLogSenderAppender(Tracer tracer, Encoder<ILoggingEvent> formatter) {
this.tracer = tracer;
// Due to API compatibility (see below in 'append'), we have to use our own formatter type rather than the
// base/interface class from logback.
if (!(formatter instanceof EcsEncoder)) {
Expand All @@ -42,6 +42,6 @@ public LogbackLogSenderAppender(Reporter reporter, Encoder<ILoggingEvent> format
protected void append(ILoggingEvent eventObject) {
// the Formatter interface was changed in logback 1.x, but our ECS implementation is compatible with both
// older and newer versions of the API so we can rely on the more recent version of the API
reporter.reportLog(formatter.encode(eventObject));
tracer.reportLog(formatter.encode(eventObject));
}
}
Loading

0 comments on commit d178642

Please sign in to comment.