package imgcache import ( "fmt" "github.com/prometheus/client_golang/prometheus" ) // registerMetrics makes the cache's metrics and registers them on // metricsRegistry. The metrics are kept in memory, so updating one waits // for nothing and makes no database call. It fails when metricsRegistry // already holds metrics of the same names. func (c *Cache) registerMetrics(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.sourceCacheHits = prometheus.NewCounter(prometheus.CounterOpts{ Name: "pixa_source_cache_hits_total", Help: "Variants made or checked whose source was in the cache.", }) c.sourceCacheMisses = prometheus.NewCounter(prometheus.CounterOpts{ Name: "pixa_source_cache_misses_total", Help: "Variants made or checked whose source was not in the cache.", }) c.usageBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "pixa_cache_usage_bytes", Help: "Bytes of sources and variants in the cache, as eviction last read them.", }) c.maxBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "pixa_cache_max_bytes", Help: "The disk cache limit in bytes, cache_max_bytes.", }) c.evictions = prometheus.NewCounter(prometheus.CounterOpts{ Name: "pixa_cache_evictions_total", Help: "Sources and variants removed by eviction.", }) c.evictedBytes = prometheus.NewCounter(prometheus.CounterOpts{ Name: "pixa_cache_evicted_bytes_total", Help: "Bytes of the sources and variants removed by eviction.", }) c.maxBytes.Set(float64(c.config.MaxBytes)) for _, metric := range []prometheus.Collector{ c.cacheHits, c.cacheMisses, c.sourceCacheHits, c.sourceCacheMisses, c.usageBytes, c.maxBytes, c.evictions, c.evictedBytes, } { err := metricsRegistry.Register(metric) if err != nil { return fmt.Errorf("failed to register the cache's metrics: %w", err) } } return nil } // IncrementStats counts a cache hit or miss of an image request. It only // adds to the cache's counters in memory, so it never waits for the // database or fails. func (c *Cache) IncrementStats(hit bool) { if hit { c.cacheHits.Inc() } else { c.cacheMisses.Inc() } } // IncrementSourceStats counts whether a variant being made or checked found // its source in the cache. Like IncrementStats, it never waits or fails. func (c *Cache) IncrementSourceStats(hit bool) { if hit { c.sourceCacheHits.Inc() } else { c.sourceCacheMisses.Inc() } } // countEviction counts one source or variant of sizeBytes bytes removed by // eviction. func (c *Cache) countEviction(sizeBytes int64) { c.evictions.Inc() c.evictedBytes.Add(float64(sizeBytes)) }