Golang Observability
Samuel BertheCommunityCreated by samberDistributed by Samuel Berthe
- Skill
- Tool integration
- Cloud and DevOps
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a standardized approach to instrumenting Go applications for production monitoring. It covers the five essential signals—logs, metrics, traces, profiles, and real-user monitoring—to ensure full system visibility. It emphasizes best practices such as structured logging with slog, histogram-based latency tracking, context propagation for distributed tracing, and proactive alerting. The skill includes guidance on migrating legacy loggers, correlating signals for easier debugging, and ensuring features are production-ready through rigorous observability standards.
What it does
- Provides best practices for structured logging using log/slog.
- Guides the implementation of Prometheus metrics and OpenTelemetry tracing.
- Offers strategies for continuous profiling and real-user monitoring.
- Details how to correlate logs, metrics, and traces for effective diagnostics.
- Includes a checklist for ensuring features meet production observability standards.
- Provides guidance on migrating from legacy logging libraries to the standard library.
When to use it
- When instrumenting new Go services for production monitoring.
- When adding observability to existing features or refactoring legacy code.
- When setting up alerting rules or Grafana dashboards for Go applications.
- When migrating legacy loggers like zap or logrus to the standard log/slog library.
- When implementing GDPR/CCPA-compliant event tracking.
Advantages
- Promotes consistent observability patterns across Go codebases.
- Reduces technical debt by standardizing on Go's native logging and instrumentation.
- Improves incident response times through better signal correlation.
- Ensures production readiness by enforcing observability as a definition of done.
- Provides clear guidance on avoiding common pitfalls like high-cardinality labels.
Usage examples
- “How should I instrument a new HTTP endpoint in my Go service for observability?”
- The agent will guide you to add structured logging with slog, declare Prometheus metrics for latency and error rates, and create OpenTelemetry spans for the request flow.
- “Help me migrate my project from zap to log/slog.”
- The agent will provide a step-by-step migration strategy, including using bridge handlers to maintain compatibility while gradually replacing log calls.
- “How do I link my logs to my traces in Go?”
- The agent will show you how to use the otelslog bridge to automatically inject trace_id and span_id into your logs using context.
- “What is the best way to track latency for my API endpoints?”
- The agent will recommend using Prometheus Histograms instead of Summaries to allow for server-side aggregation and accurate percentile calculation.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/samber/golang-observability/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip golang-observability.zip -d .agents/skills
Documentation by samber
Persona: You are a Go observability engineer. You treat every unobserved production system as a liability — instrument proactively, correlate signals to diagnose, and never consider a feature done until it is observable.
Orchestration mode: Fan out the five signal-specific sub-agents described in Audit mode (metrics, logging, tracing, profiling, RUM) for auditing observability coverage across a codebase, and merge their coverage findings. On Claude Code, use ultracode to opt into multi-agent orchestration explicitly.
Modes:
Coding / instrumentation (default): Add observability to new or existing code — declare metrics, add spans, set up structured logging, wire pprof toggles. Follow the sequential instrumentation guide.
Review mode — reviewing a PR's instrumentation changes. Check that new code exports the expected signals (metrics declared, spans opened and closed, structured log fields consistent). Sequential.
Audit mode — auditing existing observability coverage across a codebase. Launch up to 5 parallel sub-agents — one per signal (metrics, logging, tracing, profiling, RUM) — to check coverage simultaneously.
Community default. A company skill that explicitly supersedes
samber/cc-skills-golang@golang-observabilityskill takes precedence.
Go Observability Best Practices
Observability is the ability to understand a system's internal state from its external outputs. In Go services, this means five complementary signals: logs, metrics, traces, profiles, and RUM. Each answers different questions, and together they give you full visibility into both system behavior and user experience.
When using observability libraries (Prometheus client, OpenTelemetry SDK, vendor integrations), refer to the library's official documentation and code examples for current API signatures.
Best Practices Summary
Use structured logging with
log/slog— production services MUST emit structured logs (JSON), not freeform stringsChoose the right log level — Debug for development, Info for normal operations, Warn for degraded states, Error for failures requiring attention
Log with context — use
slog.InfoContext(ctx, ...)to correlate logs with tracesPrefer Histogram over Summary for latency metrics — Histograms support server-side aggregation and percentile queries. Every HTTP endpoint MUST have latency and error rate metrics.
Keep label cardinality low in Prometheus — NEVER use unbounded values (user IDs, full URLs) as label values
Track percentiles (P50, P90, P99, P99.9) using Histograms +
histogram_quantile()in PromQLSet up OpenTelemetry tracing on new projects — configure the TracerProvider early, then add spans everywhere
Add spans to every meaningful operation — service methods, DB queries, external API calls, message queue operations
Propagate context everywhere — context is the vehicle that carries trace_id, span_id, and deadlines across service boundaries
Enable profiling via environment variables — toggle pprof and continuous profiling on/off without redeploying
Correlate signals — inject trace_id into logs, use exemplars to link metrics to traces
A feature is not done until it is observable — declare metrics, add proper logging, create spans
awesome-prometheus-alerts provides ~500 ready-to-use alerting rules organized by technology for infrastructure and dependency monitoring
Cross-References
→ See
samber/cc-skills-golang@golang-error-handlingskill for the single handling rule.→ See
samber/cc-skills-golang@golang-troubleshootingskill for using observability signals to diagnose production issues.→ See
samber/cc-skills-golang@golang-securityskill for protecting pprof endpoints and avoiding PII in logs.→ See
samber/cc-skills-golang@golang-contextskill for propagating trace context across service boundaries.→ See
samber/cc-skills@promql-cliskill for querying and exploring PromQL expressions against Prometheus from the CLI.
Go 1.26+: slog multi-handler
For simple fan-out to multiple slog handlers, prefer stdlib slog.NewMultiHandler before adding third-party handler-composition dependencies.
logger := slog.New(slog.NewMultiHandler(
slog.NewJSONHandler(os.Stdout, nil),
auditHandler,
))Use third-party slog handler libraries only when the stdlib handler composition is insufficient.
The Five Signals
| Signal | Question it answers | Tool | When to use |
|---|---|---|---|
| Logs | What happened? | log/slog | Discrete events, errors, audit trails |
| Metrics | How much / how fast? | Prometheus client | Aggregated measurements, alerting, SLOs |
| Traces | Where did time go? | OpenTelemetry | Request flow across services, latency breakdown |
| Profiles | Why is it slow / using memory? | pprof, Pyroscope | CPU hotspots, memory leaks, lock contention |
| RUM | How do users experience it? | PostHog, Segment | Product analytics, funnels, session replay |
Detailed Guides
Each signal has a dedicated guide with full code examples, configuration patterns, and cost analysis:
Structured Logging — Why structured logging matters for log aggregation at scale. Covers
log/slogsetup, log levels (Debug/Info/Warn/Error) and when to use each, request correlation with trace IDs, context propagation withslog.InfoContext, request-scoped attributes, the slog ecosystem (handlers, formatters, middleware), and migration strategies from zap/logrus/zerolog.Metrics Collection — Prometheus client setup and the four metric types (Counter for rate-of-change, Gauge for snapshots, Histogram for latency aggregation). Deep dive: why Histograms beat Summaries (server-side aggregation, supports
histogram_quantilePromQL), naming conventions, the PromQL-as-comments convention (write queries above metric declarations for discoverability), production-grade PromQL examples, multi-window SLO burn rate alerting, and the high-cardinality label problem (why unbounded values like user IDs destroy performance).Distributed Tracing — When and how to use OpenTelemetry SDK to trace request flows across services. Covers spans (creating, attributes, status recording),
otelhttpmiddleware for HTTP instrumentation, error recording withspan.RecordError(), trace sampling (why you can't collect everything at scale), propagating trace context across service boundaries, and cost optimization.Profiling — On-demand profiling with pprof (CPU, heap, goroutine, mutex, block profiles) — how to enable it in production, secure it with auth, and toggle via environment variables without redeploying. Continuous profiling with Pyroscope for always-on performance visibility. Cost implications of each profiling type and mitigation strategies.
Real User Monitoring — Understanding how users actually experience your service. Covers product analytics (event tracking, funnels), Customer Data Platform integration, and critical compliance: GDPR/CCPA consent checks, data subject rights (user deletion endpoints), and privacy checklist for tracking. Server-side event tracking (PostHog, Segment) and identity key best practices.
Alerting — Proactive problem detection. Covers the four golden signals (latency, traffic, errors, saturation), awesome-prometheus-alerts provides ~500 ready-to-use rules by technology, Go runtime alerts (goroutine leaks, GC pressure, OOM risk), severity levels, and common mistakes that break alerting (using
irateinstead ofrate, missingfor:duration to avoid flapping).Grafana Dashboards — Prebuilt dashboards for Go runtime monitoring (heap allocation, GC pause frequency, goroutine count, CPU). Explains the standard dashboards to install, how to customize them for your service, and when each dashboard answers a different operational question.
Correlating Signals
Signals are most powerful when connected. A trace_id in your logs lets you jump from a log line to the full request trace. An exemplar on a metric links a latency spike to the exact trace that caused it.
Logs + Traces: otelslog bridge
import "go.opentelemetry.io/contrib/bridges/otelslog"
// Create a logger that automatically injects trace_id and span_id
logger := otelslog.NewHandler("my-service")
slog.SetDefault(slog.New(logger))
// Now every slog call with context includes trace correlation
slog.InfoContext(ctx, "order created", "order_id", orderID)
// Output includes: {"trace_id":"abc123", "span_id":"def456", "msg":"order created", ...}Metrics + Traces: Exemplars
// When recording a histogram observation, attach the trace_id as an exemplar
// so you can jump from a P99 spike directly to the offending trace
obs := histogram.WithLabelValues("POST", "/orders")
if eo, ok := obs.(prometheus.ExemplarObserver); ok {
eo.ObserveWithExemplar(duration, prometheus.Labels{"trace_id": traceID})
} else {
obs.Observe(duration)
}Migrating Legacy Loggers
If the project currently uses zap, logrus, or zerolog, migrate to log/slog. It is the standard library logger since Go 1.21, has a stable API, and the ecosystem has consolidated around it. Continuing with third-party loggers means maintaining an extra dependency for no benefit.
Migration strategy:
Add
slogas the new logger withslog.SetDefault()Bridge handlers during migration route slog output through the existing logger: samber/slog-zap, samber/slog-logrus, samber/slog-zerolog
Gradually replace all
zap.L().Info(...)/logrus.Info(...)/log.Info().Msg(...)calls withslog.Info(...)Once fully migrated, remove the bridge handler and the old logger dependency
Definition of Done for Observability
A feature is not production-ready until it is observable. Before marking a feature as done, verify:
[ ] Metrics declared — counters for operations/errors, histograms for latencies, gauges for saturation. Each metric var has PromQL queries and alert rules as comments above its declaration.
[ ] Logging is proper — structured key-value pairs with
slog, context variants used (slog.InfoContext), no PII in logs, errors MUST be either logged OR returned (NEVER both).[ ] Spans created — every service method, DB query, and external API call has a span with relevant attributes, errors recorded with
span.RecordError().[ ] Dashboards and alerts exist — the PromQL from your metric comments is wired into Grafana dashboards and Prometheus alerting rules. Ready-to-use alert rules for common infrastructure dependencies are available at awesome-prometheus-alerts.
[ ] RUM events tracked — key business events tracked server-side (PostHog/Segment), identity key is
user_id(not email), consent checked before tracking.
Common Mistakes
// ✗ Bad — log AND return (error gets logged multiple times up the chain)
if err != nil {
slog.Error("query failed", "error", err)
return fmt.Errorf("query: %w", err)
}
// ✓ Good — return with context, log once at the top level
if err != nil {
return fmt.Errorf("querying users: %w", err)
}// ✗ Bad — high-cardinality label (unbounded user IDs)
httpRequests.WithLabelValues(r.Method, r.URL.Path, userID).Inc()
// ✓ Good — bounded label values only
httpRequests.WithLabelValues(r.Method, routePattern).Inc()// ✗ Bad — not passing context (breaks trace propagation)
result, err := db.Query("SELECT ...")
// ✓ Good — context flows through, trace continues
result, err := db.QueryContext(ctx, "SELECT ...")// ✗ Bad — using Summary for latency (can't aggregate across instances)
prometheus.NewSummary(prometheus.SummaryOpts{
Name: "http_request_duration_seconds",
Objectives: map[float64]float64{0.99: 0.001},
})
// ✓ Good — use Histogram (aggregatable, supports histogram_quantile)
prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Buckets: prometheus.DefBuckets,
})Files
- ATTRIBUTION.mdAdded by AutoGineer917 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md13.6 KiB
- evals/evals.json43.5 KiB
- references/alerting.md7.9 KiB
- references/dashboards.md1.7 KiB
- references/logging.md8.4 KiB
- references/metrics.md23.1 KiB
- references/profiling.md3.8 KiB
- references/rum.md11.7 KiB
- references/tracing.md7.7 KiB
references/metrics.md
Metrics with Prometheus
→ See samber/cc-skills-golang@golang-troubleshooting skill for using metrics to diagnose production issues. → See samber/cc-skills@promql-cli skill for executing and testing PromQL queries via CLI.
When using the Prometheus client library, refer to the library's official documentation for up-to-date API signatures and examples.
Table of Contents
Metric Types
Histogram vs Summary
Tracking Percentiles (P50, P90, P99, P99.9)
Naming Conventions
Exposing Metrics
Document Metrics with PromQL Comments
Metric Examples and PromQL Queries
Counters — tracking events
Gauges — tracking current state
Histograms — tracking distributions (recommended for latency)
Summary — client-side quantiles (use sparingly)
Multi-Window Burn-Rate SLO Alerting
High-Cardinality Labels
Metric Types
| Type | What it measures | Example | When to use |
|---|---|---|---|
| Counter | Cumulative total (only goes up) | Total requests, total errors | Counting events |
| Gauge | Current value (goes up and down) | In-flight requests, queue size, temperature | Current state |
| Histogram | Distribution of values in configurable buckets | Request duration, response size | Latency, sizes — when you need percentiles |
| Summary | Client-computed quantiles | Request duration (pre-computed P50, P99) | Rarely — prefer Histogram |
Histogram vs Summary
This is one of the most common sources of confusion. Both measure distributions, but they work very differently.
Histogram stores observations in configurable buckets (e.g., 5ms, 10ms, 25ms, 50ms, 100ms, ...). Percentiles are computed at query time by Prometheus using histogram_quantile(). Because the raw bucket counts are stored server-side, histograms can be aggregated across multiple instances — essential for services running multiple replicas.
Summary computes quantiles (P50, P99, etc.) on the client side before sending them to Prometheus. This means the quantile values are pre-baked and cannot be aggregated — if you have 10 instances, you cannot combine their P99 values into a meaningful overall P99.
Recommendation: Histogram SHOULD be preferred over Summary in almost all cases. Summary is only useful when you need exact quantiles for a single instance and don't care about cross-instance aggregation.
Tracking Percentiles (P50, P90, P99, P99.9)
Define a Histogram with appropriate buckets, then query percentiles with histogram_quantile():
import "github.com/prometheus/client_golang/prometheus"
import "github.com/prometheus/client_golang/prometheus/promauto"
var httpRequestDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "myapp",
Subsystem: "http",
Name: "request_duration_seconds",
Help: "HTTP request duration in seconds.",
Buckets: prometheus.DefBuckets, // .005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10
},
[]string{"method", "path", "status"},
)
// In your handler or middleware:
func instrumentHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
sw := &statusWriter{ResponseWriter: w, status: 200}
next.ServeHTTP(sw, r)
httpRequestDuration.WithLabelValues(
r.Method,
r.URL.Path,
strconv.Itoa(sw.status),
).Observe(time.Since(start).Seconds())
})
}PromQL queries for percentiles:
# P50 (median) over the last 5 minutes
histogram_quantile(0.50, rate(myapp_http_request_duration_seconds_bucket[5m]))
# P90
histogram_quantile(0.90, rate(myapp_http_request_duration_seconds_bucket[5m]))
# P99
histogram_quantile(0.99, rate(myapp_http_request_duration_seconds_bucket[5m]))
# P99.9
histogram_quantile(0.999, rate(myapp_http_request_duration_seconds_bucket[5m]))
# P99 broken down by path
histogram_quantile(0.99, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le, path))Naming Conventions
Metric names MUST follow the Prometheus naming best practices. The pattern is: <namespace>_<subsystem>_<name>_<unit>
Rules:
Use a single-word application prefix (namespace) relevant to the domain
A metric must refer to a single unit and single quantity
Include the unit as a suffix, in plural form
MUST use base units — not derived units
Always use base units:
| Measurement | Use | Not |
|---|---|---|
| Time | _seconds | _milliseconds, _minutes |
| Data size | _bytes | _kilobytes, _megabytes |
| Temperature | _celsius | _fahrenheit |
| Ratio/percent | _ratio (0–1) | _percent (0–100) |
| Mass | _grams | _kilograms |
Suffix conventions:
| Suffix | When to use | Example |
|---|---|---|
_total | Counters MUST use this suffix | myapp_http_requests_total |
_seconds | Duration measurements | myapp_http_request_duration_seconds |
_bytes | Data sizes | myapp_response_size_bytes |
_info | Pseudo-metrics exposing metadata | myapp_build_info |
_created | Creation timestamp of a counter | myapp_http_requests_created |
// ✓ Good — namespace, subsystem, descriptive name, base unit suffix
myapp_http_requests_total // Counter
myapp_http_request_duration_seconds // Histogram — seconds, not milliseconds
myapp_http_response_size_bytes // Histogram — bytes, not kilobytes
myapp_db_connections_active // Gauge
myapp_queue_messages_pending // Gauge
process_cpu_seconds_total // Counter — total CPU time in seconds
// ✗ Bad
request_count // no namespace, no unit suffix
httpDuration // camelCase, no unit
request_duration_ms // milliseconds instead of seconds
myapp_request_size_kb // kilobytes instead of bytesLabel naming: do not embed label names into the metric name. Use labels to differentiate characteristics:
// ✗ Bad — operation embedded in metric name
myapp_http_get_requests_total
myapp_http_post_requests_total
// ✓ Good — use a label
myapp_http_requests_total{method="GET"}
myapp_http_requests_total{method="POST"}Semantic consistency: sum() or avg() over all label dimensions of a metric should be meaningful. If not, split into separate metrics.
Exposing Metrics
import "github.com/prometheus/client_golang/prometheus/promhttp"
mux.Handle("/metrics", promhttp.Handler())Document Metrics with PromQL Comments
EVERY METRIC declaration SHOULD include the relevant PromQL queries and alert rules as comments directly above the variable. This makes metrics self-documenting — when a developer reads the code, they immediately see how the metric is used in dashboards and alerts, without hunting through Grafana or alert configurations.
// ✗ Bad — metric exists but nobody knows how to query or alert on it
var httpRequestsTotal = promauto.NewCounterVec(...)
// ✓ Good — PromQL queries and alert rules are part of the code
//
// Dashboard: rate(myapp_http_requests_total[5m])
// Dashboard: sum by (status) (rate(myapp_http_requests_total[5m]))
// Alert: sum(rate(myapp_http_requests_total{status=~"5.."}[5m])) / sum(rate(myapp_http_requests_total[5m])) > 0.01
var httpRequestsTotal = promauto.NewCounterVec(...)This convention has practical benefits: PromQL queries are reviewed in PRs alongside the metric, queries stay in sync with metric changes (label renames, bucket changes), and new team members can understand the metric's purpose at a glance.
Metric Examples and PromQL Queries
Production-ready metrics covering all four types with comprehensive PromQL for dashboards and alerts.
For infrastructure and dependency alerting (databases, caches, message brokers, reverse proxies, Kubernetes), awesome-prometheus-alerts provides a curated collection of ~500 ready-to-use Prometheus alerting rules organized by technology. See alerting.md for integration details and Go runtime alerts.
NEVER use irate(...) for alerts — use rate(...) instead.
Counters — tracking events
// Dashboard: rate(myapp_http_requests_total[5m])
// Dashboard: sum by (status) (rate(myapp_http_requests_total[5m]))
// Dashboard: sum by (path) (rate(myapp_http_requests_total[5m]))
// Dashboard: topk(5, sum by (path) (rate(myapp_http_requests_total[5m])))
// Dashboard: increase(myapp_http_requests_total[1h])
// SLI: 1 - (sum(rate(myapp_http_requests_total{status=~"5.."}[5m])) / sum(rate(myapp_http_requests_total[5m])))
// Alert: sum(rate(myapp_http_requests_total{status=~"5.."}[5m])) / sum(rate(myapp_http_requests_total[5m])) > 0.01
// Alert: sum(rate(myapp_http_requests_total{status=~"5.."}[1m])) / sum(rate(myapp_http_requests_total[1m])) > 0.05
var httpRequestsTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "myapp",
Subsystem: "http",
Name: "requests_total",
Help: "Total number of HTTP requests.",
},
[]string{"method", "path", "status"},
)
// Dashboard: sum by (type) (rate(myapp_errors_total[5m]))
// Dashboard: topk(3, sum by (type) (rate(myapp_errors_total[5m])))
// Alert: rate(myapp_errors_total{type="database"}[5m]) > 0.5
var errorsTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "myapp",
Name: "errors_total",
Help: "Total number of errors by type.",
},
[]string{"type"}, // "database", "external_api", "validation"
)
// Dashboard: sum by (payment_method) (rate(myapp_orders_created_total[5m]))
// Dashboard: increase(myapp_orders_created_total[24h])
// Alert: rate(myapp_orders_created_total[30m]) == 0
var ordersCreated = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "myapp",
Subsystem: "orders",
Name: "created_total",
Help: "Total number of orders created.",
},
[]string{"payment_method"},
)Key PromQL patterns for counters:
# Requests per second (smoothed over 5 minutes)
rate(myapp_http_requests_total[5m])
# Traffic by status code — see distribution of 2xx/4xx/5xx
sum by (status) (rate(myapp_http_requests_total[5m]))
# Top 5 busiest endpoints
topk(5, sum by (path) (rate(myapp_http_requests_total[5m])))
# Absolute request count in the last hour (useful for reports)
increase(myapp_http_requests_total[1h])
# Error ratio — fraction of requests returning 5xx (SLI)
sum(rate(myapp_http_requests_total{status=~"5.."}[5m]))
/
sum(rate(myapp_http_requests_total[5m]))
# 4xx error ratio — client errors (useful for spotting bad deployments)
sum(rate(myapp_http_requests_total{status=~"4.."}[5m]))
/
sum(rate(myapp_http_requests_total[5m]))
# Alert: error rate > 1% for 5 minutes (for: 5m)
sum(rate(myapp_http_requests_total{status=~"5.."}[5m]))
/
sum(rate(myapp_http_requests_total[5m]))
> 0.01
# Alert: spike detection — error rate > 5% over 1 minute (for: 2m)
sum(rate(myapp_http_requests_total{status=~"5.."}[1m]))
/
sum(rate(myapp_http_requests_total[1m]))
> 0.05
# Alert: zero orders for 30 minutes — business is broken (for: 30m)
rate(myapp_orders_created_total[30m]) == 0Gauges — tracking current state
// Dashboard: myapp_http_in_flight_requests
// Alert: myapp_http_in_flight_requests > 500
var httpInFlightRequests = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: "myapp",
Subsystem: "http",
Name: "in_flight_requests",
Help: "Number of HTTP requests currently being processed.",
},
)
// Dashboard: myapp_db_connections_active
// Dashboard: myapp_db_connections_active / myapp_db_connections_max
// Alert: myapp_db_connections_active{pool="write"} / myapp_db_connections_max{pool="write"} > 0.9
// Alert: predict_linear(myapp_db_connections_active[15m], 600) > myapp_db_connections_max
var dbConnectionsActive = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "myapp",
Subsystem: "db",
Name: "connections_active",
Help: "Number of active database connections.",
},
[]string{"pool"}, // "read", "write"
)
var dbConnectionsMax = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "myapp",
Subsystem: "db",
Name: "connections_max",
Help: "Maximum database connections in the pool.",
},
[]string{"pool"},
)
// Dashboard: myapp_queue_messages_pending
// Dashboard: deriv(myapp_queue_messages_pending[5m])
// Alert: myapp_queue_messages_pending{queue_name="orders"} > 1000
// Alert: deriv(myapp_queue_messages_pending[10m]) > 50
// Alert: predict_linear(myapp_queue_messages_pending[30m], 3600) > 10000
var queueSize = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "myapp",
Subsystem: "queue",
Name: "messages_pending",
Help: "Number of messages waiting to be processed.",
},
[]string{"queue_name"},
)
// Dashboard: myapp_workers_active / myapp_workers_max
// Alert: myapp_workers_active / myapp_workers_max > 0.8
var workersActive = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: "myapp",
Name: "workers_active",
Help: "Number of worker goroutines currently processing jobs.",
},
)
// Usage in middleware:
func instrumentMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
httpInFlightRequests.Inc()
defer httpInFlightRequests.Dec()
next.ServeHTTP(w, r)
})
}Key PromQL patterns for gauges:
# Current value — gauges are queried directly
myapp_http_in_flight_requests
# Saturation — what fraction of the pool is in use
myapp_db_connections_active{pool="write"} / myapp_db_connections_max{pool="write"}
# Rate of change — is the queue growing or shrinking? (items/second)
deriv(myapp_queue_messages_pending[5m])
# Prediction — will the connection pool be exhausted in 10 minutes?
# predict_linear extrapolates the trend from the last 15 minutes
predict_linear(myapp_db_connections_active[15m], 600) > myapp_db_connections_max
# Prediction — will the queue exceed 10k items in 1 hour?
predict_linear(myapp_queue_messages_pending[30m], 3600) > 10000
# Alert: connection pool > 90% saturated (for: 5m)
myapp_db_connections_active{pool="write"} / myapp_db_connections_max{pool="write"} > 0.9
# Alert: queue depth growing faster than 50 items/sec (for: 10m)
deriv(myapp_queue_messages_pending[10m]) > 50
# Alert: worker pool saturated (for: 5m)
myapp_workers_active / myapp_workers_max > 0.8Histograms — tracking distributions (recommended for latency)
// Dashboard: histogram_quantile(0.50, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le))
// Dashboard: histogram_quantile(0.90, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le))
// Dashboard: histogram_quantile(0.99, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le))
// Dashboard: histogram_quantile(0.99, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le, path))
// SLI: sum(rate(myapp_http_request_duration_seconds_bucket{le="0.3"}[5m])) / sum(rate(myapp_http_request_duration_seconds_count[5m]))
// Alert: histogram_quantile(0.99, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le)) > 2
var httpRequestDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "myapp",
Subsystem: "http",
Name: "request_duration_seconds",
Help: "HTTP request duration in seconds.",
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10},
},
[]string{"method", "path", "status"},
)
// Dashboard: histogram_quantile(0.95, sum(rate(myapp_external_call_duration_seconds_bucket[5m])) by (le, service))
// Alert: histogram_quantile(0.99, sum(rate(myapp_external_call_duration_seconds_bucket[5m])) by (le, service)) > 5
var externalAPICallDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "myapp",
Subsystem: "external",
Name: "call_duration_seconds",
Help: "Duration of external API calls in seconds.",
Buckets: []float64{.01, .05, .1, .25, .5, 1, 2.5, 5, 10, 30},
},
[]string{"service", "endpoint"},
)
// Dashboard: histogram_quantile(0.95, sum(rate(myapp_orders_amount_dollars_bucket[5m])) by (le))
var orderAmount = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "myapp",
Subsystem: "orders",
Name: "amount_dollars",
Help: "Order amount in dollars.",
Buckets: []float64{1, 5, 10, 25, 50, 100, 250, 500, 1000, 5000},
},
[]string{"payment_method"},
)Key PromQL patterns for histograms:
# Percentile latencies — the core latency dashboard
histogram_quantile(0.50, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le)) # P50
histogram_quantile(0.90, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le)) # P90
histogram_quantile(0.95, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le)) # P95
histogram_quantile(0.99, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le)) # P99
histogram_quantile(0.999, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le)) # P99.9
# P99 latency broken down by endpoint — find the slowest paths
histogram_quantile(0.99, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le, path))
# Average latency (mean) — useful alongside percentiles
sum(rate(myapp_http_request_duration_seconds_sum[5m]))
/
sum(rate(myapp_http_request_duration_seconds_count[5m]))
# Apdex-like SLI — fraction of requests under 300ms (target threshold)
sum(rate(myapp_http_request_duration_seconds_bucket{le="0.3"}[5m]))
/
sum(rate(myapp_http_request_duration_seconds_count[5m]))
# Request throughput from histogram (requests/sec)
sum(rate(myapp_http_request_duration_seconds_count[5m]))
# External API P95 latency per service
histogram_quantile(0.95, sum(rate(myapp_external_call_duration_seconds_bucket[5m])) by (le, service))
# Alert: P99 latency > 2s (for: 5m)
histogram_quantile(0.99, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le)) > 2
# Alert: P95 latency > 500ms (for: 10m)
histogram_quantile(0.95, sum(rate(myapp_http_request_duration_seconds_bucket[5m])) by (le)) > 0.5
# Alert: external API P99 > 5s (for: 5m)
histogram_quantile(0.99, sum(rate(myapp_external_call_duration_seconds_bucket[5m])) by (le, service)) > 5
# Alert: less than 95% of requests under 300ms (SLO breach) (for: 10m)
(
sum(rate(myapp_http_request_duration_seconds_bucket{le="0.3"}[5m]))
/
sum(rate(myapp_http_request_duration_seconds_count[5m]))
) < 0.95Summary — client-side quantiles (use sparingly)
Summaries compute quantiles on the client and cannot be aggregated across instances. Use them only for single-process diagnostics where exact quantiles matter. Prefer Histogram in all other cases.
// Dashboard: myapp_jobs_processing_seconds{quantile="0.5"}
// Dashboard: myapp_jobs_processing_seconds{quantile="0.99"}
// Note: these quantiles CANNOT be aggregated across instances
var jobProcessingDuration = promauto.NewSummary(
prometheus.SummaryOpts{
Namespace: "myapp",
Subsystem: "jobs",
Name: "processing_seconds",
Help: "Job processing duration in seconds.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
MaxAge: 10 * time.Minute,
},
)Multi-Window Burn-Rate SLO Alerting
For critical services, simple threshold alerts ("error rate > 1%") fire too late for fast incidents and too early for slow ones. Multi-window burn-rate alerting scales alert urgency to how fast you're consuming your error budget.
For a 99.9% availability SLO (0.1% error budget over 30 days):
| Window | Burn rate | Error rate | Severity | Meaning |
|---|---|---|---|---|
| 5m + 1h | 14.4x | > 1.44% | Critical (page) | Budget exhausted in ~2 days |
| 30m + 6h | 6x | > 0.6% | Critical (page) | Budget exhausted in ~5 days |
| 2h + 24h | 1x | > 0.1% | Warning (ticket) | On track to exhaust budget |
# Fast burn — page immediately (for: 2m)
# Both short and long windows must fire to avoid noise from brief spikes
(
(1 - sum(rate(myapp_http_requests_total{status=~"2.."}[5m])) / sum(rate(myapp_http_requests_total[5m]))) > 0.0144
and
(1 - sum(rate(myapp_http_requests_total{status=~"2.."}[1h])) / sum(rate(myapp_http_requests_total[1h]))) > 0.0144
)
# Medium burn — page (for: 15m)
(
(1 - sum(rate(myapp_http_requests_total{status=~"2.."}[30m])) / sum(rate(myapp_http_requests_total[30m]))) > 0.006
and
(1 - sum(rate(myapp_http_requests_total{status=~"2.."}[6h])) / sum(rate(myapp_http_requests_total[6h]))) > 0.006
)
# Slow burn — ticket (for: 1h)
(
(1 - sum(rate(myapp_http_requests_total{status=~"2.."}[2h])) / sum(rate(myapp_http_requests_total[2h]))) > 0.001
and
(1 - sum(rate(myapp_http_requests_total{status=~"2.."}[24h])) / sum(rate(myapp_http_requests_total[24h]))) > 0.001
)The short window catches the incident fast; the long window confirms it's sustained. Together they eliminate false positives from transient blips.
High-Cardinality Labels
NEVER use high-cardinality labels (user IDs, full URLs, request IDs). Every unique combination of label values creates a separate time series in Prometheus. Unbounded labels cause memory explosion on the Prometheus server, slow queries, and can crash the monitoring stack.
// ✗ Bad — unbounded cardinality (millions of unique values)
httpRequestsTotal.WithLabelValues(r.URL.Path) // /users/alice, /users/bob, /users/charlie...
httpRequestsTotal.WithLabelValues(userID) // one series per user
httpRequestsTotal.WithLabelValues(r.Header.Get("X-Request-ID")) // one series per request!
// ✓ Good — bounded, normalized labels
httpRequestsTotal.WithLabelValues(routePattern) // /users/:id (the route template, not the actual path)
httpRequestsTotal.WithLabelValues(r.Method) // GET, POST, PUT, DELETE (5 values)
httpRequestsTotal.WithLabelValues(statusBucket) // "2xx", "3xx", "4xx", "5xx" (4 values)How to limit cardinality:
Use route templates (
/users/:id) instead of actual paths (/users/alice)Bucket status codes (
2xx,4xx,5xx) instead of exact codes (200, 201, 204, 400, 401, ...)Never use user IDs, request IDs, session IDs, or email addresses as labels
Use attributes/tags in traces instead — traces handle high cardinality naturally
Rule of thumb: if a label can have more than ~100 unique values, it's too many
Requirements and compatibility
Written for: Claude Code, OpenAI Codex
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Declares allowed tools, Claude Code frontmatter
The author says: Designed for Claude Code, Codex or similar harness, and for projects using Golang.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- samber
- Publisher
- Samuel Berthe (Community)
- Distributed by
- Samuel Berthe
- Source
- samber/cc-skills-golang · skills/golang-observability · commit bb1d43d
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by samber or Samuel Berthe.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download golang-observability.zip