Skip to main content
Copper’s cmetrics package provides Prometheus metrics with a declare-then-use model: register your counters and histograms up front, then emit them from anywhere. The cprometheus package (from gocopper/pkg) exposes them over HTTP.

Declaring Metrics

To declare your application’s metrics, provide a *cmetrics.Registry and add cmetrics.WireModule to your wire set:
pkg/app/metrics.go
Projects scaffolded with copper create already include this file and the wiring, so you only need to add your metrics to the registry.

Emitting Metrics

To emit a metric, inject cmetrics.Metrics and reference the metric by name:
Emitting an unregistered metric name (or a mismatched label set) logs a warning and does nothing; it will never panic or return an error.

Built-in HTTP Metrics

Every registry automatically includes two built-in metrics, which are emitted by chttp.RequestLoggerMiddleware (wired in by default):
  • http_requests_total{status_code, path}
  • http_request_duration_seconds{status_code, path}
The path label is the route template (/api/rockets/{id}) rather than the raw URL, so cardinality stays manageable.

Exposing the Metrics Endpoint

To expose your metrics over HTTP, add cprometheus.WireModule, register *cprometheus.Router in your handler, and enable the endpoint in your config:
The endpoint is disabled by default, so you may enable it only in the environments that Prometheus scrapes.