Skip to content

Commit

Permalink
elastic#3030: Introduce SPI for plugins to avoid need for dependency …
Browse files Browse the repository at this point in the history
…on main Elastic agent. Refactor plugins in a binary compatible manner to make use of the new API.
  • Loading branch information
raphw committed Feb 22, 2023
1 parent 0f0b724 commit bf59008
Show file tree
Hide file tree
Showing 334 changed files with 2,855 additions and 1,260 deletions.
5 changes: 5 additions & 0 deletions apm-agent-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<artifactId>apm-agent-cached-lookup-key</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apm-agent-plugin-spi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.blogspot.mydailyjava</groupId>
<artifactId>weak-lock-free</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package co.elastic.apm.agent.collections;

import co.elastic.apm.agent.impl.transaction.AbstractSpan;
import co.elastic.apm.plugin.spi.AbstractSpan;

import javax.annotation.Nullable;
import java.util.Iterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
*/
package co.elastic.apm.agent.collections;

import co.elastic.apm.agent.impl.transaction.AbstractSpan;
import co.elastic.apm.agent.sdk.weakconcurrent.DetachedThreadLocal;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakSet;
import co.elastic.apm.plugin.spi.AbstractSpan;
import com.blogspot.mydailyjava.weaklockfree.AbstractWeakConcurrentMap;
import com.blogspot.mydailyjava.weaklockfree.WeakConcurrentSet;

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

import co.elastic.apm.agent.configuration.ServiceInfo;
import co.elastic.apm.agent.impl.transaction.TextHeaderGetterBridge;
import co.elastic.apm.agent.impl.error.ErrorCapture;
import co.elastic.apm.agent.impl.sampling.Sampler;
import co.elastic.apm.agent.impl.transaction.*;
Expand All @@ -28,7 +29,6 @@
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap;
import org.stagemonitor.configuration.ConfigurationOptionProvider;

import javax.annotation.Nullable;
import java.util.Collections;
Expand Down Expand Up @@ -441,11 +441,56 @@ public TracerState getState() {
}

@Override
public <T extends ConfigurationOptionProvider> T getConfig(Class<T> configProvider) {
public <T> T getConfig(Class<T> configProvider) {
try {
return configProvider.getConstructor().newInstance();
} catch (Exception e) {
throw new IllegalStateException("Failed to create empty configuration for " + configProvider.getName(), e);
}
}

@Nullable
@Override
public <C> co.elastic.apm.plugin.spi.Transaction<?> startChildTransaction(@Nullable C headerCarrier, final co.elastic.apm.plugin.spi.TextHeaderGetter<C> textHeadersGetter, @Nullable ClassLoader initiatingClassLoader) {
return startChildTransaction(headerCarrier, new TextHeaderGetterBridge<>(textHeadersGetter), initiatingClassLoader);
}

@Nullable
@Override
public <C> co.elastic.apm.plugin.spi.Transaction<?> startChildTransaction(@Nullable C headerCarrier, co.elastic.apm.plugin.spi.TextHeaderGetter<C> textHeadersGetter, @Nullable ClassLoader initiatingClassLoader, long epochMicros) {
return startChildTransaction(headerCarrier, new TextHeaderGetterBridge<C>(textHeadersGetter), initiatingClassLoader, epochMicros);
}

@Nullable
@Override
public <C> co.elastic.apm.plugin.spi.Transaction<?> startChildTransaction(@Nullable C headerCarrier, co.elastic.apm.plugin.spi.BinaryHeaderGetter<C> binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader) {
return startChildTransaction(headerCarrier, new BinaryHeaderGetterBridge<C>(binaryHeadersGetter), initiatingClassLoader);
}

@Nullable
@Override
public String captureAndReportException(long epochMicros, @Nullable Throwable e, @Nullable co.elastic.apm.plugin.spi.AbstractSpan<?> parent) {
return captureAndReportException(epochMicros, e, (AbstractSpan<?>) parent);
}

@Nullable
@Override
public co.elastic.apm.plugin.spi.ErrorCapture captureException(@Nullable Throwable e, @Nullable co.elastic.apm.plugin.spi.AbstractSpan<?> parent, @Nullable ClassLoader initiatingClassLoader) {
return captureException(e, (AbstractSpan<?>) parent, initiatingClassLoader);
}

@Override
public void endSpan(co.elastic.apm.plugin.spi.Span<?> span) {
endSpan((Span) span);
}

@Override
public void endTransaction(co.elastic.apm.plugin.spi.Transaction<?> transaction) {
endTransaction((Transaction) transaction);
}

@Override
public void endError(co.elastic.apm.plugin.spi.ErrorCapture errorCapture) {
endError((ErrorCapture) errorCapture);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,13 @@ public ConfigurationRegistry getConfigurationRegistry() {
}

@Override
public <T extends ConfigurationOptionProvider> T getConfig(Class<T> configProvider) {
return configurationRegistry.getConfig(configProvider);
@SuppressWarnings({"unchecked", "rawtypes"})
public <T> T getConfig(Class<T> configProvider) {
if (ConfigurationOptionProvider.class.isAssignableFrom(configProvider)) {
return (T) configurationRegistry.getConfig((Class) configProvider);
} else {
throw new IllegalArgumentException("Configuration types must subclass " + ConfigurationOptionProvider.class.getName());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import co.elastic.apm.agent.objectpool.ObjectPoolFactory;
import co.elastic.apm.agent.util.PrivilegedActionUtils;
import co.elastic.apm.agent.util.VersionUtils;
import org.stagemonitor.configuration.ConfigurationOptionProvider;

import javax.annotation.Nullable;
import java.io.File;
Expand Down Expand Up @@ -125,6 +124,7 @@ public static synchronized void init(Tracer tracer) {
if (!isNoop()) {
throw new IllegalStateException("Tracer is already initialized");
}
co.elastic.apm.plugin.spi.GlobalTracer.init(tracer);
INSTANCE.tracer = tracer;
}

Expand Down Expand Up @@ -264,7 +264,7 @@ public void endError(ErrorCapture errorCapture) {
}

@Override
public <T extends ConfigurationOptionProvider> T getConfig(Class<T> configuration) {
public <T> T getConfig(Class<T> configuration) {
return tracer.getConfig(configuration);
}

Expand All @@ -277,4 +277,49 @@ public ObjectPoolFactory getObjectPoolFactory() {
public void recycle(ErrorCapture errorCapture) {
tracer.recycle(errorCapture);
}

@Nullable
@Override
public <C> co.elastic.apm.plugin.spi.Transaction<?> startChildTransaction(@Nullable C headerCarrier, co.elastic.apm.plugin.spi.TextHeaderGetter<C> textHeadersGetter, @Nullable ClassLoader initiatingClassLoader) {
return tracer.startChildTransaction(headerCarrier, textHeadersGetter, initiatingClassLoader);
}

@Nullable
@Override
public <C> co.elastic.apm.plugin.spi.Transaction<?> startChildTransaction(@Nullable C headerCarrier, co.elastic.apm.plugin.spi.TextHeaderGetter<C> textHeadersGetter, @Nullable ClassLoader initiatingClassLoader, long epochMicros) {
return tracer.startChildTransaction(headerCarrier, textHeadersGetter, initiatingClassLoader, epochMicros);
}

@Nullable
@Override
public <C> co.elastic.apm.plugin.spi.Transaction<?> startChildTransaction(@Nullable C headerCarrier, co.elastic.apm.plugin.spi.BinaryHeaderGetter<C> binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader) {
return tracer.startChildTransaction(headerCarrier, binaryHeadersGetter, initiatingClassLoader);
}

@Nullable
@Override
public String captureAndReportException(long epochMicros, @Nullable Throwable e, @Nullable co.elastic.apm.plugin.spi.AbstractSpan<?> parent) {
return tracer.captureAndReportException(epochMicros, e, parent);
}

@Nullable
@Override
public co.elastic.apm.plugin.spi.ErrorCapture captureException(@Nullable Throwable e, @Nullable co.elastic.apm.plugin.spi.AbstractSpan<?> parent, @Nullable ClassLoader initiatingClassLoader) {
return tracer.captureException(e, parent, initiatingClassLoader);
}

@Override
public void endSpan(co.elastic.apm.plugin.spi.Span<?> span) {
tracer.endSpan(span);
}

@Override
public void endTransaction(co.elastic.apm.plugin.spi.Transaction<?> transaction) {
tracer.endTransaction(transaction);
}

@Override
public void endError(co.elastic.apm.plugin.spi.ErrorCapture errorCapture) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import co.elastic.apm.agent.impl.transaction.TextHeaderGetter;
import co.elastic.apm.agent.impl.transaction.Transaction;
import co.elastic.apm.agent.objectpool.ObjectPoolFactory;
import org.stagemonitor.configuration.ConfigurationOptionProvider;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -173,12 +172,54 @@ public void endTransaction(Transaction transaction) { }
public void endError(ErrorCapture errorCapture) { }

@Override
public <T extends ConfigurationOptionProvider> T getConfig(Class<T> configuration) {
public <T> T getConfig(Class<T> configuration) {
return null;
}

@Override
public ObjectPoolFactory getObjectPoolFactory() {
return null;
}

@Nullable
@Override
public <C> co.elastic.apm.plugin.spi.Transaction<?> startChildTransaction(@Nullable C headerCarrier, co.elastic.apm.plugin.spi.TextHeaderGetter<C> textHeadersGetter, @Nullable ClassLoader initiatingClassLoader) {
return null;
}

@Nullable
@Override
public <C> co.elastic.apm.plugin.spi.Transaction<?> startChildTransaction(@Nullable C headerCarrier, co.elastic.apm.plugin.spi.TextHeaderGetter<C> textHeadersGetter, @Nullable ClassLoader initiatingClassLoader, long epochMicros) {
return null;
}

@Nullable
@Override
public <C> co.elastic.apm.plugin.spi.Transaction<?> startChildTransaction(@Nullable C headerCarrier, co.elastic.apm.plugin.spi.BinaryHeaderGetter<C> binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader) {
return null;
}

@Nullable
@Override
public String captureAndReportException(long epochMicros, @Nullable Throwable e, @Nullable co.elastic.apm.plugin.spi.AbstractSpan<?> parent) {
return null;
}

@Nullable
@Override
public co.elastic.apm.plugin.spi.ErrorCapture captureException(@Nullable Throwable e, @Nullable co.elastic.apm.plugin.spi.AbstractSpan<?> parent, @Nullable ClassLoader initiatingClassLoader) {
return null;
}

@Override
public void endSpan(co.elastic.apm.plugin.spi.Span<?> span) {
}

@Override
public void endTransaction(co.elastic.apm.plugin.spi.Transaction<?> transaction) {
}

@Override
public void endError(co.elastic.apm.plugin.spi.ErrorCapture errorCapture) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* {@link co.elastic.apm.agent.impl.transaction.AbstractSpan}.
* </p>
*/
public interface Scope extends AutoCloseable {
public interface Scope extends AutoCloseable, co.elastic.apm.plugin.spi.Scope {

@Override
void close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
import co.elastic.apm.agent.impl.transaction.TextHeaderGetter;
import co.elastic.apm.agent.impl.transaction.Transaction;
import co.elastic.apm.agent.objectpool.ObjectPoolFactory;
import org.stagemonitor.configuration.ConfigurationOptionProvider;

import javax.annotation.Nullable;

public interface Tracer {
public interface Tracer extends co.elastic.apm.plugin.spi.Tracer {

/**
* Starts a trace-root transaction
Expand Down Expand Up @@ -191,7 +190,7 @@ <C> Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGet

void endError(ErrorCapture errorCapture);

<T extends ConfigurationOptionProvider> T getConfig(Class<T> configuration);
<T> T getConfig(Class<T> configuration);

ObjectPoolFactory getObjectPoolFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class AbstractContext implements Recyclable {
public abstract class AbstractContext implements Recyclable, co.elastic.apm.plugin.spi.AbstractContext {

public static final String REDACTED_CONTEXT_STRING = "[REDACTED]";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* If a request originated from a cloud component that provides information about the cloud origin,
* the cloud origin interface can be used to collect this information.
*/
public class CloudOrigin implements Recyclable {
public class CloudOrigin implements Recyclable, co.elastic.apm.plugin.spi.CloudOrigin {

@Nullable
protected String accountId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
/**
* An object containing contextual data for database spans
*/
public class Db implements Recyclable {
public class Db implements Recyclable, co.elastic.apm.plugin.spi.Db {

private static final ObjectPool<CharBuffer> charBufferPool = QueueBasedObjectPool.of(new MpmcAtomicArrayQueue<CharBuffer>(128), false,
new Allocator<CharBuffer>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* Context information about a destination of outgoing calls.
*/
public class Destination implements Recyclable {
public class Destination implements Recyclable, co.elastic.apm.plugin.spi.Destination {

/**
* An IP (v4 or v6) or a host/domain name.
Expand Down Expand Up @@ -184,7 +184,7 @@ public Destination withInetAddress(InetAddress inetAddress) {
return this;
}

public static class Cloud implements Recyclable {
public static class Cloud implements Recyclable, co.elastic.apm.plugin.spi.Cloud {
private final StringBuilder region = new StringBuilder();

public Cloud withRegion(@Nullable String region) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import javax.annotation.Nullable;

public class Http implements Recyclable {
public class Http implements Recyclable, co.elastic.apm.plugin.spi.Http {

/**
* URL used by this HTTP outgoing span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import static co.elastic.apm.agent.impl.context.AbstractContext.REDACTED_CONTEXT_STRING;

public class Message implements Recyclable {
public class Message implements Recyclable, co.elastic.apm.plugin.spi.Message {

private static final ObjectPool<StringBuilder> stringBuilderPool = QueueBasedObjectPool.of(new MpmcAtomicArrayQueue<StringBuilder>(128), false,
new Allocator<StringBuilder>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* <p>
* If a log record was generated as a result of a http request, the http interface can be used to collect this information.
*/
public class Request implements Recyclable {
public class Request implements Recyclable, co.elastic.apm.plugin.spi.Request {


private static final ObjectPool<CharBuffer> charBufferPool = QueueBasedObjectPool.of(new MpmcAtomicArrayQueue<CharBuffer>(128), false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import javax.annotation.Nullable;
import java.util.Collection;

public class Response implements Recyclable {
public class Response implements Recyclable, co.elastic.apm.plugin.spi.Response {

/**
* A mapping of HTTP headers of the response object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* Represents a target service
*/
public class ServiceTarget implements Recyclable {
public class ServiceTarget implements Recyclable, co.elastic.apm.plugin.spi.ServiceTarget {

@Nullable
private String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import javax.annotation.Nullable;

public class Socket implements Recyclable {
public class Socket implements Recyclable, co.elastic.apm.plugin.spi.Socket {

@Nullable
private String remoteAddress;
Expand Down
Loading

0 comments on commit bf59008

Please sign in to comment.