Micrometer Tracing
Strumentalizzi i servizi con gli ID di trace e span.
Micrometer Tracing è una lezione Spring Boot 4 Microservices & REST APIs gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Spring Boot 4 Microservices & REST APIs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Micrometer Tracing overview
Micrometer Tracing is a vendor-neutral tracing facade. You code against its API, and a bridge (Brave or OpenTelemetry) does the real work and propagation.
Adding the dependencies
Add Micrometer Tracing plus a bridge. The Brave bridge is a common choice.
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>Auto-configuration kicks in
With the bridge on the classpath, Spring Boot auto-configures a Tracer and instruments incoming requests and outbound HTTP clients. Many spans appear with zero code.
The Tracer bean
The Tracer is the entry point for working with spans manually. It is auto-configured and injectable.
@Service
public class OrderService {
private final Tracer tracer;
public OrderService(Tracer tracer) { this.tracer = tracer; }
}Creating a custom span
Wrap a meaningful unit of work in its own span to time it and see it in the trace.
Span span = tracer.nextSpan().name("calculate-price");
try (Tracer.SpanInScope ws = tracer.withSpan(span.start())) {
calculatePrice();
} finally {
span.end();
}@NewSpan and @SpanTag
For convenience, annotate a method with @NewSpan to wrap it in a span automatically, and tag arguments with @SpanTag.
@NewSpan("load-customer")
public Customer load(@SpanTag("customerId") Long id) {
return repo.findById(id);
}Adding tags and events
Enrich spans with tags (key/value metadata) and events (timestamped notes) to make traces more diagnostic.
span.tag("user.id", userId);
span.event("cache-miss");Reading the current trace IDs
You can read the active trace and span IDs - useful for returning a trace id to clients or correlating.
Span current = tracer.currentSpan();
if (current != null) {
String traceId = current.context().traceId();
String spanId = current.context().spanId();
}Brave vs OpenTelemetry bridge
Two bridges exist: Brave (Zipkin-native) and OpenTelemetry (OTel ecosystem). Pick one - they should not be on the classpath together.
Propagation format
The bridge controls how context is propagated. You can choose formats like W3C (traceparent) or B3, which matters when integrating with other systems.
management:
tracing:
propagation:
type: w3cBaggage
Baggage is extra key/value data propagated alongside the trace context across services - e.g. a tenant id - so every downstream span and log can access it.
Quick Check
Check your Micrometer Tracing knowledge.
Recap
You used Micrometer Tracing:
- It is a facade; a Brave or OTel bridge does the real work
- Inject the
Tracerto create custom spans, or use@NewSpan - Add tags/events; read traceId/spanId
- Baggage propagates extra context
Domande Frequenti
La lezione «Micrometer Tracing» è gratuita?
Sì — il testo completo di «Micrometer Tracing» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Spring Boot 4 Microservices & REST APIs, passa a CoddyKit PRO. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Cosa imparerò in «Micrometer Tracing»?
Strumentalizzi i servizi con gli ID di trace e span. Eserciti Spring Boot 4 Microservices & REST APIs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Spring Boot 4 Microservices & REST APIs?
Non è richiesta alcuna esperienza precedente. Spring Boot 4 Microservices & REST APIs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Micrometer Tracing»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Spring Boot 4 Microservices & REST APIs?
Sì. Ogni lezione Spring Boot 4 Microservices & REST APIs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Perché il tracing distribuito è importante
- Micrometer Tracing
- Esportare le trace in Zipkin
- Correlare log e trace