diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java index 92b5c9631a..1b7729ad75 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java @@ -348,6 +348,12 @@ public Transaction currentTransaction() { return currentContext().getTransaction(); } + @Nullable + @Override + public co.elastic.apm.agent.tracer.ErrorCapture getActiveError() { + return ErrorCapture.getActive(); + } + /** * Starts a span with a given parent context. *

@@ -421,6 +427,12 @@ public ErrorCapture captureException(@Nullable Throwable e, ElasticContext pa return captureException(System.currentTimeMillis() * 1000, e, parentContext, initiatingClassLoader); } + @Nullable + @Override + public ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader) { + return captureException(System.currentTimeMillis() * 1000, e, currentContext(), initiatingClassLoader); + } + @Nullable private ErrorCapture captureException(long epochMicros, @Nullable Throwable e, ElasticContext parentContext, @Nullable ClassLoader initiatingClassLoader) { if (!isRunning() || e == null) { diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/error/ErrorCapture.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/error/ErrorCapture.java index 5f63869587..5be5431390 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/error/ErrorCapture.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/error/ErrorCapture.java @@ -37,7 +37,7 @@ /** * Data captured by an agent representing an event occurring in a monitored service */ -public class ErrorCapture implements Recyclable { +public class ErrorCapture implements Recyclable, co.elastic.apm.agent.tracer.ErrorCapture { private static final Logger logger = LoggerFactory.getLogger(ErrorCapture.class); @@ -147,6 +147,7 @@ public ErrorCapture asChildOf(AbstractSpan parent) { return this; } + @Override public TraceContext getTraceContext() { return traceContext; } @@ -199,11 +200,13 @@ private void setCulprit(StackTraceElement stackTraceElement) { culprit.append(')'); } + @Override public ErrorCapture activate() { activeError.set(this); return this; } + @Override public ErrorCapture deactivate() { activeError.remove(); return this; @@ -267,6 +270,7 @@ public void setTransactionType(@Nullable String type) { transactionInfo.type = type; } + @Override public void end() { tracer.endError(this); } diff --git a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/CaptureExceptionInstrumentation.java b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/CaptureExceptionInstrumentation.java index 7d7f1f179d..4afe5ef561 100644 --- a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/CaptureExceptionInstrumentation.java +++ b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/CaptureExceptionInstrumentation.java @@ -18,8 +18,8 @@ */ package co.elastic.apm.agent.pluginapi; -import co.elastic.apm.agent.impl.Tracer; import co.elastic.apm.agent.sdk.internal.util.PrivilegedActionUtils; +import co.elastic.apm.agent.tracer.ErrorCapture; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; @@ -33,7 +33,10 @@ public class CaptureExceptionInstrumentation extends ApiInstrumentation { public static class AdviceClass { @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static void captureException(@Advice.Origin Class clazz, @Advice.Argument(0) Throwable t) { - tracer.require(Tracer.class).captureAndReportException(t, PrivilegedActionUtils.getClassLoader(clazz)); + ErrorCapture errorCapture = tracer.captureException(t, PrivilegedActionUtils.getClassLoader(clazz)); + if (errorCapture != null) { + errorCapture.end(); + } } } diff --git a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/ElasticApmApiInstrumentation.java b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/ElasticApmApiInstrumentation.java index 105d55a0c7..6be447f2f4 100644 --- a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/ElasticApmApiInstrumentation.java +++ b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/ElasticApmApiInstrumentation.java @@ -21,6 +21,7 @@ import co.elastic.apm.agent.configuration.ServiceInfo; import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.impl.Tracer; +import co.elastic.apm.agent.tracer.ErrorCapture; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.sdk.internal.util.PrivilegedActionUtils; @@ -149,7 +150,10 @@ public CaptureExceptionInstrumentation() { public static class AdviceClass { @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static void captureException(@Advice.Origin Class clazz, @Advice.Argument(0) @Nullable Throwable e) { - tracer.require(Tracer.class).captureAndReportException(e, PrivilegedActionUtils.getClassLoader(clazz)); + ErrorCapture errorCapture = tracer.captureException(e, PrivilegedActionUtils.getClassLoader(clazz)); + if (errorCapture != null) { + errorCapture.end(); + } } } } diff --git a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/correlation/Log4j2_7PlusLogCorrelationHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/correlation/Log4j2_7PlusLogCorrelationHelper.java index 65e4971f0c..0551af209a 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/correlation/Log4j2_7PlusLogCorrelationHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/correlation/Log4j2_7PlusLogCorrelationHelper.java @@ -36,7 +36,7 @@ public class Log4j2_7PlusLogCorrelationHelper extends AbstractLogCorrelationHelp @Override protected boolean addToMdc() { - if (tracer.currentTransaction() == null && ErrorCapture.getActive() == null) { + if (tracer.currentTransaction() == null && tracer.getActiveError() == null) { return false; } ThreadContext.putAll(CorrelationIdMapAdapter.get()); diff --git a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/correlation/AbstractLogCorrelationHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/correlation/AbstractLogCorrelationHelper.java index a939ca184f..09828a84d0 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/correlation/AbstractLogCorrelationHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/correlation/AbstractLogCorrelationHelper.java @@ -19,8 +19,8 @@ package co.elastic.apm.agent.loginstr.correlation; import co.elastic.apm.agent.tracer.AbstractSpan; +import co.elastic.apm.agent.tracer.ErrorCapture; import co.elastic.apm.agent.tracer.GlobalTracer; -import co.elastic.apm.agent.impl.error.ErrorCapture; import co.elastic.apm.agent.sdk.state.CallDepth; import co.elastic.apm.agent.sdk.state.GlobalState; import co.elastic.apm.agent.tracer.Tracer; @@ -80,7 +80,7 @@ protected boolean addToMdc() { addToMdc(TRANSACTION_ID_MDC_KEY, activeSpan.getTraceContext().getTransactionId().toString()); addedToMdc = true; } - ErrorCapture activeError = ErrorCapture.getActive(); + ErrorCapture activeError = tracer.getActiveError(); if (activeError != null) { addToMdc(ERROR_ID_MDC_KEY, activeError.getTraceContext().getId().toString()); addedToMdc = true; diff --git a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/correlation/CorrelationIdMapAdapter.java b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/correlation/CorrelationIdMapAdapter.java index 7262590c8e..7548453060 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/correlation/CorrelationIdMapAdapter.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/correlation/CorrelationIdMapAdapter.java @@ -19,8 +19,8 @@ package co.elastic.apm.agent.loginstr.correlation; import co.elastic.apm.agent.tracer.AbstractSpan; +import co.elastic.apm.agent.tracer.ErrorCapture; import co.elastic.apm.agent.tracer.GlobalTracer; -import co.elastic.apm.agent.impl.error.ErrorCapture; import co.elastic.apm.agent.tracer.Tracer; import javax.annotation.Nullable; @@ -71,7 +71,7 @@ public String call() { @Override @Nullable public String call() { - ErrorCapture error = ErrorCapture.getActive(); + ErrorCapture error = tracer.getActiveError(); if (error == null) { return null; } diff --git a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/error/LoggerErrorHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/error/LoggerErrorHelper.java index a534c19548..ce88c8b356 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/error/LoggerErrorHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/error/LoggerErrorHelper.java @@ -18,7 +18,7 @@ */ package co.elastic.apm.agent.loginstr.error; -import co.elastic.apm.agent.impl.error.ErrorCapture; +import co.elastic.apm.agent.tracer.ErrorCapture; import co.elastic.apm.agent.sdk.internal.util.PrivilegedActionUtils; import co.elastic.apm.agent.sdk.state.CallDepth; import co.elastic.apm.agent.tracer.Tracer; @@ -46,8 +46,7 @@ public LoggerErrorHelper(Class adviceClass, Tracer tracer) { public Object enter(@Nullable Throwable exception, Class originClass) { if (!callDepth.isNestedCallAndIncrement()) { if (exception != null) { - co.elastic.apm.agent.impl.Tracer required = tracer.require(co.elastic.apm.agent.impl.Tracer.class); - ErrorCapture error = required.captureException(exception, required.currentContext(), PrivilegedActionUtils.getClassLoader(originClass)); + ErrorCapture error = tracer.captureException(exception, PrivilegedActionUtils.getClassLoader(originClass)); if (error != null) { error.activate(); } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java index 8e45b76c59..e164291c4d 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java @@ -23,7 +23,7 @@ import javax.annotation.Nullable; -public interface AbstractSpan> extends Activateable, ReferenceCounted { +public interface AbstractSpan> extends ActivateableInScope, ReferenceCounted { int PRIORITY_DEFAULT = 0; int PRIORITY_LOW_LEVEL_FRAMEWORK = 10; diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Activateable.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Activateable.java index 737d5276b0..196a393a05 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Activateable.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Activateable.java @@ -33,11 +33,4 @@ public interface Activateable> { * @return this */ T deactivate(); - - /** - * Activates context in a scope - * - * @return active scope that will deactivate context when closed - */ - Scope activateInScope(); } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/ActivateableInScope.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/ActivateableInScope.java new file mode 100644 index 0000000000..4b9b4eeec8 --- /dev/null +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/ActivateableInScope.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.tracer; + +public interface ActivateableInScope> extends Activateable { + + /** + * Activates context in a scope + * + * @return active scope that will deactivate context when closed + */ + Scope activateInScope(); +} diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/ElasticContext.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/ElasticContext.java index ab1536f093..4f86c8bd6a 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/ElasticContext.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/ElasticContext.java @@ -24,7 +24,7 @@ import javax.annotation.Nullable; -public interface ElasticContext> extends ReferenceCounted, Activateable { +public interface ElasticContext> extends ActivateableInScope, ReferenceCounted { /** * @return the span/transaction that is associated to this context, {@literal null} if there is none diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/ErrorCapture.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/ErrorCapture.java new file mode 100644 index 0000000000..2441b0e825 --- /dev/null +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/ErrorCapture.java @@ -0,0 +1,26 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.tracer; + +public interface ErrorCapture extends Activateable { + + TraceContext getTraceContext(); + + void end(); +} diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java index 8f011fd7a3..031f927381 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java @@ -110,6 +110,12 @@ public Transaction currentTransaction() { return tracer.currentTransaction(); } + @Nullable + @Override + public ErrorCapture getActiveError() { + return tracer.getActiveError(); + } + @Nullable @Override public Transaction startRootTransaction(@Nullable ClassLoader initiatingClassLoader) { @@ -122,4 +128,9 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, He return tracer.startChildTransaction(headerCarrier, textHeadersGetter, initiatingClassLoader); } + @Nullable + @Override + public ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader) { + return tracer.captureException(e, initiatingClassLoader); + } } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java index 1556ead3dc..6088e75683 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java @@ -87,6 +87,12 @@ public Transaction currentTransaction() { return null; } + @Nullable + @Override + public ErrorCapture getActiveError() { + return null; + } + @Nullable @Override public Transaction startRootTransaction(@Nullable ClassLoader initiatingClassLoader) { @@ -99,4 +105,9 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, He return null; } + @Nullable + @Override + public ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader) { + return null; + } } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java index e2eaf77bd6..97f8e8bb87 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java @@ -43,7 +43,6 @@ public interface Tracer { Set getTraceHeaderNames(); - ElasticContext currentContext(); @Nullable @@ -52,6 +51,9 @@ public interface Tracer { @Nullable Transaction currentTransaction(); + @Nullable + ErrorCapture getActiveError(); + /** * Starts a trace-root transaction * @@ -76,4 +78,6 @@ public interface Tracer { @Nullable Transaction startChildTransaction(@Nullable C headerCarrier, HeaderGetter textHeadersGetter, @Nullable ClassLoader initiatingClassLoader); + @Nullable + ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader); }