Class OpenTelemetrySpanFactory

java.lang.Object
org.axonframework.extension.tracing.opentelemetry.OpenTelemetrySpanFactory
All Implemented Interfaces:
SpanFactory

public class OpenTelemetrySpanFactory extends Object implements SpanFactory
Creates Span implementations that are compatible with OpenTelemetry java agent instrumentation. OpenTelemetry is a standard to collect logging, tracing and metrics from applications. This SpanFactory focuses on supporting the tracing part of the standard.

To get started with OpenTelemetry, check out their documentation. Note that, even after configuring the correct dependencies, you still need to run the application using the OpenTelemetry java agent to export data. Without this, it will have the same effect as the NoOpSpanFactory since the data is not sent anywhere.

When creating a trace, the context of the current trace is used as a parent, instead of the one active at the time of starting the span (Default OpenTelemetry behavior). This is done using SpanBuilder.setParent(Context).

Since:
4.6.0
Author:
Mitchell Herrijgers
  • Constructor Details

  • Method Details

    • builder

      public static OpenTelemetrySpanFactory.Builder builder()
      Instantiate a Builder to create a OpenTelemetrySpanFactory.

      The SpanAttributeProvieders are defaulted to an empty list, and the Tracer is defaulted to the tracer defined by GlobalOpenTelemetry.

      Returns:
      a Builder able to create a OpenTelemetrySpanFactory.
    • propagateContext

      public <M extends Message> M propagateContext(M message)
      Description copied from interface: SpanFactory
      Propagates the currently active trace and span to the message. It should do so in a way that the context can be retrieved by the SpanFactory.createLinkedHandlerSpan(Supplier, Message, Message[]) method. The most efficient method currently known is to enhance the message's metadata.

      Since messages are immutable, the method returns the enhanced message. This enhanced message should be used during dispatch instead of the original message.

      Specified by:
      propagateContext in interface SpanFactory
      Type Parameters:
      M - The message's type.
      Parameters:
      message - The message to enhance.
      Returns:
      The enhanced message.
    • createRootTrace

      public Span createRootTrace(Supplier<String> operationNameSupplier)
      Description copied from interface: SpanFactory
      Creates a new Span without any parent trace. This should be used for logical start point of asynchronous calls that are not related to a message. For example snapshotting an aggregate.

      In monitoring systems, this Span will be the root of the trace.

      Specified by:
      createRootTrace in interface SpanFactory
      Parameters:
      operationNameSupplier - Supplier of the operation's name.
      Returns:
      The created Span.
    • createHandlerSpan

      public Span createHandlerSpan(Supplier<String> operationNameSupplier, Message parentMessage, boolean isChildTrace, Message... linkedParents)
      Description copied from interface: SpanFactory
      Creates a new Span linked to asynchronously handling a Message, for example when handling a command from Axon Server. The message attributes will be added to the span, based on the provided SpanAttributesProviders for additional debug information.

      In monitoring systems, this Span will be the root of the trace.

      The message's name will be concatenated with the operationName, see SpanUtils.determineMessageName(Message).

      Specified by:
      createHandlerSpan in interface SpanFactory
      Parameters:
      operationNameSupplier - Supplier of the operation's name.
      parentMessage - The message that is being handled.
      isChildTrace - Whether to force the span to be a part of the current trace. This means not linking, but setting a parent.
      linkedParents - Optional parameter, providing this will link the provided message(s) to the current, in addition to the original.
      Returns:
      The created Span.
    • createDispatchSpan

      public Span createDispatchSpan(Supplier<String> operationNameSupplier, Message parentMessage, Message... linkedSiblings)
      Description copied from interface: SpanFactory
      Creates a new Span linked to dispatching a Message, for example when sending a command to Axon Server. The message attributes will be added to the span, based on the provided SpanAttributesProviders for additional debug information.

      In monitoring systems, this Span will be part of another trace.

      Before asynchronously dispatching a message, add the tracing context to the message, using SpanFactory.propagateContext(Message) to the message's metadata.

      The message's name will be concatenated with the operationName, see SpanUtils.determineMessageName(Message).

      Specified by:
      createDispatchSpan in interface SpanFactory
      Parameters:
      operationNameSupplier - Supplier of the operation's name.
      parentMessage - The message that is being handled.
      linkedSiblings - Optional parameter, providing this will link the provided messages to the current.
      Returns:
      The created Span.
    • createInternalSpan

      public Span createInternalSpan(Supplier<String> operationNameSupplier)
      Description copied from interface: SpanFactory
      Creates a new Span linked to the currently active span. This is useful for tracing different parts of framework logic, so we can time what has the most impact.

      In monitoring systems, this Span will be part of another trace.

      Specified by:
      createInternalSpan in interface SpanFactory
      Parameters:
      operationNameSupplier - Supplier of the operation's name.
      Returns:
      The created Span.
    • createInternalSpan

      public Span createInternalSpan(Supplier<String> operationNameSupplier, Message message)
      Description copied from interface: SpanFactory
      Creates a new Span linked to the currently active span. This is useful for tracing different parts of framework logic, so we can time what has the most impact.

      The message supplied is used to provide a clearer name, based on SpanUtils.determineMessageName(Message), and to add the message's attributes to the span.

      In monitoring systems, this Span will be part of another trace.

      Specified by:
      createInternalSpan in interface SpanFactory
      Parameters:
      operationNameSupplier - Supplier of the operation's name.
      Returns:
      The created Span.
    • registerSpanAttributeProvider

      public void registerSpanAttributeProvider(SpanAttributesProvider provider)
      Description copied from interface: SpanFactory
      Registers an additional SpanAttributesProvider to the factory.
      Specified by:
      registerSpanAttributeProvider in interface SpanFactory
      Parameters:
      provider - The provider to add.