Operations Observability

35 articles
How to Add Context to Log Messages in Go Add context to Go log messages using context.WithValue or structured logging with log/slog for key-value pairs. How to Configure Log Rotation in Go Configure log rotation for Go applications using external tools like logrotate or internal libraries like lumberjack to manage file size and history. How to Correlate Logs with Request IDs in Go Generate a unique UUID per request, inject it into the context, and log it in handlers to trace requests across your Go application. How to Export Custom Metrics from Go Applications Export custom Go metrics by defining metric names in a Sample slice and reading values via runtime/metrics.Read. How to Implement Custom Middleware for Observability in Go Implement Go observability middleware by wrapping your HTTP handler to log request methods, paths, and execution duration. How to Implement Distributed Tracing in Go with OpenTelemetry Implement distributed tracing in Go by setting up an OpenTelemetry tracer provider and starting spans to track request flow. How to Implement Health Checks for Microservices in Go Add an HTTP endpoint that pings your database and returns 200 OK if healthy or 503 if a dependency fails. How to Implement Log Levels in Go Implement log levels in Go using the standard library's slog package by configuring a Handler with a specific Level threshold. How to Implement Request Logging Middleware in Go Create a Go middleware function that wraps an http.Handler to log request method and path before executing the next handler in the chain. How to Instrument Go Code with Prometheus Metrics Add the Prometheus client library, define and register metrics, and expose the /metrics endpoint to enable monitoring. How to Integrate Go Logging with ELK, Datadog, or Grafana Loki Use third-party libraries like zap to output JSON logs from Go, then configure an agent to forward them to ELK, Datadog, or Loki. How to Integrate Go with Grafana Dashboards Integrate Go with Grafana by enabling the built-in pprof HTTP handler and configuring Grafana to scrape the resulting metrics endpoint. How to Log to a File in Go Log to a file in Go by opening a file with os.OpenFile and creating a new logger instance pointing to it. How to Monitor Goroutine and Memory Usage in Production Import net/http/pprof to expose live goroutine and memory metrics via a local HTTP endpoint. How to Output JSON Logs in Go for Log Aggregation Output JSON logs in Go by creating a logger with slog.NewJSONHandler and writing to os.Stdout. How to Propagate Trace Context Across Go Services Use OpenTelemetry libraries to automatically inject and extract trace context headers for distributed tracing across Go services. How to Set Up Structured Logging with Trace IDs in Go Implement structured logging with trace IDs in Go by using log/slog with a custom handler to inject context values into JSON log records. How to Use Distributed Tracing in Go (OpenTelemetry, Jaeger) Implement distributed tracing in Go by initializing the OpenTelemetry SDK with an OTLP exporter configured to send spans to a Jaeger collector. How to Use Jaeger for Tracing in Go Instrument your Go application with the OpenTelemetry SDK to send distributed traces to a Jaeger collector for performance monitoring. How to Use logrus for Structured Logging in Go Initialize a logrus logger with a JSON formatter and use WithFields to add structured key-value data to your log entries. How to Use OpenTelemetry for Metrics and Traces in Go Initialize OpenTelemetry Tracer and Meter providers in Go, then wrap HTTP handlers and database connections to automatically export traces and metrics. How to Use OpenTelemetry in Go: A Complete Guide Instrument Go applications with OpenTelemetry by initializing a tracer provider and starting spans to track request flow and performance. How to Use pprof for Production Profiling in Go Use runtime/pprof to capture profiles and go tool pprof to analyze performance bottlenecks in Go applications. How to Use Prometheus Client Library in Go Install the Prometheus Go client, define metrics, register them, and serve them at /metrics to enable monitoring. How to Use slog (Structured Logging) in Go 1.21+ Use the log/slog package to create a logger and output structured JSON logs with key-value pairs for better machine readability. How to Use the expvar Package for Exposing Metrics Use the expvar package to define variables and automatically expose them as JSON at /debug/vars for monitoring. How to Use the expvar Package in Go Use the expvar package to expose Go variables as JSON at /debug/vars for easy server monitoring. How to Use the log Package in Go The `log` package provides a simple, thread-safe interface for logging messages with timestamps, prefixes, and optional flags. How to Use the slog Package for Structured Logging Use the slog package to create structured, machine-readable logs in Go by initializing a handler and calling logger methods with key-value pairs. How to Use zap for Structured Logging in Go Use Zap by configuring a logger with `zap.NewProductionConfig()` or `zap.NewDevelopmentConfig()`, then call `Sugar()` to get a simpler API or stick with the core `Logger` for maximum performance. How to Use zerolog for High-Performance Logging in Go Connect pgx to zerolog using the pgx-zerolog adapter to enable high-performance structured JSON logging for PostgreSQL operations. Logging Best Practices for Go Microservices Implement structured JSON logging with context propagation and trace IDs to effectively debug and monitor Go microservices. slog vs zap vs zerolog: Which Go Logger to Choose Select zap for speed, logrus for ecosystem, or zerolog for minimal overhead in Go logging. Use trace tool Control Go runtime behavior and compatibility using GODEBUG environment variables or source file directives. What Is Observability and Why It Matters for Go Services Observability enables Go developers to diagnose system health by analyzing logs, metrics, and traces generated from internal state.