httpcache includes optional Prometheus metrics integration to monitor cache performance, HTTP requests, and resource usage. The metrics system is:
- Zero-dependency by default - Metrics are opt-in and don't add dependencies to the core package
- Non-intrusive - Works with any existing cache backend
- Production-ready - Battle-tested metric types and labels
- Integration-friendly - Easily integrates with existing Prometheus setups
import (
"github.com/sandrolain/httpcache"
prommetrics "github.com/sandrolain/httpcache/wrapper/metrics/prometheus"
)
// Create metrics collector
collector := prommetrics.NewCollector()
// Wrap your cache
cache := httpcache.NewMemoryCache()
instrumentedCache := prommetrics.NewInstrumentedCache(cache, "memory", collector)
// Wrap your transport
transport := httpcache.NewTransport(instrumentedCache)
instrumentedTransport := prommetrics.NewInstrumentedTransport(transport, collector)
// Use the instrumented client
client := instrumentedTransport.Client()
// Metrics are automatically exposed via Prometheus default registry
// Access them at your /metrics endpoint| Metric | Type | Labels | Description |
|---|---|---|---|
httpcache_cache_requests_total |
Counter | backend, operation, result |
Total cache operations (get/set/delete) |
httpcache_cache_operation_duration_seconds |
Histogram | backend, operation |
Cache operation latency |
httpcache_cache_size_bytes |
Gauge | backend |
Current cache size in bytes |
httpcache_cache_entries |
Gauge | backend |
Number of cached entries |
| Metric | Type | Labels | Description |
|---|---|---|---|
httpcache_http_requests_total |
Counter | method, cache_status |
HTTP requests by cache hit/miss/revalidated |
httpcache_http_request_duration_seconds |
Histogram | method, cache_status |
HTTP request duration |
httpcache_http_response_size_bytes_total |
Counter | method, cache_status |
Total response sizes |
httpcache_stale_responses_total |
Counter | method |
Stale responses served (RFC 5861) |
rate(httpcache_cache_requests_total{result="hit"}[5m]) /
rate(httpcache_cache_requests_total{operation="get"}[5m]) * 100
histogram_quantile(0.95,
rate(httpcache_cache_operation_duration_seconds_bucket[5m]))
httpcache_http_response_size_bytes_total{cache_status="hit"}
sum by (cache_status) (rate(httpcache_http_requests_total[5m]))
If your application already has Prometheus metrics, httpcache metrics are automatically included:
// Your existing Prometheus setup
http.Handle("/metrics", promhttp.Handler())
// httpcache metrics use the default registry
collector := prommetrics.NewCollector()
// All metrics are exposed together at /metricsFor custom namespaces or registries:
customRegistry := prometheus.NewRegistry()
collector := prommetrics.NewCollectorWithConfig(prommetrics.CollectorConfig{
Registry: customRegistry,
Namespace: "myapp", // Use "myapp_cache_requests_total" instead of "httpcache_..."
})collector := prommetrics.NewCollectorWithConfig(prommetrics.CollectorConfig{
HistogramBuckets: []float64{.001, .005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10},
})collector := prommetrics.NewCollectorWithConfig(prommetrics.CollectorConfig{
Namespace: "myapp", // Metrics will be prefixed with "myapp_"
})registry := prometheus.NewRegistry()
collector := prommetrics.NewCollectorWithConfig(prommetrics.CollectorConfig{
Registry: registry,
})
// Expose on separate endpoint
http.Handle("/httpcache-metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))See examples/prometheus/README.md for Grafana dashboard recommendations and sample queries.
- Label Cardinality: Keep label values bounded to avoid metric explosion
- Namespaces: Use custom namespaces when running multiple instances
- Alerting: Set up alerts for low hit rates or high latencies
- Sampling: Consider sampling for very high-traffic applications
For a complete working example, see examples/prometheus/.