Record and analyze exceptions with open source tracing clients

更新时间:
复制 MD 格式

When your application throws exceptions, you need visibility into error types, stack traces, and affected traces to diagnose issues quickly. Managed Service for OpenTelemetry detects exceptions in your trace data and aggregates them on the Exceptions page in the console. If you use automatic instrumentation (OpenTelemetry agents or SkyWalking agents), exceptions are captured and reported without code changes. For manual instrumentation, this topic explains how to record exceptions on spans for each tracing protocol.

Prerequisites

Before you begin, make sure that you have:

  • An open source tracing client reporting trace data to Managed Service for OpenTelemetry. For setup instructions, see Connection description

How Managed Service for OpenTelemetry detects exceptions

The detection mechanism depends on the tracing protocol your client uses:

ProtocolWhere exceptions are storedDetection trigger
OpenTelemetryEvent attributes on a spanexception.type attribute present
OpenTracing (Jaeger, Zipkin, SkyWalking)Log fields on a spanevent field set to "error" and error.kind field present

Once detected, exceptions appear on the Exceptions page with full stack traces, if provided.

Record exceptions with OpenTelemetry

The OpenTelemetry semantic conventions for exceptions define how exception data attaches to spans. Managed Service for OpenTelemetry checks for the following event attributes:

AttributeRequiredDescription
exception.typeYesException class name. Triggers exception detection.
exception.messageNoException message text.
exception.stacktraceNoFull stack trace string. Displayed on the Exceptions page.

Use the SDK (recommended)

OpenTelemetry SDKs provide a recordException method that populates these attributes automatically.

Span span = myTracer.startSpan("spanName");
try {
    // Business logic
} catch (Throwable e) {
    span.recordException(e);
    throw e;
} finally {
    span.end();
}

recordException creates a span event with exception.type, exception.message, and exception.stacktrace populated from the caught exception.

Note

The examples in this topic use the OpenTelemetry SDK for Java. The recordException method is available in all OpenTelemetry SDK languages. Refer to the OpenTelemetry documentation for language-specific usage.

Use automatic instrumentation

If your application is instrumented with an OpenTelemetry agent or framework, the agent or framework captures and reports exceptions automatically. No code changes are needed. Managed Service for OpenTelemetry detects the exception data and displays it on the Exceptions page.

Record exceptions with OpenTracing

The OpenTracing semantic conventions store exception data in span log fields. Managed Service for OpenTelemetry checks for the following log fields:

Log fieldRequiredDescription
eventYesMust be set to "error". Triggers exception detection.
error.kindYesException class name.
messageNoException message text.
stackNoFull stack trace string. Displayed on the Exceptions page.

Jaeger and Zipkin: use the OpenTracing SDK

The following Java example records exceptions on spans with the OpenTracing SDK:

public static void test() {
    Tracer tracer = GlobalTracer.get();
    Span span = tracer.buildSpan("spanName").start();

    try (Scope scope = tracer.activateSpan(span)) {
        // Business logic
    } catch (Exception e) {
        onException(e, span);
    } finally {
        span.finish();
    }
}

public static void onException(Throwable throwable, Span span) {
    if (span != null) {
        Tags.ERROR.set(span, Boolean.TRUE);
        if (throwable != null) {
            span.log(errorLogs(throwable));
        }
    }
}

private static Map<String, Object> errorLogs(Throwable throwable) {
    Map<String, Object> errorLogs = new HashMap<>();
    errorLogs.put("event", Tags.ERROR.getKey());
    errorLogs.put("error.object", throwable);
    errorLogs.put("error.kind", throwable.getClass().getName());
    String message = throwable.getCause() != null
        ? throwable.getCause().getMessage()
        : throwable.getMessage();
    if (message != null) {
        errorLogs.put("message", message);
    }
    StringWriter sw = new StringWriter();
    throwable.printStackTrace(new PrintWriter(sw));
    errorLogs.put("stack", sw.toString());
    return errorLogs;
}

Key points in this example:

  • Tags.ERROR.set(span, Boolean.TRUE) marks the span as an error span.

  • The event field is set to the value of Tags.ERROR.getKey(), which resolves to "error".

  • The stack field captures the full stack trace through StringWriter and PrintWriter.

SkyWalking: automatic exception capture

SkyWalking agents use non-intrusive instrumentation to capture exceptions automatically. No code changes are needed. In most cases, the agent records exception data and reports it to Managed Service for OpenTelemetry, where it appears on the Exceptions page.

View and analyze exception data

After your application reports exceptions, Managed Service for OpenTelemetry aggregates the data on the Exceptions page. Open the Managed Service for OpenTelemetry console to review:

  • Exception types and occurrence counts

  • Stack traces for each exception

  • Affected spans and traces

Use this data to identify recurring errors, track exception trends, and prioritize fixes based on frequency and impact.

For detailed analysis capabilities, see Exception analysis.