Class MessagingSleuthOperators


  • public final class MessagingSleuthOperators
    extends Object
    Messaging helpers to manually parse and inject spans. We're treating message headers as a context that gets passed through. IMPORTANT: This API is experimental and might change in the future. The forInputMessage factory methods will retrieve the tracer context from the message headers and set up a a child span in the header under key Span.class name. If you need to continue it or tag it, it's enough to retrieve it from the headers. The first messaging span (the one that was first found in the input message) is present under the traceHandlerParentSpan header key.
    Since:
    3.0.0
    Author:
    Marcin Grzejszczak
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> org.springframework.messaging.Message<T> afterMessageHandled​(org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.messaging.Message<T> message, Throwable ex)
      Reports the span stored in the message.
      static <T> Function<org.springframework.messaging.Message<T>,​org.springframework.messaging.Message<T>> asFunction​(org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.messaging.Message<T> inputMessage)
      Function converting an input message to a message with tracer headers.
      static <T> org.springframework.messaging.Message<T> forInputMessage​(org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.messaging.Message<T> message)
      Processes the input message and returns a message with a header containing a span.
      static <T> org.springframework.messaging.Message<T> forInputMessage​(org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.messaging.Message<T> message, Consumer<org.springframework.messaging.Message<T>> withSpanInScope)
      Executes a span wrapped operation for an input message.
      static <T> org.springframework.messaging.Message<T> handleOutputMessage​(org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.messaging.Message<T> message)
      Creates an output message with tracer headers and reports the corresponding producer span.
      static <T> org.springframework.messaging.Message<T> handleOutputMessage​(org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.messaging.Message<T> message, Throwable throwable)
      Creates an output message with tracer headers and reports the corresponding producer span.
      static <T> org.springframework.messaging.Message<T> handleOutputMessage​(org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.messaging.Message<T> message, Consumer<Span> spanCustomizer, Throwable throwable)
      Creates an output message with tracer headers and reports the corresponding producer span.
      static <T> Span spanFromMessage​(org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.messaging.Message<T> message)
      Retrieves tracer information from message headers.
      static <T> void withSpanInScope​(org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.messaging.Message<T> message, Consumer<org.springframework.messaging.Message<T>> withSpanInScope)
      Retrieves tracer information from message headers and applies the operation.
      static <T> org.springframework.messaging.Message<T> withSpanInScope​(org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.messaging.Message<T> message, Function<org.springframework.messaging.Message<T>,​org.springframework.messaging.Message<T>> withSpanInScope)
      Retrieves tracer information from message headers and applies the operation.
    • Method Detail

      • forInputMessage

        public static <T> org.springframework.messaging.Message<T> forInputMessage​(org.springframework.beans.factory.BeanFactory beanFactory,
                                                                                   org.springframework.messaging.Message<T> message,
                                                                                   Consumer<org.springframework.messaging.Message<T>> withSpanInScope)
        Executes a span wrapped operation for an input message.
        Type Parameters:
        T - - type of payload
        Parameters:
        beanFactory - - bean factory
        message - - message to wrap
        withSpanInScope - - an operation that will be wrapped in a span and the span will be reported at the end
        Returns:
        message with tracer context
      • forInputMessage

        public static <T> org.springframework.messaging.Message<T> forInputMessage​(org.springframework.beans.factory.BeanFactory beanFactory,
                                                                                   org.springframework.messaging.Message<T> message)
        Processes the input message and returns a message with a header containing a span.
        Type Parameters:
        T - - payload type
        Parameters:
        beanFactory - - bean factory
        message - - input message to process
        Returns:
        message with tracer context
      • asFunction

        public static <T> Function<org.springframework.messaging.Message<T>,​org.springframework.messaging.Message<T>> asFunction​(org.springframework.beans.factory.BeanFactory beanFactory,
                                                                                                                                       org.springframework.messaging.Message<T> inputMessage)
        Function converting an input message to a message with tracer headers.
        Type Parameters:
        T - input message type
        Parameters:
        beanFactory - - bean factory
        inputMessage - - input message to process
        Returns:
        function representation of input message with tracer context
      • spanFromMessage

        public static <T> Span spanFromMessage​(org.springframework.beans.factory.BeanFactory beanFactory,
                                               org.springframework.messaging.Message<T> message)
        Retrieves tracer information from message headers.
        Type Parameters:
        T - - payload type
        Parameters:
        beanFactory - - bean factory
        message - - message to process
        Returns:
        span retrieved from message or null if there was no span
      • withSpanInScope

        public static <T> void withSpanInScope​(org.springframework.beans.factory.BeanFactory beanFactory,
                                               org.springframework.messaging.Message<T> message,
                                               Consumer<org.springframework.messaging.Message<T>> withSpanInScope)
        Retrieves tracer information from message headers and applies the operation.
        Type Parameters:
        T - - payload type
        Parameters:
        beanFactory - - bean factory
        message - - message to process
        withSpanInScope - - an operation that will be wrapped in a span but will not be reported
      • withSpanInScope

        public static <T> org.springframework.messaging.Message<T> withSpanInScope​(org.springframework.beans.factory.BeanFactory beanFactory,
                                                                                   org.springframework.messaging.Message<T> message,
                                                                                   Function<org.springframework.messaging.Message<T>,​org.springframework.messaging.Message<T>> withSpanInScope)
        Retrieves tracer information from message headers and applies the operation.
        Type Parameters:
        T - - payload type
        Parameters:
        beanFactory - - bean factory
        message - - message to process
        withSpanInScope - - an operation that will be wrapped in a span but will not be reported
        Returns:
        a message with tracer headers.
      • handleOutputMessage

        public static <T> org.springframework.messaging.Message<T> handleOutputMessage​(org.springframework.beans.factory.BeanFactory beanFactory,
                                                                                       org.springframework.messaging.Message<T> message)
        Creates an output message with tracer headers and reports the corresponding producer span. If the message contains a header called destination it will be used to tag the span with destination name.
        Type Parameters:
        T - - message payload
        Parameters:
        beanFactory - - bean factory
        message - - message to which tracer headers should be injected
        Returns:
        instrumented message
      • handleOutputMessage

        public static <T> org.springframework.messaging.Message<T> handleOutputMessage​(org.springframework.beans.factory.BeanFactory beanFactory,
                                                                                       org.springframework.messaging.Message<T> message,
                                                                                       Throwable throwable)
        Creates an output message with tracer headers and reports the corresponding producer span. If the message contains a header called destination it will be used to tag the span with destination name.
        Type Parameters:
        T - - message payload
        Parameters:
        beanFactory - - bean factory
        message - - message to which tracer headers should be injected
        throwable - - exception that took place while processing the message
        Returns:
        instrumented message
      • handleOutputMessage

        public static <T> org.springframework.messaging.Message<T> handleOutputMessage​(org.springframework.beans.factory.BeanFactory beanFactory,
                                                                                       org.springframework.messaging.Message<T> message,
                                                                                       Consumer<Span> spanCustomizer,
                                                                                       Throwable throwable)
        Creates an output message with tracer headers and reports the corresponding producer span. If the message contains a header called destination it will be used to tag the span with destination name.
        Type Parameters:
        T - - message payload
        Parameters:
        beanFactory - - bean factory
        message - - message to which tracer headers should be injected
        spanCustomizer - - customizer of the output span
        throwable - - exception that took place while processing the message
        Returns:
        instrumented message
      • afterMessageHandled

        public static <T> org.springframework.messaging.Message<T> afterMessageHandled​(org.springframework.beans.factory.BeanFactory beanFactory,
                                                                                       org.springframework.messaging.Message<T> message,
                                                                                       Throwable ex)
        Reports the span stored in the message.
        Type Parameters:
        T - - message payload
        Parameters:
        beanFactory - - bean factory
        message - - message with tracer context
        ex - - potential exception that took place while processing
        Returns:
        instrumented message