By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Production debugging asks three different questions. Logs answer 'what happened to this one request?' Metrics answer 'how much, how fast, how often?' Traces answer 'where did the time go across services?' You need all three because each one is bad at the others' job: metrics cannot explain a single request, logs cannot show a trend, and traces are sampled.
Go 1.21 added log/slog for structured, leveled logging. A structured log line is a message plus typed key-value pairs, so your log processor can filter request_id= and service= without regexes. Relay uses one logger per request, derived with slog.With, and puts the same request ID into logs, response headers and (later) trace attributes.
Generate a request ID in the outermost middleware, put it in context.Context, and add it to every log line and span in that request. When a customer reports a failure, the ID from the response header finds the exact log lines and the trace. This is also the cheapest distributed-tracing lesson: propagation is a convention, not a vendor.
package main
import (
"context"
"fmt"
"log/slog"
"os"
"sync/atomic"
)
type ctxKey string
const requestIDKey ctxKey = "request_id"
type metrics struct {
requests atomic.Int64
errors atomic.Int64
}
func (m *metrics) String() string {
return fmt.Sprintf("requests=%d errors=%d", m.requests.Load(), m.errors.Load())
}
func handle(ctx context.Context, m *metrics, service string, failed bool) {
m.requests.Add(1)
logger := slog.With("request_id", ctx.Value(requestIDKey), "service", service)
if failed {
m.errors.Add(1)
logger.Error("event rejected", "reason", "validation")
return
}
logger.Info("event accepted")
}
func main() {
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
ReplaceAttr: func(_ []string, attr slog.Attr) slog.Attr {
if attr.Key == slog.TimeKey {
return slog.Attr{}
}
return attr
},
})
slog.SetDefault(slog.New(handler))
m := &metrics{}
ctx := context.WithValue(context.Background(), requestIDKey, "req-1")
handle(ctx, m, "api", false)
handle(ctx, m, "api", true)
fmt.Println("metrics:", m)
}| Signal | Question | Relay artifact |
|---|---|---|
| Logs | What happened to this request? | slog lines with request_id, service, status |
| Metrics | How much and how fast? | Counters for requests/errors, histogram for latency |
| Traces | Where did the time go? | A span per middleware, handler and query |
| Correlation | Which lines belong together? | One request ID in context, logs and headers |
| Alerts | Is it bad right now? | Metric thresholds and error-rate windows |
| Dashboards | What changed? | Latency percentiles, queue depth, error budget |
Key takeaways
Logs explain one request, metrics show trends, and traces show cross-service time.
Use log/slog and typed key-value attributes instead of formatted strings.
Derive a request-scoped logger with the request ID in context, and reuse it through the call chain.
Keep counters and histograms next to the code that increments them.
Redact secrets and personal data before they reach a log line.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.