การติดตามแบบกระจายเพื่อค้นหาจุดหน่วงเวลา
เรียนรู้การใช้การติดตามแบบกระจายเพื่อติดตามคำขอเดียวข้ามบริการต่าง ๆ ระบุจุดหน่วงเวลา และเชื่อมโยงร่องรอยกับบันทึกระหว่างแก้ไขข้อบกพร่องในระบบจริง
การติดตามแบบกระจายเพื่อค้นหาจุดหน่วงเวลา เป็นบทเรียน Production Debugging & Incident Response Playbook ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Production Debugging & Incident Response Playbook และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Production Debugging & Incident Response Playbook มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Distributed Tracing
In a microservice system a single user request can fan out across dozens of services. When it is slow, which service is to blame?
Distributed tracing answers this by attaching a shared trace_id to a request and recording a span for every operation it touches.
- A trace = the whole request journey
- A span = one timed unit of work
Anatomy of a Span
Each span carries timing and context so you can reconstruct the call tree.
trace_idlinks all spans of one requestspan_ididentifies the operationparent_idrecords who called it- start/end timestamps give duration
{
"trace_id": "abc123",
"span_id": "s2",
"parent_id": "s1",
"name": "db.query.users",
"start_ms": 1042,
"end_ms": 1310
}Context Propagation
For spans to join one trace, the trace_id must travel with the request. This is called context propagation.
Most systems inject standard headers like traceparent (W3C Trace Context) into outgoing HTTP calls and message metadata.
If propagation breaks, traces fragment and the call tree falls apart.
GET /orders HTTP/1.1
traceparent: 00-abc123-s1-01Instrumenting Code
You create spans around the operations you want to measure. Auto-instrumentation covers common libraries; manual spans capture your own logic.
The example below wraps a function in a span using OpenTelemetry conventions.
with tracer.start_as_current_span('charge_card') as span:
span.set_attribute('amount', 42)
result = payment.charge(42)
span.set_attribute('status', result.status)Reading the Waterfall
Tracing UIs show spans as a waterfall. The widest bar that is NOT just waiting on a child is usually your hotspot.
- Long bars with no children = local CPU/IO cost
- Long bars full of children = downstream cost
- Gaps between spans = queueing or untraced work
Sampling Strategies
Tracing every request is expensive. Sampling keeps volume manageable.
- Head sampling: decide at the start (e.g. keep 5%)
- Tail sampling: decide after the trace ends, keeping slow or errored traces
For debugging latency, tail sampling on high duration is invaluable.
Correlating Traces and Logs
A trace tells you where; logs tell you why. Stamp every log line with the active trace_id so you can jump from a slow span straight to its logs.
import logging
logging.info('cache miss', extra={'trace_id': current_trace_id()})Span Attributes and Events
Attributes are key/value tags on a span (db statement, HTTP status). Events are timestamped points inside a span (retry, lock acquired).
Rich attributes let you filter traces like 'all spans where db.rows > 10000', turning tracing into a query tool.
span.add_event('retry', {'attempt': 2})
span.set_attribute('db.rows', 12044)Finding the Critical Path
Total latency is not the sum of all spans. Parallel spans overlap. The critical path is the chain of spans that actually determines end-to-end time.
Optimizing a span NOT on the critical path will not make the request faster.
Tracing Async and Queues
Across queues, the consumer runs later than the producer. Propagate the context inside the message so the consumer span links back as a follows-from relationship instead of a parent-child one.
producer: msg.headers['traceparent'] = inject_context()
consumer: ctx = extract_context(msg.headers)A Debugging Workflow
Put it together when an endpoint is slow in production:
- Filter traces for that endpoint sorted by duration
- Open the slowest trace and read the waterfall
- Identify the dominant span on the critical path
- Jump to that span's logs via
trace_id - Fix, then re-check the latency distribution
Quick Check
Test your understanding of distributed tracing.
Recap
You learned how distributed tracing reconstructs a request across services using trace_id, spans, and context propagation.
- Read waterfalls to find hotspots
- Focus on the critical path, not total span time
- Use tail sampling to keep slow traces
- Correlate spans with logs for the full story
คำถามที่พบบ่อย
บทเรียน “การติดตามแบบกระจายเพื่อค้นหาจุดหน่วงเวลา” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การติดตามแบบกระจายเพื่อค้นหาจุดหน่วงเวลา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Production Debugging & Incident Response Playbook ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Production Debugging & Incident Response Playbook มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การติดตามแบบกระจายเพื่อค้นหาจุดหน่วงเวลา”
เรียนรู้การใช้การติดตามแบบกระจายเพื่อติดตามคำขอเดียวข้ามบริการต่าง ๆ ระบุจุดหน่วงเวลา และเชื่อมโยงร่องรอยกับบันทึกระหว่างแก้ไขข้อบกพร่องในระบบจริง คุณปฏิบัติ Production Debugging & Incident Response Playbook ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Production Debugging & Incident Response Playbook หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Production Debugging & Incident Response Playbook บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การติดตามแบบกระจายเพื่อค้นหาจุดหน่วงเวลา” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Production Debugging & Incident Response Playbook นี้ได้ไหม
ได้ บทเรียน Production Debugging & Incident Response Playbook ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การแก้ไขข้อบกพร่องของแอปพลิเคชันที่กำลังทำงานจากระยะไกล
- การแก้ไขข้อบกพร่องย้อนหลังด้วยดัมป์แกนหน่วยความจำ
- เทคนิคการทำโปรไฟล์หน่วยความจำและ CPU
- การติดตามแบบกระจายเพื่อค้นหาจุดหน่วงเวลา