From 607528450b2e1175b311bffab0c061c71a6129d6 Mon Sep 17 00:00:00 2001 From: Augustin Date: Tue, 13 Dec 2022 17:37:47 +0100 Subject: [PATCH] airbyte-metric: implement `recordErrorOnRootSpan` in ApmUtils (#20286) --- .../io/airbyte/metrics/lib/ApmTraceUtils.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/airbyte-metrics/metrics-lib/src/main/java/io/airbyte/metrics/lib/ApmTraceUtils.java b/airbyte-metrics/metrics-lib/src/main/java/io/airbyte/metrics/lib/ApmTraceUtils.java index feaee0f20558..d38c29bbe04d 100644 --- a/airbyte-metrics/metrics-lib/src/main/java/io/airbyte/metrics/lib/ApmTraceUtils.java +++ b/airbyte-metrics/metrics-lib/src/main/java/io/airbyte/metrics/lib/ApmTraceUtils.java @@ -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; /** @@ -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}.