All Products
Search
Document Center

Managed Service for OpenTelemetry:Obtain exception information by using open source tracing clients

Last Updated:May 11, 2024

This topic describes how to use an open source tracing client to trace and analyze exceptions in Managed Service for OpenTelemetry.

Prerequisites

An open source client is used to report data to Managed Service for OpenTelemetry. For more information, see Connection Description.

Set exception information for spans

OpenTelemetry exception specification

This specification applies to data reporting by using OpenTelemetry.

According to the OpenTelemetry exception specification, the exception information about a span is stored in the attributes of events. If the attributes of an event include exception.type, Managed Service for OpenTelemetry considers that an exception occurred on the span and displays the exception on the Exceptions page in the console. If exception stack traces are available, they can be added to the exception.stacktrace attribute. Then, you can view the exception stack traces on the Exceptions page.

If you use an OpenTelemetry SDK, you do not need to manually add the exception.type and exception.stacktrace attributes to an event. The SDK provides methods to record exceptions. The following code provides an example on how to record exceptions for a span by using OpenTelemetry SDK for Java:

Span span = myTracer.startSpan("spanName");
try {
  // Run the business code.
} catch (Throwable e) {
  span.recordException(e);
  throw e;
} finally {
  span.end();
}
Note

If your application is automatically instrumented by using OpenTelemetry, the agent or framework automatically captures the exception information and reports it to the server. Managed Service for OpenTelemetry identifies the exception information about spans and displays it on the Exceptions page.

OpenTracing exception specification

This specification applies to data reporting by using Jaeger, SkyWalking, or Zipkin.

According to the OpenTracing exception specification, the exception information about a span is stored in the log fields. If the fields of a log include event and error.kind and the value of the event field is "error", Managed Service for OpenTelemetry considers that an exception occurred on the span and displays the exception on the Exceptions page in the console. If exception stack traces are available, they can be added to the stack field. Then, you can view the exception stack traces on the Exceptions page.

  • Trace exceptions by using an OpenTracing SDK

    The following code provides an example on how to record exceptions for a span by using OpenTracing SDK for Java:

    public static void test() {
      Tracer tracer = GlobalTracer.get();
      Span span = tracer.buildSpan("spanName").start();
    
      try (Scope scope = tracer.activateSpan(span)) {
        // ... Run the business code.
      } 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;
    }
  • Trace exceptions by using SkyWalking

    SkyWalking provides a variety of agents for non-intrusive instrumentation. In most cases, an agent records exception information and reports it to the server. Managed Service for OpenTelemetry identifies the exception information about spans and displays it on the Exceptions page.

Analyze exceptions by using Managed Service for OpenTelemetry

After you set exception information for spans on the client side, Managed Service for OpenTelemetry generates statistics on the reported exception information. You can go to the Exceptions page in the Managed Service for OpenTelemetry console to view the exception information and statistics. For more information, see Exception analysis.