Observability is the ability to measure a system's internal state by analyzing its external outputs (logs, metrics, and traces) to diagnose issues without modifying the code. For Go services, this means leveraging the language's concurrency model and standard libraries to emit structured data that reveals how goroutines, memory, and network calls interact under load. You implement this by configuring tracers and exporters to send telemetry data to a central backend for analysis.
// Configure a tracer to capture query performance in a Go service
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/tracelog"
)
// Create a tracer that logs query duration and errors
tracer := &tracelog.TraceLog{
Logger: log.New(os.Stdout, "", 0),
LogLevel: tracelog.LogLevelInfo,
}
// Connect with the tracer enabled
conn, err := pgx.ConnectConfig(ctx, &pgx.ConnConfig{
ConnConfig: pgx.ConnConfig{},
Tracer: tracer,
})