การติดตามด้วย Micrometer
เพิ่มการติดตามให้บริการด้วยรหัส trace และ span
การติดตามด้วย Micrometer เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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
คำถามที่พบบ่อย
บทเรียน “การติดตามด้วย Micrometer” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การติดตามด้วย Micrometer” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การติดตามด้วย Micrometer”
เพิ่มการติดตามให้บริการด้วยรหัส trace และ span คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การติดตามด้วย Micrometer” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุผลที่การติดตามแบบกระจายมีความสำคัญ
- การติดตามด้วย Micrometer
- การส่งออกการติดตามไปยัง Zipkin
- การเชื่อมโยงบันทึกกับการติดตาม