Add Logging and Instrumentation
Purpose
Make the code's behavior observable in production so that operators can detect, diagnose and measure it, while keeping telemetry cheap, consistent and free of sensitive data.
When to use
- New code or a new integration is about to be released.
- An incident or support case could not be diagnosed from existing telemetry.
- An SLO or alert needs a signal the code does not emit yet.
When not to use
- Designing observability for a whole system or service landscape. Use
observability-plan.
- Defining alert rules and thresholds. Use
alert-design.
- Investigating existing logs. Use
log-analysis.
Inputs
Required:
- The code to instrument and its language/runtime.
Optional, improves quality:
- The team's telemetry stack and conventions (log format, field names, metric naming, OpenTelemetry usage), existing SLOs, known incident questions.
If the stack is unknown, use vendor-neutral OpenTelemetry concepts and semantic conventions and mark stack-specific details [TBD].
Process
- Write the operational questions first: Is it working? How fast? How often does it fail and why? Which input caused it? What is the backlog?
- Map each question to a signal: metric (rates, durations, sizes, saturation), trace/span (latency across hops), log (discrete event with context).
- Metrics: use RED for request paths (rate, errors, duration) and USE for resources; histograms for durations; labels with bounded cardinality only (never user id, order id, raw URL).
- Traces: create spans around external calls and significant internal steps; propagate context across HTTP/messaging; record errors on spans; add attributes following semantic conventions.
- Logs: structured key-value/JSON; one event per meaningful state change or failure; include correlation/trace id, operation, outcome, duration, stable error code.
- Levels: ERROR = needs action, WARN = degraded but handled, INFO = business-relevant lifecycle events, DEBUG = diagnostic off by default. No logging inside tight loops without sampling.
- Privacy and security: never log secrets, tokens, full card numbers or passwords; mask or hash personal data; log IDs not payloads.
- Keep instrumentation out of business logic where possible (middleware, decorators, interceptors).
- Define how to verify: a test or local run showing the log line, the metric increment and the span.
- Suggest dashboard panels and alert candidates linked to the signals, without inventing thresholds.
- If the goal continues, suggest
alert-design to turn the candidates into alerts or observability-plan for service-wide coverage.
Output format
# Instrumentation: <component>
## Operational Questions → Signals
| Question | Signal type | Name | Labels / attributes |
## Code Changes
<code with instrumentation>
## Log Events
| Event | Level | Fields | When |
## Privacy Notes
## Verification
## Dashboard / Alert Candidates
Quality checklist
Common pitfalls
- High-cardinality labels (user id, full path) that explode metric storage and cost.
- Logging whole request/response bodies "for debugging", leaking personal data.
- Counting only failures; without total attempts you cannot compute an error rate.
Example
Input: "Order import job: read file, validate rows, call inventory API per row."
Excerpt of output:
- Metric
order_import_rows_total{result="imported|invalid|failed"} and histogram order_import_duration_seconds.
- Span
inventory.reserve per call with http.response.status_code; job span links row spans.
- Log
import.completed INFO with file_id, rows_total, rows_invalid, duration_ms; invalid rows logged by row number and error code, never with customer name or address.
1---2name: logging-instrumentation3description: "Adds structured logs, metrics and distributed traces to code at the points that answer real operational questions, with consistent field names, correct levels, low-cardinality metric labels, trace context propagation and no secrets or personal data. Use when a feature is going to production, an incident showed missing visibility, or someone asks to add logging, metrics, tracing or telemetry (e.g. OpenTelemetry) to code."4license: MIT5metadata:6 source: https://skillmd.com/skills/muratdincer/logging-instrumentation7 version: "1.0.0"8 language: en9 category: 05-engineering10 role: developer11 area: coding12 title: "Add logging and instrumentation"13 related: "observability-plan, alert-design, slo-definition, error-handling-review, log-analysis"14 prompt: "Add logging, metrics and tracing to this order import job that reads a file, validates rows and calls the inventory API."15---1617# Add Logging and Instrumentation1819## Purpose20Make the code's behavior observable in production so that operators can detect, diagnose and measure it, while keeping telemetry cheap, consistent and free of sensitive data.2122## When to use23- New code or a new integration is about to be released.24- An incident or support case could not be diagnosed from existing telemetry.25- An SLO or alert needs a signal the code does not emit yet.2627## When not to use28- Designing observability for a whole system or service landscape. Use `observability-plan`.29- Defining alert rules and thresholds. Use `alert-design`.30- Investigating existing logs. Use `log-analysis`.3132## Inputs33Required:34- The code to instrument and its language/runtime.3536Optional, improves quality:37- The team's telemetry stack and conventions (log format, field names, metric naming, OpenTelemetry usage), existing SLOs, known incident questions.3839If the stack is unknown, use vendor-neutral OpenTelemetry concepts and semantic conventions and mark stack-specific details `[TBD]`.4041## Process421. Write the operational questions first: Is it working? How fast? How often does it fail and why? Which input caused it? What is the backlog?432. Map each question to a signal: metric (rates, durations, sizes, saturation), trace/span (latency across hops), log (discrete event with context).443. Metrics: use RED for request paths (rate, errors, duration) and USE for resources; histograms for durations; labels with bounded cardinality only (never user id, order id, raw URL).454. Traces: create spans around external calls and significant internal steps; propagate context across HTTP/messaging; record errors on spans; add attributes following semantic conventions.465. Logs: structured key-value/JSON; one event per meaningful state change or failure; include correlation/trace id, operation, outcome, duration, stable error code.476. Levels: ERROR = needs action, WARN = degraded but handled, INFO = business-relevant lifecycle events, DEBUG = diagnostic off by default. No logging inside tight loops without sampling.487. Privacy and security: never log secrets, tokens, full card numbers or passwords; mask or hash personal data; log IDs not payloads.498. Keep instrumentation out of business logic where possible (middleware, decorators, interceptors).509. Define how to verify: a test or local run showing the log line, the metric increment and the span.5110. Suggest dashboard panels and alert candidates linked to the signals, without inventing thresholds.5211. If the goal continues, suggest `alert-design` to turn the candidates into alerts or `observability-plan` for service-wide coverage.5354## Output format55```markdown56# Instrumentation: <component>57## Operational Questions → Signals58| Question | Signal type | Name | Labels / attributes |5960## Code Changes61<code with instrumentation>6263## Log Events64| Event | Level | Fields | When |6566## Privacy Notes67## Verification68## Dashboard / Alert Candidates69```7071## Quality checklist72- [ ] Every signal answers a stated operational question.73- [ ] Metric labels have bounded cardinality.74- [ ] Trace context is propagated across every outbound call and message.75- [ ] No secrets or unmasked personal data appear in logs, labels or span attributes.76- [ ] Log levels follow the stated rules and no event is logged twice.77- [ ] Names follow the team's convention or OpenTelemetry semantic conventions.78- [ ] Inferences are labeled `[ASSUMPTION]` and listed as assumptions or open questions; nothing unsupported is stated as fact.79- [ ] All checks pass; if any fails, revise the output and re-run this checklist before answering.8081## Common pitfalls82- High-cardinality labels (user id, full path) that explode metric storage and cost.83- Logging whole request/response bodies "for debugging", leaking personal data.84- Counting only failures; without total attempts you cannot compute an error rate.8586## Example87Input: "Order import job: read file, validate rows, call inventory API per row."8889Excerpt of output:90- Metric `order_import_rows_total{result="imported|invalid|failed"}` and histogram `order_import_duration_seconds`.91- Span `inventory.reserve` per call with `http.response.status_code`; job span links row spans.92- Log `import.completed` INFO with `file_id`, `rows_total`, `rows_invalid`, `duration_ms`; invalid rows logged by row number and error code, never with customer name or address.