Refactor the multi-host salt install page.
[arvados.git] / services / keepstore / stats_ticker.go
index 6742dbaa9aa1a8669c875266c9cac09dce7a8a48..520d4530c2ac2404ca47f561b3dd08c015f1e832 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepstore
 
 import (
        "sync"
@@ -16,14 +16,12 @@ type statsTicker struct {
        InBytes  uint64
        OutBytes uint64
 
-       // Prometheus metrics
-       PromErrors     prometheus.Counter
-       PromInBytes    prometheus.Counter
-       PromOutBytes   prometheus.Counter
-       PromErrorCodes *prometheus.CounterVec
-
        ErrorCodes map[string]uint64 `json:",omitempty"`
        lock       sync.Mutex
+
+       opsCounters *prometheus.CounterVec
+       errCounters *prometheus.CounterVec
+       ioBytes     *prometheus.CounterVec
 }
 
 // Tick increments each of the given counters by 1 using
@@ -41,7 +39,6 @@ func (s *statsTicker) TickErr(err error, errType string) {
        if err == nil {
                return
        }
-       s.PromErrors.Inc()
        s.Tick(&s.Errors)
 
        s.lock.Lock()
@@ -50,17 +47,33 @@ func (s *statsTicker) TickErr(err error, errType string) {
        }
        s.ErrorCodes[errType]++
        s.lock.Unlock()
-       s.PromErrorCodes.WithLabelValues(errType).Inc()
+       if s.errCounters != nil {
+               s.errCounters.With(prometheus.Labels{"error_type": errType}).Inc()
+       }
 }
 
 // TickInBytes increments the incoming byte counter by n.
 func (s *statsTicker) TickInBytes(n uint64) {
-       s.PromInBytes.Add(float64(n))
+       if s.ioBytes != nil {
+               s.ioBytes.With(prometheus.Labels{"direction": "in"}).Add(float64(n))
+       }
        atomic.AddUint64(&s.InBytes, n)
 }
 
 // TickOutBytes increments the outgoing byte counter by n.
 func (s *statsTicker) TickOutBytes(n uint64) {
-       s.PromOutBytes.Add(float64(n))
+       if s.ioBytes != nil {
+               s.ioBytes.With(prometheus.Labels{"direction": "out"}).Add(float64(n))
+       }
        atomic.AddUint64(&s.OutBytes, n)
 }
+
+// TickOps increments the counter of the listed operations by 1.
+func (s *statsTicker) TickOps(operations ...string) {
+       if s.opsCounters == nil {
+               return
+       }
+       for _, opType := range operations {
+               s.opsCounters.With(prometheus.Labels{"operation": opType}).Inc()
+       }
+}