0Pricing
Vibe Coding · 课时

添加日志和指标

了解您的应用正在执行什么。

添加日志和指标 是 CoddyKit 上的免费 Vibe Coding 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Vibe Coding 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Vibe Coding 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

You Can't Fix What You Can't See

When a vibe-coded app misbehaves in production, you're blind unless you instrumented it. Logs, metrics, and traces are the three lenses that let you see inside a running system.

This lesson uses your AI assistant to add observability that answers real questions instead of producing noise.

Logs, Metrics, Traces

Each pillar answers a different question. Logs tell you what happened in one event. Metrics tell you how often and how much over time. Traces tell you how a single request moved across services.

Knowing which to reach for keeps your instrumentation focused and your bills sane.

Explain for my app which of logs, metrics, or traces best answers each question: 'why did this one checkout fail', 'is error rate rising', and 'which step makes this request slow'. Then recommend what to instrument first.

Make Logs Structured

A wall of free-text print statements is unsearchable. Structured logs emit JSON with consistent fields, so you can filter by user, route, or status in seconds.

Ask the assistant to replace ad-hoc logging with a structured logger and a standard field schema.

Replace all console prints with a structured JSON logger. Every log line should include timestamp, level, request_id, route, and duration_ms where relevant. Show the logger setup and convert three existing log statements as examples.

Correlate With Request IDs

A single user action can touch many log lines and several services. Without a shared identifier you can't tell which lines belong together.

Generate a request ID at the edge and thread it through every log and downstream call so one trace tells the whole story.

Add middleware that assigns a unique request_id to every incoming request, attaches it to all logs for that request, and forwards it as a header to downstream services. Show how I would later filter logs by a single request_id.

Choose Log Levels Wisely

Logging everything at one level is the same as logging nothing useful. Levels let you stay quiet in normal operation and verbose when investigating.

Have the assistant assign sensible levels and make verbosity configurable per environment without a redeploy.

Audit our log statements and assign appropriate levels: debug for development detail, info for business events, warn for recoverable issues, error for failures. Make the active level configurable via an environment variable so production can run at info and drop to debug temporarily.

Never Log Secrets

Logs are read by many people and often shipped to third-party tools. A password, token, or full credit-card number in a log line is a breach waiting to happen.

Ask the assistant to add redaction so sensitive fields are masked before anything is written.

Add automatic redaction to the logger so fields like password, token, authorization, and card_number are masked before output. Scan existing log calls for places that currently log full request bodies and fix them.

Emit the Golden Signals

For metrics, start with the four golden signals: latency, traffic, errors, and saturation. They cover most of what you need to know about service health.

Let the assistant instrument these as counters and histograms you can graph and alert on.

Instrument the four golden signals for the HTTP layer: request latency as a histogram, request count by route and status, error rate, and a saturation gauge like active connections. Expose them on a /metrics endpoint in a format my monitoring stack can scrape.

Track Business Metrics Too

System health is only half the picture. Signups, checkouts, and failed payments tell you whether the product is actually working for users.

Ask for custom metrics on the events that matter to the business so a silent revenue drop becomes visible immediately.

Add custom business metrics: a counter for completed checkouts, a counter for failed payments tagged by failure reason, and a gauge for active subscriptions. Make sure these are cheap to emit and won't slow the request path.

Add Distributed Tracing

When a request fans out across services and a database, a trace shows the timeline of every span so you can see exactly where the time went.

Have the assistant wire up a tracing standard so spans propagate automatically across service boundaries.

Add OpenTelemetry tracing to the API and worker. Auto-instrument HTTP and database calls, propagate trace context across the queue, and export spans to our collector. Show me what a single checkout request's trace would look like.

Build a Dashboard That Answers Questions

Raw metrics are useless until someone can read them at a glance. A good dashboard is organized around the questions you'll ask during an incident.

Ask the assistant to define panels for error rate, p95 latency, and the key business counters, with thresholds marked.

Design a dashboard layout for this service: panels for request rate, error rate, p95 and p99 latency, queue depth, and completed checkouts. For each panel, suggest a threshold line that would indicate trouble worth investigating.

Signal, Not Noise

The goal of observability is fast answers, not data hoarding. Too many logs and metrics cost money and bury the signal you need.

Periodically prune what you never query, keep field names consistent, and make sure every metric maps to a question someone actually asks.

Quick Check

Test your understanding of logging and metrics.

Recap

Observability rests on three pillars: structured, correlated, redacted logs; the golden-signal and business metrics that surface trends; and distributed traces that show a request's full path.

Prompt your assistant to instrument each deliberately, build dashboards around real questions, and prune anything that produces noise instead of answers.

常见问题解答

「添加日志和指标」课时是免费的吗?

是的 — 「添加日志和指标」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Vibe Coding 课程的其余内容,请升级到 CoddyKit PRO。 Vibe Coding 课程共包含 4 节课。

「添加日志和指标」这节课中我会学到什么?

了解您的应用正在执行什么。 你通过在浏览器中直接运行的动手代码来练习 Vibe Coding,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Vibe Coding 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Vibe Coding 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「添加日志和指标」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Vibe Coding 课中编写并运行代码吗?

能。每节 Vibe Coding 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 从原型到产品
  2. 通过提示词优化性能
  3. 添加日志和指标
  4. 响应事故
← 返回 Vibe Coding