How to: enable distributed tracing
Enable distributed tracing for a service by adding the jEAP monitoring starter and configuring trace export and sampling, so request flows across services become visible as traces in a tracing backend and as trace/span ids in the service's logs.
Steps
-
Add the jEAP monitoring starter to the service:
<dependency><groupId>ch.admin.bit.jeap</groupId><artifactId>jeap-spring-boot-monitoring-starter</artifactId></dependency>With the starter on the classpath the service is instrumented end to end for tracing:
- Incoming HTTP requests open a server span, picking up an existing trace id from the
traceparent(orB3) header or generating a new one if there is none. - Outbound HTTP calls open a client span and propagate the trace context (
traceparent/B3) to the next service. - Incoming Kafka messages open a consumer span, adopting the trace context carried in the record
headers (
traceparent/B3), or starting a new trace if none is present. - Outgoing Kafka messages open a producer span and inject the trace context (
traceparent/B3) into the record headers so the consuming service continues the same trace. - Trace and span ids are added to the MDC for the duration of the active span scope (for an HTTP request or while a Kafka message is being processed) so every log line written in scope carries them.
- A Micrometer
Tracerbean is exposed for creating custom spans.
- Incoming HTTP requests open a server span, picking up an existing trace id from the
-
Declare the service's name, which is the name shown in the tracing backend:
spring:application:name: my-service -
Activate trace export by pointing Spring Boot at an OTLP HTTP endpoint:
management:opentelemetry:tracing:export:otlp:endpoint: http://localhost:4318/v1/tracesWithout this property, traces are still produced and visible as
traceId/spanIdin MDC, but no spans leave the service. The configured URL must point to an OpenTelemetry collector. -
Optionally, adjust the sampling rate. Spring Boot exposes a single probability property, decided at the root of a trace and propagated downstream so a trace is sampled in or out consistently across every service hop:
management:tracing:sampling:probability: 0.1 # default — keep 10% of tracesPick the probability so the resulting trace volume fits the tracing backend's capacity; production services typically keep the default or sample lower. For request flows that must always be investigable (errors, slow paths, business-critical operations), prefer always-on instrumentation in the service itself over raising the sample rate globally. The jEAP example services use
probability: 1.0so every demo call shows up in Grafana — appropriate only for demos and debugging, not for production.
Related
- Distributed Tracing — how tracing works and how to instrument code that bypasses Spring-managed HTTP/Kafka clients, including custom spans.
- jme-tracing-service — example demonstrating this integration together with custom spans.