Skip to content

Commit

Permalink
airbyte-metric: implement recordErrorOnRootSpan in ApmUtils (#20286)
Browse files Browse the repository at this point in the history
  • Loading branch information
alafanechere authored Dec 13, 2022
1 parent 4e9b014 commit 6075284
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

package io.airbyte.metrics.lib;

import datadog.trace.api.DDTags;
import datadog.trace.api.interceptor.MutableSpan;
import io.opentracing.Span;
import io.opentracing.log.Fields;
import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;

/**
Expand Down Expand Up @@ -83,6 +87,26 @@ public static void addExceptionToTrace(final Span span, final Throwable t) {
}
}

/**
* Adds an exception to the root span, if an active one exists.
*
* @param t The {@link Throwable} to be added to the provided span.
*/
public static void recordErrorOnRootSpan(final Throwable t) {
final Span activeSpan = GlobalTracer.get().activeSpan();
activeSpan.setTag(Tags.ERROR, true);
activeSpan.log(Map.of(Fields.ERROR_OBJECT, t));
if (activeSpan instanceof MutableSpan) {
final MutableSpan localRootSpan = ((MutableSpan) activeSpan).getLocalRootSpan();
localRootSpan.setError(true);
localRootSpan.setTag(DDTags.ERROR_MSG, t.getMessage());
localRootSpan.setTag(DDTags.ERROR_TYPE, t.getClass().getName());
final StringWriter errorString = new StringWriter();
t.printStackTrace(new PrintWriter(errorString));
localRootSpan.setTag(DDTags.ERROR_STACK, errorString.toString());
}
}

/**
* Formats the tag key using {@link #TAG_FORMAT} provided by this utility, using the default tag
* prefix {@link #TAG_PREFIX}.
Expand Down

0 comments on commit 6075284

Please sign in to comment.