Skip to content

Commit

Permalink
Add error reporting API to tracer-api. (#3273)
Browse files Browse the repository at this point in the history
* Add error reporting API to tracer-api.

* Added missing @OverRide annotations

---------

Co-authored-by: Jonas Kunz <[email protected]>
  • Loading branch information
raphw and JonasKunz authored Sep 8, 2023
1 parent 30f5f3d commit e81f082
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -147,6 +147,7 @@ public ErrorCapture asChildOf(AbstractSpan<?> parent) {
return this;
}

@Override
public TraceContext getTraceContext() {
return traceContext;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -267,6 +270,7 @@ public void setTransactionType(@Nullable String type) {
transactionInfo.type = type;
}

@Override
public void end() {
tracer.endError(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import javax.annotation.Nullable;

public interface AbstractSpan<T extends AbstractSpan<T>> extends Activateable<T>, ReferenceCounted {
public interface AbstractSpan<T extends AbstractSpan<T>> extends ActivateableInScope<T>, ReferenceCounted {

int PRIORITY_DEFAULT = 0;
int PRIORITY_LOW_LEVEL_FRAMEWORK = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,4 @@ public interface Activateable<T extends Activateable<T>> {
* @return this
*/
T deactivate();

/**
* Activates context in a scope
*
* @return active scope that will deactivate context when closed
*/
Scope activateInScope();
}
Original file line number Diff line number Diff line change
@@ -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<T extends ActivateableInScope<T>> extends Activateable<T> {

/**
* Activates context in a scope
*
* @return active scope that will deactivate context when closed
*/
Scope activateInScope();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import javax.annotation.Nullable;

public interface ElasticContext<T extends ElasticContext<T>> extends ReferenceCounted, Activateable<T> {
public interface ElasticContext<T extends ElasticContext<T>> extends ActivateableInScope<T>, ReferenceCounted {

/**
* @return the span/transaction that is associated to this context, {@literal null} if there is none
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ErrorCapture> {

TraceContext getTraceContext();

void end();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -122,4 +128,9 @@ public <T, C> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public Transaction<?> currentTransaction() {
return null;
}

@Nullable
@Override
public ErrorCapture getActiveError() {
return null;
}

@Nullable
@Override
public Transaction<?> startRootTransaction(@Nullable ClassLoader initiatingClassLoader) {
Expand All @@ -99,4 +105,9 @@ public <T, C> Transaction<?> startChildTransaction(@Nullable C headerCarrier, He
return null;
}

@Nullable
@Override
public ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public interface Tracer {

Set<String> getTraceHeaderNames();


ElasticContext<?> currentContext();

@Nullable
Expand All @@ -52,6 +51,9 @@ public interface Tracer {
@Nullable
Transaction<?> currentTransaction();

@Nullable
ErrorCapture getActiveError();

/**
* Starts a trace-root transaction
*
Expand All @@ -76,4 +78,6 @@ public interface Tracer {
@Nullable
<T, C> Transaction<?> startChildTransaction(@Nullable C headerCarrier, HeaderGetter<T, C> textHeadersGetter, @Nullable ClassLoader initiatingClassLoader);

@Nullable
ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader);
}

0 comments on commit e81f082

Please sign in to comment.