package imgcache import ( "fmt" "github.com/prometheus/client_golang/prometheus" ) // registerCounters makes the cache's counters and registers them on // metricsRegistry. The counters are kept in memory, so adding to one waits // for nothing and makes no database call. It fails when metricsRegistry // already holds counters of the same names. func (c *Cache) registerCounters(metricsRegistry prometheus.Registerer) error { c.cacheHits = prometheus.NewCounter(prometheus.CounterOpts{ Name: "pixa_cache_hits_total", Help: "Image requests served from the cache.", }) c.cacheMisses = prometheus.NewCounter(prometheus.CounterOpts{ Name: "pixa_cache_misses_total", Help: "Image requests not served from the cache.", }) for _, counter := range []prometheus.Counter{c.cacheHits, c.cacheMisses} { err := metricsRegistry.Register(counter) if err != nil { return fmt.Errorf("failed to register the cache's counters: %w", err) } } return nil }