Class ZipkinTracer

  • All Implemented Interfaces:
    AutoCloseable, org.apache.camel.CamelContextAware, org.apache.camel.Service, org.apache.camel.ShutdownableService, org.apache.camel.spi.RoutePolicyFactory, org.apache.camel.StatefulService, org.apache.camel.StaticService, org.apache.camel.SuspendableService

    @ManagedResource(description="ZipkinTracer")
    public class ZipkinTracer
    extends org.apache.camel.support.service.ServiceSupport
    implements org.apache.camel.spi.RoutePolicyFactory, org.apache.camel.StaticService, org.apache.camel.CamelContextAware
    To use Zipkin with Camel then setup this ZipkinTracer in your Camel application.

    Events (span) are captured for incoming and outgoing messages being sent to/from Camel. This means you need to configure which which Camel endpoints that maps to zipkin service names. The mapping can be configured using

    • route id - A Camel route id
    • endpoint url - A Camel endpoint url
    For both kinds you can use wildcards and regular expressions to match, which is using the rules from PatternHelper.matchPattern(String, String) and EndpointHelper.matchEndpoint(CamelContext, String, String)

    To match all Camel messages you can use * in the pattern and configure that to the same service name.
    If no mapping has been configured then Camel will fallback and use endpoint uri's as service names. However its recommended to configure service mappings so you can use human logic names instead of Camel endpoint uris in the names.

    Camel will auto-configure a span reporter one hasn't been explicitly configured, and if the hostname and port to a zipkin collector has been configured as environment variables

    • ZIPKIN_COLLECTOR_HTTP_SERVICE_HOST - The http hostname
    • ZIPKIN_COLLECTOR_HTTP_SERVICE_PORT - The port number
    or
    • ZIPKIN_COLLECTOR_THRIFT_SERVICE_HOST - The Scribe (Thrift RPC) hostname
    • ZIPKIN_COLLECTOR_THRIFT_SERVICE_PORT - The port number

    This class is implemented as both an EventNotifier and RoutePolicy that allows to trap when Camel starts/ends an Exchange being routed using the RoutePolicy and during the routing if the Exchange sends messages, then we track them using the EventNotifier.

    • Constructor Detail

      • ZipkinTracer

        public ZipkinTracer()
    • Method Detail

      • createRoutePolicy

        public org.apache.camel.spi.RoutePolicy createRoutePolicy​(org.apache.camel.CamelContext camelContext,
                                                                  String routeId,
                                                                  org.apache.camel.NamedNode route)
        Specified by:
        createRoutePolicy in interface org.apache.camel.spi.RoutePolicyFactory
      • init

        public void init​(org.apache.camel.CamelContext camelContext)
        Registers this ZipkinTracer on the CamelContext if not already registered.
      • getCamelContext

        public org.apache.camel.CamelContext getCamelContext()
        Specified by:
        getCamelContext in interface org.apache.camel.CamelContextAware
      • setCamelContext

        public void setCamelContext​(org.apache.camel.CamelContext camelContext)
        Specified by:
        setCamelContext in interface org.apache.camel.CamelContextAware
      • getEndpoint

        @ManagedAttribute(description="The POST URL for zipkin\'s v2 api.")
        public String getEndpoint()
      • setEndpoint

        public void setEndpoint​(String endpoint)
        Sets the POST URL for zipkin's v2 api, usually "http://zipkinhost:9411/api/v2/spans"
      • getHostName

        @ManagedAttribute(description="The hostname for the remote zipkin scribe collector.")
        public String getHostName()
      • setHostName

        public void setHostName​(String hostName)
        Sets the hostname for the remote zipkin scribe collector.
      • getPort

        @ManagedAttribute(description="The port number for the remote zipkin scribe collector.")
        public int getPort()
      • setPort

        public void setPort​(int port)
        Sets the port number for the remote zipkin scribe collector.
      • getRate

        @ManagedAttribute(description="Rates how many events should be traced by zipkin. The rate is expressed as a percentage (1.0f = 100%, 0.5f is 50%, 0.1f is 10%).")
        public float getRate()
      • setRate

        public void setRate​(float rate)
        Configures a rate that decides how many events should be traced by zipkin. The rate is expressed as a percentage (1.0f = 100%, 0.5f is 50%, 0.1f is 10%).
        Parameters:
        rate - minimum sample rate is 0.0001, or 0.01% of traces
      • setSpanReporter

        public void setSpanReporter​(zipkin2.reporter.Reporter<zipkin2.Span> spanReporter)
        Sets the reporter used to send timing data (spans) to the zipkin server.
      • getSpanReporter

        public zipkin2.reporter.Reporter<zipkin2.Span> getSpanReporter()
        Returns the reporter used to send timing data (spans) to the zipkin server.
      • getServiceName

        public String getServiceName()
      • setServiceName

        public void setServiceName​(String serviceName)
        To use a global service name that matches all Camel events
      • getClientServiceMappings

        public Map<String,​String> getClientServiceMappings()
      • setClientServiceMappings

        public void setClientServiceMappings​(Map<String,​String> clientServiceMappings)
      • addClientServiceMapping

        public void addClientServiceMapping​(String pattern,
                                            String serviceName)
        Adds a client service mapping that matches Camel events to the given zipkin service name. See more details at the class javadoc.
        Parameters:
        pattern - the pattern such as route id, endpoint url
        serviceName - the zipkin service name
      • getServerServiceMappings

        public Map<String,​String> getServerServiceMappings()
      • setServerServiceMappings

        public void setServerServiceMappings​(Map<String,​String> serverServiceMappings)
      • addServerServiceMapping

        public void addServerServiceMapping​(String pattern,
                                            String serviceName)
        Adds a server service mapping that matches Camel events to the given zipkin service name. See more details at the class javadoc.
        Parameters:
        pattern - the pattern such as route id, endpoint url
        serviceName - the zipkin service name
      • getExcludePatterns

        public Set<String> getExcludePatterns()
      • setExcludePatterns

        public void setExcludePatterns​(Set<String> excludePatterns)
      • addExcludePattern

        public void addExcludePattern​(String pattern)
        Adds an exclude pattern that will disable tracing with zipkin for Camel messages that matches the pattern.
        Parameters:
        pattern - the pattern such as route id, endpoint url
      • isIncludeMessageBody

        @ManagedAttribute(description="Whether to include the Camel message body in the zipkin traces")
        public boolean isIncludeMessageBody()
      • setIncludeMessageBody

        @ManagedAttribute(description="Whether to include the Camel message body in the zipkin traces")
        public void setIncludeMessageBody​(boolean includeMessageBody)
        Whether to include the Camel message body in the zipkin traces.

        This is not recommended for production usage, or when having big payloads. You can limit the size by configuring the max debug log size.

        By default message bodies that are stream based are not included. You can use the option setIncludeMessageBodyStreams(boolean) to turn that on.

      • isIncludeMessageBodyStreams

        @ManagedAttribute(description="Whether to include stream based Camel message bodies in the zipkin traces")
        public boolean isIncludeMessageBodyStreams()
      • setIncludeMessageBodyStreams

        @ManagedAttribute(description="Whether to include stream based Camel message bodies in the zipkin traces")
        public void setIncludeMessageBodyStreams​(boolean includeMessageBodyStreams)
        Whether to include message bodies that are stream based in the zipkin traces.

        This requires enabling stream caching on the routes or globally on the CamelContext.

        This is not recommended for production usage, or when having big payloads. You can limit the size by configuring the max debug log size.

      • setClientCustomTags

        public void setClientCustomTags​(Map<String,​String> clientCustomTags)
        Custom tags that should be included on all client requests, in addition to the tags specified on the Exchange via camel.client.customtags.
        Parameters:
        clientCustomTags - custom tags to be added for all client requests
        See Also:
        ZipkinClientRequestAdapter.onRequest(Exchange, SpanCustomizer)
      • getClientCustomTags

        public Map<String,​String> getClientCustomTags()
        Custom tags that are added for all client requests.
        Returns:
        custom tags added for all client requests
        See Also:
        ZipkinClientRequestAdapter.onRequest(Exchange, SpanCustomizer)
      • doInit

        protected void doInit()
                       throws Exception
        Overrides:
        doInit in class org.apache.camel.support.service.BaseService
        Throws:
        Exception
      • doShutdown

        protected void doShutdown()
                           throws Exception
        Overrides:
        doShutdown in class org.apache.camel.support.service.BaseService
        Throws:
        Exception
      • getProducerComponentSpanKind

        protected brave.Span.Kind getProducerComponentSpanKind​(org.apache.camel.Endpoint endpoint)
      • addProducerComponentSpanKind

        protected void addProducerComponentSpanKind​(String component,
                                                    brave.Span.Kind kind)
      • getConsumerComponentSpanKind

        protected brave.Span.Kind getConsumerComponentSpanKind​(org.apache.camel.Endpoint endpoint)