Skip to content

Commit

Permalink
elastic#3030: Removing unused error capture and make use of a tracer …
Browse files Browse the repository at this point in the history
…subtype in one module to demonstrate expandability.
  • Loading branch information
raphw committed Feb 22, 2023
1 parent 73ee8a8 commit 5d0a5f7
Show file tree
Hide file tree
Showing 22 changed files with 135 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap;
import co.elastic.apm.plugin.spi.*;
import co.elastic.apm.plugin.spi.Tracer;

import javax.annotation.Nullable;
import java.util.Collections;
Expand Down Expand Up @@ -487,12 +488,6 @@ public String captureAndReportException(long epochMicros, @Nullable Throwable e,
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);
Expand All @@ -503,11 +498,6 @@ 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()
Expand All @@ -519,4 +509,23 @@ public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm
public ServiceInfo autoDetectedServiceName() {
return ServiceInfo.autoDetected();
}

@Nullable
@Override
public <T extends Tracer> T probe(Class<T> type) {
if (type.isInstance(this)) {
return type.cast(this);
} else {
return null;
}
}

@Override
public <T extends Tracer> T require(Class<T> type) {
T tracer = probe(type);
if (tracer == null) {
throw new IllegalStateException(this + " tracer does not support features of " + type.getName());
}
return tracer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,9 @@ public static Tracer get() {
return INSTANCE;
}

@Nullable
public static <T extends Tracer> T get(Class<T> type) {
Tracer tracer = INSTANCE.tracer;
if (type.isInstance(tracer)) {
return type.cast(tracer);
}
return null;
}

public static <T extends Tracer> T require(Class<T> type) {
return Objects.requireNonNull(get(type), "Registered tracer is not an instance of " + type.getName());
}

@Nullable
public static ElasticApmTracer getTracerImpl() {
return get(ElasticApmTracer.class);
return get().probe(ElasticApmTracer.class);
}

public static ElasticApmTracer requireTracerImpl() {
Expand Down Expand Up @@ -302,12 +289,6 @@ public String captureAndReportException(long epochMicros, @Nullable Throwable e,
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);
Expand All @@ -318,11 +299,6 @@ 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);
Expand All @@ -332,4 +308,15 @@ public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm
public ServiceInfo autoDetectedServiceName() {
return tracer.autoDetectedServiceName();
}

@Nullable
@Override
public <T extends co.elastic.apm.plugin.spi.Tracer> T probe(Class<T> type) {
return tracer.probe(type);
}

@Override
public <T extends co.elastic.apm.plugin.spi.Tracer> T require(Class<T> type) {
return tracer.require(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,22 @@
*/
package co.elastic.apm.agent.impl;

import co.elastic.apm.agent.impl.metadata.MetaDataFuture;
import co.elastic.apm.agent.metrics.MetricRegistry;
import co.elastic.apm.agent.report.Reporter;

import java.io.Closeable;
import java.util.concurrent.ScheduledExecutorService;

public interface MetricsAwareTracer extends SpanAwareTracer {

MetricRegistry getMetricRegistry();

Reporter getReporter();

void addShutdownHook(Closeable closeable);

ScheduledExecutorService getSharedSingleThreadedPool();

MetaDataFuture getMetaDataFuture();
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,30 +205,31 @@ public String captureAndReportException(long epochMicros, @Nullable Throwable e,
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;
public void endSpan(co.elastic.apm.plugin.spi.Span<?> span) {
}

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

@Override
public void endTransaction(co.elastic.apm.plugin.spi.Transaction<?> transaction) {
public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm.plugin.spi.ServiceInfo serviceInfo) {
}

@Override
public void endError(co.elastic.apm.plugin.spi.ErrorCapture errorCapture) {
public ServiceInfo autoDetectedServiceName() {
return null;
}

@Nullable
@Override
public void setServiceInfoForClassLoader(ClassLoader classLoader, co.elastic.apm.plugin.spi.ServiceInfo serviceInfo) {
public <T extends co.elastic.apm.plugin.spi.Tracer> T probe(Class<T> type) {
return null;
}

@Override
public ServiceInfo autoDetectedServiceName() {
public <T extends co.elastic.apm.plugin.spi.Tracer> T require(Class<T> type) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package co.elastic.apm.plugin.spi;

import javax.annotation.Nullable;
import java.util.Objects;

public class GlobalTracer implements Tracer {

Expand All @@ -33,19 +32,6 @@ public static Tracer get() {
return INSTANCE;
}

@Nullable
public static <T extends Tracer> T get(Class<T> type) {
Tracer tracer = INSTANCE.tracer;
if (type.isInstance(tracer)) {
return type.cast(tracer);
}
return null;
}

public static <T extends Tracer> T require(Class<T> type) {
return Objects.requireNonNull(get(type), "Registered tracer is not an instance of " + type.getName());
}

public static synchronized void init(Tracer tracer) {
if (!isNoop()) {
throw new IllegalStateException("Tracer is already initialized");
Expand Down Expand Up @@ -110,11 +96,6 @@ public String captureAndReportException(long epochMicros, @Nullable Throwable e,
return tracer.captureAndReportException(epochMicros, e, parent);
}

@Nullable
public ErrorCapture captureException(@Nullable Throwable e, @Nullable AbstractSpan<?> parent, @Nullable ClassLoader initiatingClassLoader) {
return tracer.captureException(e, parent, initiatingClassLoader);
}

@Nullable
@Override
public Span<?> getActiveExitSpan() {
Expand All @@ -137,11 +118,6 @@ public void endTransaction(Transaction<?> transaction) {
tracer.endTransaction(transaction);
}

@Override
public void endError(ErrorCapture errorCapture) {
tracer.endError(errorCapture);
}

@Override
public <T> T getConfig(Class<T> configuration) {
return tracer.getConfig(configuration);
Expand Down Expand Up @@ -171,4 +147,15 @@ public ServiceInfo getServiceInfoForClassLoader(ClassLoader classLoader) {
public ServiceInfo autoDetectedServiceName() {
return tracer.autoDetectedServiceName();
}

@Nullable
@Override
public <T extends Tracer> T probe(Class<T> type) {
return tracer.probe(type);
}

@Override
public <T extends Tracer> T require(Class<T> type) {
return tracer.require(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ public String captureAndReportException(long epochMicros, @Nullable Throwable e,
return null;
}

@Nullable
@Override
public ErrorCapture captureException(@Nullable Throwable e, @Nullable AbstractSpan<?> parent, @Nullable ClassLoader initiatingClassLoader) {
return null;
}

@Nullable
@Override
public Span<?> getActiveExitSpan() {
Expand All @@ -93,10 +87,6 @@ public void endSpan(Span<?> span) {
public void endTransaction(Transaction<?> transaction) {
}

@Override
public void endError(ErrorCapture errorCapture) {
}

@Override
public <T> T getConfig(Class<T> configuration) {
return null;
Expand Down Expand Up @@ -125,4 +115,15 @@ public ServiceInfo getServiceInfoForClassLoader(ClassLoader classLoader) {
public ServiceInfo autoDetectedServiceName() {
return null;
}

@Nullable
@Override
public <T extends Tracer> T probe(Class<T> type) {
return null;
}

@Override
public <T extends Tracer> T require(Class<T> type) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ public interface Tracer {
@Nullable
String captureAndReportException(long epochMicros, @Nullable Throwable e, @Nullable AbstractSpan<?> parent);

@Nullable
ErrorCapture captureException(@Nullable Throwable e, @Nullable AbstractSpan<?> parent, @Nullable ClassLoader initiatingClassLoader);

@Nullable
Span<?> getActiveExitSpan();

Expand All @@ -64,8 +61,6 @@ public interface Tracer {

void endTransaction(Transaction<?> transaction);

void endError(ErrorCapture errorCapture);

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

ObjectPoolFactory getObjectPoolFactory();
Expand All @@ -77,4 +72,9 @@ public interface Tracer {
ServiceInfo getServiceInfoForClassLoader(ClassLoader classLoader);

ServiceInfo autoDetectedServiceName();

@Nullable
<T extends Tracer> T probe(Class<T> type);

<T extends Tracer> T require(Class<T> type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
package co.elastic.apm.agent.awslambda.helper;

import co.elastic.apm.agent.awslambda.MapTextHeaderGetter;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.impl.GlobalTracer;
import co.elastic.apm.agent.impl.MetricsAwareTracer;
import co.elastic.apm.agent.impl.transaction.Transaction;
import co.elastic.apm.agent.util.PrivilegedActionUtils;
import co.elastic.apm.plugin.spi.GlobalTracer;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
Expand All @@ -35,13 +35,13 @@ public class APIGatewayProxyV1TransactionHelper extends AbstractAPIGatewayTransa
@Nullable
private static APIGatewayProxyV1TransactionHelper INSTANCE;

private APIGatewayProxyV1TransactionHelper(ElasticApmTracer tracer) {
private APIGatewayProxyV1TransactionHelper(MetricsAwareTracer tracer) {
super(tracer);
}

public static APIGatewayProxyV1TransactionHelper getInstance() {
if (INSTANCE == null) {
INSTANCE = new APIGatewayProxyV1TransactionHelper(GlobalTracer.requireTracerImpl());
INSTANCE = new APIGatewayProxyV1TransactionHelper(GlobalTracer.get().require(MetricsAwareTracer.class));
}
return INSTANCE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
package co.elastic.apm.agent.awslambda.helper;

import co.elastic.apm.agent.awslambda.MapTextHeaderGetter;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.impl.GlobalTracer;
import co.elastic.apm.agent.impl.MetricsAwareTracer;
import co.elastic.apm.agent.impl.transaction.Transaction;
import co.elastic.apm.agent.util.PrivilegedActionUtils;
import co.elastic.apm.plugin.spi.GlobalTracer;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse;
Expand All @@ -33,13 +33,13 @@ public class APIGatewayProxyV2TransactionHelper extends AbstractAPIGatewayTransa
@Nullable
private static APIGatewayProxyV2TransactionHelper INSTANCE;

private APIGatewayProxyV2TransactionHelper(ElasticApmTracer tracer) {
private APIGatewayProxyV2TransactionHelper(MetricsAwareTracer tracer) {
super(tracer);
}

public static APIGatewayProxyV2TransactionHelper getInstance() {
if (INSTANCE == null) {
INSTANCE = new APIGatewayProxyV2TransactionHelper(GlobalTracer.requireTracerImpl());
INSTANCE = new APIGatewayProxyV2TransactionHelper(GlobalTracer.get().require(MetricsAwareTracer.class));
}
return INSTANCE;
}
Expand Down
Loading

0 comments on commit 5d0a5f7

Please sign in to comment.