Logging records discrete, human-readable events that your code chooses to emit, such as "order 42 failed". Instrumentation measures the behaviour of a running program, producing metrics (counters, histograms), traces (the path of a request across functions and services) and profiles (where CPU and memory go). Logging answers "what happened", instrumentation answers "how is it behaving and where is the time going". Production Go services normally use both, tied together by a shared request or trace ID.
Today the standard approach is structured logging with log/slog, plus OpenTelemetry for metrics and traces, and pprof and runtime/trace for profiling.
| Aspect | Logging | Instrumentation |
|---|---|---|
| Question answered | What happened, and why did it fail? | How often, how slow, and where is time spent? |
| Data shape | Timestamped event records | Numeric time series, spans, profiles |
| Volume | Grows with events, can be large | Aggregated, small and predictable |
| Typical Go tools | log/slog, log | OpenTelemetry, Prometheus client, expvar, runtime/metrics |
| Debugging tools | Log search | pprof, runtime/trace, go test -bench |
| Cost | I/O and storage per line | CPU per measurement, low when aggregated |
Since Go 1.21 the standard library ships log/slog, which writes key-value records with levels and supports JSON output. The older log package prints unstructured lines and has no levels.
Use logger.With("service", "billing") to attach fields once, and slog.InfoContext(ctx, ...) so a custom handler can add the trace ID from the context. Never log secrets or full personal data.
Metrics are cheap numbers that you scrape or push to a monitoring system. The standard library offers expvar (simple JSON counters) and runtime/metrics (runtime internals such as GC and goroutine counts). For application metrics most teams use the OpenTelemetry metrics API or the Prometheus client.
Keep label values bounded. Using a user ID or full URL as a label creates millions of series and overwhelms the backend.
A trace follows one request through many functions and services, split into spans. OpenTelemetry is the vendor-neutral standard, and its Go API is stable for traces.
You also need to configure a tracer provider and an exporter (usually OTLP) once at start-up. Passing ctx through every call is what links the spans together, and it is also how you can attach the trace ID to log records so a log line leads straight to its trace.
Profiling shows where a program spends CPU time and memory, without adding measurement code to every function.
net/http/pprof exposes profiles over HTTP: import _ "net/http/pprof" and open /debug/pprof/. Protect that endpoint; do not expose it publicly.go tool pprof analyses CPU, heap, goroutine and mutex profiles.runtime/trace records scheduler, GC and goroutine events, viewed with go tool trace.go test -bench . -cpuprofile cpu.out profiles a benchmark.Avoid logging inside hot loops, avoid high-cardinality metric labels, and sample traces on busy services.
For small tools it is fine. For services, prefer log/slog because it has levels, structured fields and pluggable handlers.
No. Instrumentation is the code or runtime support that produces measurements. Monitoring is collecting, storing, visualising and alerting on them.
There is a small overhead per span. Sampling, for example recording a fraction of requests, keeps it low on busy systems.
Not efficiently. You can derive counts from logs, but metrics are cheaper to store and query, and are the better basis for alerts.
Logging records events with context, while instrumentation measures behaviour through metrics, traces and profiles. In Go, use log/slog for structured logs, OpenTelemetry or Prometheus for metrics and traces, and pprof with runtime/trace for performance work. Link the signals with a shared trace ID, keep cardinality low, and keep sensitive data out of logs.