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 2c5f251
Show file tree
Hide file tree
Showing 336 changed files with 3,016 additions and 1,276 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 @@ -28,18 +28,13 @@
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class ServiceInfo {
public class ServiceInfo extends co.elastic.apm.plugin.spi.ServiceInfo {

private static final String JAR_VERSION_SUFFIX = "-(\\d+\\.)+(\\d+)(.*)?$";
private static final String DEFAULT_SERVICE_NAME = "unknown-java-service";
private static final ServiceInfo EMPTY = new ServiceInfo(null, null);
private static final ServiceInfo AUTO_DETECTED = autoDetect(System.getProperties(), PrivilegedActionUtils.getEnv());

private final String serviceName;
@Nullable
private final String serviceVersion;
private final boolean multiServiceContainer;

public ServiceInfo(@Nullable String serviceName) {
this(serviceName, null);
}
Expand All @@ -49,13 +44,7 @@ private ServiceInfo(@Nullable String serviceName, @Nullable String serviceVersio
}

private ServiceInfo(@Nullable String serviceName, @Nullable String serviceVersion, boolean multiServiceContainer) {
this.multiServiceContainer = multiServiceContainer;
if (serviceName == null || serviceName.trim().isEmpty()) {
this.serviceName = DEFAULT_SERVICE_NAME;
} else {
this.serviceName = replaceDisallowedServiceNameChars(serviceName).trim();
}
this.serviceVersion = serviceVersion;
super(serviceName, serviceVersion, multiServiceContainer);
}

public static ServiceInfo empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 @@ -326,11 +325,11 @@ protected boolean hasServiceName() {

@Nullable
@Override
public ServiceInfo getServiceInfoForClassLoader(@Nullable ClassLoader initiatingClassLoader) {
if (initiatingClassLoader == null) {
public ServiceInfo getServiceInfoForClassLoader(@Nullable ClassLoader classLoader) {
if (classLoader == null) {
return null;
}
return serviceInfoByClassLoader.get(initiatingClassLoader);
return serviceInfoByClassLoader.get(classLoader);
}

@Override
Expand Down Expand Up @@ -441,11 +440,63 @@ 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);
}

@Override
public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm.plugin.spi.ServiceInfo serviceInfo) {
setServiceInfoForClassLoader(classLoader, serviceInfo.isMultiServiceContainer()
? ServiceInfo.ofMultiServiceContainer(serviceInfo.getServiceName())
: ServiceInfo.of(serviceInfo.getServiceName(), serviceInfo.getServiceVersion()));
}
}
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,54 @@ 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) {
tracer.endError(errorCapture);
}

@Override
public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm.plugin.spi.ServiceInfo serviceInfo) {
tracer.setServiceInfoForClassLoader(classLoader, serviceInfo);
}
}
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,58 @@ 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) {
}

@Override
public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm.plugin.spi.ServiceInfo serviceInfo) {
}
}
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
Loading

0 comments on commit 2c5f251

Please sign in to comment.