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.", }) c.upstreamFetches = prometheus.NewCounter(prometheus.CounterOpts{ Name: "pixa_upstream_fetches_total", Help: "Fetches of a source image from upstream that read any bytes.", }) c.upstreamFetchBytes = prometheus.NewCounter(prometheus.CounterOpts{ Name: "pixa_upstream_fetch_bytes_total", Help: "Bytes read from upstream by fetches of source images.", }) c.transforms = prometheus.NewCounter(prometheus.CounterOpts{ Name: "pixa_transforms_total", Help: "Images the image processor transformed.", }) for _, counter := range []prometheus.Counter{ c.cacheHits, c.cacheMisses, c.upstreamFetches, c.upstreamFetchBytes, c.transforms, } { err := metricsRegistry.Register(counter) if err != nil { return fmt.Errorf("failed to register the cache's counters: %w", err) } } return nil }