Go's standard library covers the core of telemetry without third-party code: log/slog for structured logs, expvar and runtime/metrics for metrics, net/http/pprof and runtime/trace for profiling and tracing, and net/http for health endpoints. For distributed tracing and metrics export to systems such as Prometheus, most teams add OpenTelemetry on top.
Telemetry means collecting logs, metrics and traces from a running program so you can detect failures, find bottlenecks and prove reliability. This guide shows what each standard package gives you and how to combine them.
log/slogexpvarruntime/metricslog/slogSince Go 1.21 the standard library ships log/slog, which replaces the plain log package for most services. It emits key-value records, supports levels, and can write JSON that log platforms parse directly.
Use logger.With("service", "checkout") to attach fields to every record, and slog.InfoContext(ctx, ...) so a custom handler can add a trace ID taken from the context. The old log package still works for small programs, but it only writes unstructured lines.
expvarThe expvar package publishes counters and other variables as JSON at /debug/vars. Importing it registers the handler on http.DefaultServeMux.
Opening http://localhost:8080/debug/vars returns http_requests_total together with memstats and the command line. expvar is simple and has no labels or histograms, so it suits small tools. Services that need rich metrics usually use the Prometheus or OpenTelemetry client libraries.
runtime/metricsThe runtime/metrics package reads stable, named metrics from the Go runtime, such as live heap bytes, goroutine counts and garbage collection pauses. It is more precise and cheaper than runtime.ReadMemStats.
Call metrics.All() to list every metric the running Go version supports. Poll the ones you care about on a ticker and export them to your metrics backend.
Two packages cover performance telemetry:
| Tool | Package | Answers |
|---|---|---|
| CPU and heap profiles | net/http/pprof | Where is time and memory going? |
| Execution trace | runtime/trace | Why is a goroutine waiting or the scheduler idle? |
Importing net/http/pprof for its side effect registers endpoints under /debug/pprof/. Serve them on a separate, private port, never on a public listener.
Then run go tool pprof http://127.0.0.1:6060/debug/pprof/profile?seconds=30 to capture a CPU profile, or curl -o trace.out http://127.0.0.1:6060/debug/pprof/trace?seconds=5 followed by go tool trace trace.out for an execution trace.
A health endpoint lets load balancers and orchestrators know whether to send traffic. Combine it with middleware that logs each request and records its duration.
The GET /healthz pattern uses method matching in http.ServeMux, available since Go 1.22. Keep liveness checks cheap, and use a separate readiness check that verifies dependencies such as the database.
The standard library has no distributed tracing API. The OpenTelemetry Go SDK fills that gap: it creates spans, propagates the trace context across services through HTTP headers, and exports traces and metrics over OTLP to a collector. Instrument your HTTP handlers and clients with the otelhttp wrapper, and add the trace ID to slog records so logs and traces link together.
A sensible layering is slog for logs, runtime/metrics plus OpenTelemetry for metrics, and OpenTelemetry for traces, with pprof available for ad hoc investigation.
log package?Use it for small scripts. For services, prefer log/slog because structured fields are searchable and levels can be changed without code changes.
expvar enough for production metrics?Only for simple counters. It has no labels, histograms or standard scrape format, so most teams use Prometheus or OpenTelemetry client libraries instead.
net/http/pprof safe to leave on?Only on a private interface. The endpoints expose internals and can be used to burn CPU, so never publish them to the internet.
Read the trace ID from the span in context.Context and add it to each slog record, either by hand or in a custom slog.Handler.
Use log/slog for structured logs, runtime/metrics and expvar for metrics, net/http/pprof and runtime/trace for profiling, and health endpoints with request middleware for operations. Add OpenTelemetry when you need distributed tracing and metric export. Keep debug endpoints private and log with consistent field names.