Ajouter la journalisation et les métriques
Voyez ce que fait votre application.
Ajouter la journalisation et les métriques est une leçon Vibe Coding gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Vibe Coding, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Vibe Coding comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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.
Questions Fréquemment Posées
La leçon « Ajouter la journalisation et les métriques » est-elle gratuite ?
Oui — le texte complet de « Ajouter la journalisation et les métriques » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Vibe Coding, passe à CoddyKit PRO. Le cours Vibe Coding comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Ajouter la journalisation et les métriques » ?
Voyez ce que fait votre application. Tu pratiques Vibe Coding avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Vibe Coding ?
Aucune expérience préalable n'est requise. Vibe Coding sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.
Combien de temps prend la leçon « Ajouter la journalisation et les métriques » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Vibe Coding ?
Oui. Chaque leçon Vibe Coding inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Du prototype au produit
- Optimiser avec une consigne
- Ajouter la journalisation et les métriques
- Réagir aux incidents