ตามหาว่าเวลาหายไปไหน:Distributed tracing

checkout ใช้ 2.5 วินาที ผ่านห้า service แล้ว log ของแต่ละตัวก็อยู่คนละที่ มาตาม trace id ตัวเดียวไปทุก hop จนเห็นว่าเวลาหายไปไหน แล้วแก้ให้เร็วขึ้นแบบมีตัวเลขยืนยัน

ระดับ: ลึกดู 13 ขั้น · ~1 นาทีเปิดดู span ไหนก็ได้จำ 3 คำถาม · 8 การ์ดเทียบ 4 วิธีเก็บ traceTypeScript · YAML

สิ่งที่จะได้

  • อธิบายได้ว่า trace id ใน traceparent ร้อยทุก hop ของ request เดียวไว้ด้วยกันยังไง
  • อ่าน waterfall ออก แท่งที่ซ้อนกันรันพร้อมกัน แท่งที่ต่อกันคือรอกัน แท่งแดงคือพัง
  • เลือก tail sampling แทน head sampling ได้ เมื่อต้องเก็บทุก trace ที่ช้าหรือพัง
  • พิสูจน์ได้ว่าแก้แล้วเร็วขึ้นจริง ด้วย trace ใหม่กับ p95 ก่อนและหลัง ไม่ใช่ความรู้สึก

สรุป 1 นาที

กดข้อไหน ภาพข้างล่างจะเล่นขั้นนั้นให้ดู

4เทียบทุกแบบ
เช็กเอาต์ครั้งเดียว 2.5 วินาทีGET /checkout
GET /checkout → …
ขั้น 1 / 13 · กดทีละขั้น
0.00 sทั้ง request
0span
0span ที่ error
?ตัวที่กินเวลาสุด
?ของทั้ง request

ในภาพ เช็กเอาต์ครั้งแรกยังไม่มี tracing มันเดินผ่าน service ต่าง ๆ รอบเดียว และมีสาม service เขียน log ของตัวเอง ครั้งถัดไปเปิด tracing แล้ว เลยเล่นทีละการเรียก แต่ละการเรียกเดินไปตามถนนพร้อม trace id และทิ้งเส้นด้ายสีทองไว้ข้างหลัง service ที่ span ยังเปิดอยู่มีแสงค้าง เป็นสีแดงครู่หนึ่งตรงที่ล้ม waterfall (แถวละ span ย่อหน้าอยู่ใต้ span แม่ เวลาเดินไปทางขวา แท่งแดงคือ span ที่ error) ตั้งอยู่ในเมืองเป็นจอ เวลาเป็นตัวเลขสมมติ แต่ waterfall หน้าตาแบบนี้คือที่เครื่องมือ tracing วาดจริง

ขั้นที่ 4 · ปล่อยทุกแบบวิ่งพร้อมกัน

เทียบทุกแบบ: Distributed tracing

ร้อย request เดียวกันทุกช่อง สามตัวช้า (2.5 วินาที) สองตัว error ที่เหลือปกติ แต่ละช่องเก็บ trace ต่างกัน ดูว่าตัวที่เราอยากเห็นที่สุดติดมาไหม และต้องเก็บกี่ตัว

request ที่ 0
ไม่มี tracinglog ของแต่ละ serviceเก็บ 0 · ตัวช้า 0/0 · error 0/0

ใช้เมื่อ แอปเดียว service เดียว: log กับ metric ก็พอ

ไม่ควรใช้เมื่อ หลาย service: รู้ว่าช้าแต่ไม่รู้ว่าช้าที่ไหน

แบบจำลอง: head sampling 10% เลือกจาก trace id ตั้งแต่เริ่ม เลยเก็บตัวไหนก็ได้สิบตัว ในรอบนี้ไม่โดนตัวช้าหรือ error เลย (ความน่าจะเป็นแต่ละตัวคือ 10%) tail sampling ตัดสินใจหลัง trace จบ · กดช่องไหนก็ได้ ภาพใหญ่ด้านบนจะเปลี่ยนเป็นแบบนั้น พร้อมบอกว่าเหมาะกับงานแบบไหน

แต่ละอย่างตอบอะไร

ตอบว่าตัวอย่าง
Metricsมีอะไรผิดปกติไหมp95 ของ /checkout จาก 400 ms ขึ้นไป 2.5 วินาที
Tracesช้าตรงไหนใน requestSELECT ใน orders กินไป 68%
Logsตรงนั้นเกิดอะไรขึ้นquery, parameter, ข้อความ error

ให้ SDK ส่งต่อให้

ในโค้ดจริงไม่ต้องเขียน header เอง SDK กับ auto-instrumentation ของ OpenTelemetry สร้าง span ให้ HTTP ขาเข้าขาออกกับ database แล้วส่ง traceparent ต่อให้ ใส่ trace id ลงใน log ด้วย จะกระโดดจาก span ที่ช้าไปดู log ของมันได้เลย

เก็บตัวที่น่าสนใจ

เก็บทุก trace แพง head sampling ถูกแต่ตัดสินใจก่อนจะรู้ว่า request ไหนจะช้า tail sampling รอจนจบแล้วเก็บตัว error กับตัวช้าไว้ แลกกับต้องถือ span ไว้สักพัก

ในโค้ดจริง

traceparent.ts
// Three small services on 127.0.0.1 passing a W3C traceparent header along, the way OpenTelemetry's propagator does:
// gateway → checkout → db. Each one reads the trace id, makes a span of its own and sends its span id downstream.
import assert from "node:assert/strict";
import { randomBytes } from "node:crypto";
import { createServer, request, type IncomingMessage, type Server } from "node:http";
import type { AddressInfo } from "node:net";

interface Span { service: string; trace: string; id: string; parent: string | null; ms: number }
const spans: Span[] = []; // what an exporter would ship off; here we keep them to look at
const FORMAT = /^00-([0-9a-f]{32})-([0-9a-f]{16})-0[01]$/; // version-traceid-parentid-flags

function call(port: number, traceparent: string): Promise<string> {
  return new Promise((done, fail) => {
    request({ host: "127.0.0.1", port, headers: { traceparent } }, (res) => { let b = ""; res.on("data", (c) => (b += c)); res.on("end", () => done(b)); }).on("error", fail).end();
  });
}

/** One service: continue the trace it was sent (or start one), do its work, maybe call the next. */
function service(name: string, work: (traceparent: string) => Promise<void>): Server {
  return createServer(async (req: IncomingMessage, res) => {
    const start = performance.now();
    const m = FORMAT.exec(String(req.headers.traceparent ?? ""));
    const trace = m ? m[1] : randomBytes(16).toString("hex"), id = randomBytes(8).toString("hex");
    await work(`00-${trace}-${id}-01`); // downstream calls carry OUR span id as their parent
    spans.push({ service: name, trace, id, parent: m ? m[2] : null, ms: performance.now() - start });
    res.end("ok");
  });
}
const listen = (s: Server) => new Promise<number>((r) => s.listen(0, "127.0.0.1", () => r((s.address() as AddressInfo).port)));

const db = service("db.query", () => new Promise((r) => setTimeout(r, 300))); // the slow query
const dbPort = await listen(db);
const checkout = service("checkout", async (tp) => { await call(dbPort, tp); });
const checkoutPort = await listen(checkout);
const gateway = service("gateway", async (tp) => { await call(checkoutPort, tp); });
const gatewayPort = await listen(gateway);

await call(gatewayPort, ""); // a request from outside: no traceparent yet, the gateway starts the trace
for (const s of [db, checkout, gateway]) s.close();

const [q, c, g] = spans; // spans finish inside-out: db, then checkout, then gateway
assert.equal(new Set(spans.map((s) => s.trace)).size, 1); // one trace id on every hop
assert.deepEqual([g.parent, c.parent, q.parent], [null, g.id, c.id]); // each span's parent is the caller's span
// Where the time went: the slow query is most of the request (the two hops on 127.0.0.1 take a few ms, more on a busy
// machine, so the share is held loosely: over half, never an exact figure that a slow run would miss).
assert.ok(q.ms >= 290 && q.ms / g.ms > 0.5);
console.log(`${spans.length} spans, one trace id · gateway → checkout → db.query, each the parent of the next · db.query is most of the request`);
รัน: node traceparent.ts (สาม service บน 127.0.0.1 ส่ง header ต่อกันจริง ไม่ใช้ package ของ OpenTelemetry)ผลที่พิมพ์ออกมา: 3 spans, one trace id · gateway → checkout → db.query, each the parent of the next · db.query is most of the request

สาม service เล็ก ๆ บน 127.0.0.1 (gateway → checkout → db) ส่ง header traceparent ตามมาตรฐาน W3C ต่อกันแบบที่ propagator ของ OpenTelemetry ทำ: อ่าน trace id สร้าง span ของตัวเอง แล้วส่ง span id ของตัวเองต่อไป เขียนเองด้วย node:http จะได้เห็นว่า SDK ทำอะไรให้ รันจริง

คำถามที่เจอบ่อย

trace กับ span ต่างกันยังไง

trace คือเส้นทางทั้งหมดของ request หนึ่งอัน span คืองานชิ้นหนึ่งในนั้น เช่นรับ request เรียก service อื่น หรือ query มีชื่อ เวลาเริ่ม เวลาจบ และ span แม่ ทุก span ใน trace เดียวกันมี trace id เดียวกัน

header traceparent หน้าตาเป็นยังไง

00-<trace id 32 ตัว hex>-<span id ของคนเรียก 16 ตัว hex>-<flags> ท้ายเป็น -01 แปลว่าเลือกเก็บ (sampled) เป็นมาตรฐาน W3C Trace Context ที่ OpenTelemetry ใช้ ทุก hop ใส่ span id ของตัวเองแทนก่อนส่งต่อ

ต้องเขียนส่ง header ต่อเองไหม

ไม่ต้อง SDK กับ auto-instrumentation ของ OpenTelemetry สร้าง span ให้ HTTP ขาเข้าขาออกกับ database call แล้วส่ง traceparent ต่อให้เอง ที่ควรทำเพิ่มคือใส่ trace id ลงใน log จะได้กระโดดจาก span ที่ช้าไปดู log ของมันได้เลย

logs, metrics กับ traces ใช้ต่างกันยังไง

metrics บอกว่ามีอะไรผิดปกติ (p95 พุ่ง error เยอะขึ้น) traces บอกว่าช้าหรือพังตรงไหนใน request แล้ว logs บอกว่าตรงนั้นเกิดอะไรขึ้น ไล่ตามลำดับนี้แหละ

ตาราง: แต่ละอย่างตอบอะไร

head sampling กับ tail sampling ต่างกันยังไง

head ตัดสินใจตอน request เริ่ม ถูกและง่าย แต่ตอนนั้นยังไม่มีใครรู้ว่ามันจะช้าหรือพัง เก็บ 10% ก็ได้ตัว error แค่ราว 10% tail รอ trace จบแล้วเก็บ error กับตัวช้าทุกตัว แลกกับต้องมีตัวกลางอย่าง OpenTelemetry Collector ถือ span ไว้ก่อน

แท่งใน waterfall ซ้อนกัน แปลว่าอะไร

แปลว่าสองงานนั้นทำพร้อมกัน ในภาพ inventory กับ payments เริ่มพร้อมกัน เวลาที่เสียจริงเลยเท่ากับตัวที่นานกว่า แท่งที่ต่อกันเป็นทอด ๆ คือรอกัน ส่วนแท่งแดงคือ span ที่พัง (payments ได้ 503 รอบแรกแล้วลองใหม่)

แก้แล้วจะรู้ได้ยังไงว่าเร็วขึ้นจริง

ดู trace ใหม่ ในภาพเพิ่ม index ที่ customer_id แล้ว query เหลือ 40 ms ทั้ง request จาก 2.5 วินาทีเหลือ 0.84 แล้วเทียบ p95 ใน metrics ก่อนกับหลังด้วย จะได้รู้ว่าเร็วขึ้นกับทุกคน ไม่ได้ฟลุกอยู่ request เดียว

บทต่อไป

ขยายฐานข้อมูลระดับ: ลึก

trace ชี้ว่า SELECT ที่ไม่มี index กินเวลาไปเกือบหมด บทต่อไปคือลำดับการแก้ database ที่ช้า เริ่มจาก index