0Pricing
tRPC End-to-End Type Safe APIs · บทเรียน

มิดเดิลแวร์สำหรับบันทึกและจับเวลาประสิทธิภาพ

เพิ่มความสามารถในการสังเกตการณ์ให้ API ของ tRPC ด้วยมิดเดิลแวร์ที่บันทึกทุกการเรียกและวัดเวลาการทำงาน

มิดเดิลแวร์สำหรับบันทึกและจับเวลาประสิทธิภาพ เป็นบทเรียน tRPC End-to-End Type Safe APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน tRPC End-to-End Type Safe APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส tRPC End-to-End Type Safe APIs มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Observe Every Call?

You already use middleware for auth. Middleware is also perfect for cross-cutting concerns like logging and timing that should apply to many procedures.

Middleware Recap

A tRPC middleware wraps a procedure: it runs code, calls next(), then can run code after the result.

const mw = t.middleware(async (opts) => {
  // before
  const result = await opts.next();
  // after
  return result;
});

A Simple Logger

Log the procedure type and path on every call.

const logger = t.middleware(async ({ path, type, next }) => {
  console.log("-> " + type + " " + path);
  return next();
});

Measuring Duration

Capture the time before and after next() to compute how long the call took.

const timing = t.middleware(async ({ path, next }) => {
  const start = Date.now();
  const result = await next();
  const ms = Date.now() - start;
  console.log(path + " took " + ms + "ms");
  return result;
});

Inspecting the Result

The result tells you whether the call succeeded, useful for logging error rates.

const result = await next();
console.log(result.ok ? "ok" : "error");
return result;

Attaching to a Procedure

Use .use() to apply middleware when defining a base procedure.

const loggedProcedure = t.procedure.use(logger).use(timing);

Reusing Across Routers

Export the enhanced procedure and build all routers from it so logging applies everywhere automatically.

export const appRouter = router({
  ping: loggedProcedure.query(() => "pong"),
});

Adding Request IDs

Generate a unique id per call to correlate logs across services.

const withId = t.middleware(async ({ next, ctx }) => {
  const reqId = crypto.randomUUID();
  return next({ ctx: { ...ctx, reqId } });
});

Structured Logging

Emit JSON instead of plain text so logs are searchable in tools like Datadog or ELK.

console.log(JSON.stringify({ path, type, ms, level: "info" }));

Order of Middleware

Middleware runs in the order added. Put timing outermost so it includes the work of inner middleware.

Sampling Logs

In high-traffic APIs, logging every call is costly. Sample a percentage of requests so you keep visibility without overwhelming your log pipeline.

if (Math.random() < 0.1) console.log(path);

Quick Check

Test your middleware knowledge.

Recap

You added observability with middleware:

  • Middleware runs code before and after next()
  • Use it for logging, timing, and request IDs
  • Build a shared base procedure so it applies everywhere

Good observability makes a tRPC API far easier to debug in production.

คำถามที่พบบ่อย

บทเรียน “มิดเดิลแวร์สำหรับบันทึกและจับเวลาประสิทธิภาพ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “มิดเดิลแวร์สำหรับบันทึกและจับเวลาประสิทธิภาพ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส tRPC End-to-End Type Safe APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส tRPC End-to-End Type Safe APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “มิดเดิลแวร์สำหรับบันทึกและจับเวลาประสิทธิภาพ”

เพิ่มความสามารถในการสังเกตการณ์ให้ API ของ tRPC ด้วยมิดเดิลแวร์ที่บันทึกทุกการเรียกและวัดเวลาการทำงาน คุณปฏิบัติ tRPC End-to-End Type Safe APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน tRPC End-to-End Type Safe APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน tRPC End-to-End Type Safe APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “มิดเดิลแวร์สำหรับบันทึกและจับเวลาประสิทธิภาพ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน tRPC End-to-End Type Safe APIs นี้ได้ไหม

ได้ บทเรียน tRPC End-to-End Type Safe APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การสร้างบริบท tRPC
  2. มิดเดิลแวร์การตรวจสอบสิทธิ์
  3. สายโซ่มิดเดิลแวร์แบบกำหนดเอง
  4. มิดเดิลแวร์สำหรับบันทึกและจับเวลาประสิทธิภาพ
← กลับไปที่ tRPC End-to-End Type Safe APIs