Refactor the multi-host salt install page.
[arvados.git] / services / keepstore / stats_ticker.go
index 36fbcf98af216183afc0169b7643c5a1afcfafb2..520d4530c2ac2404ca47f561b3dd08c015f1e832 100644 (file)
@@ -2,10 +2,9 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepstore
 
 import (
-       "fmt"
        "sync"
        "sync/atomic"
 
@@ -19,28 +18,10 @@ type statsTicker struct {
 
        ErrorCodes map[string]uint64 `json:",omitempty"`
        lock       sync.Mutex
-}
 
-func (s *statsTicker) setupPrometheus(drv string, reg *prometheus.Registry, lbl prometheus.Labels) {
-       metrics := map[string][]interface{}{
-               "errors":    []interface{}{string("errors"), s.Errors},
-               "in_bytes":  []interface{}{string("input bytes"), s.InBytes},
-               "out_bytes": []interface{}{string("output bytes"), s.OutBytes},
-       }
-       for mName, data := range metrics {
-               mHelp := data[0].(string)
-               mVal := data[1].(uint64)
-               reg.Register(prometheus.NewGaugeFunc(
-                       prometheus.GaugeOpts{
-                               Namespace:   "arvados",
-                               Subsystem:   "keepstore",
-                               Name:        fmt.Sprintf("%s_%s", drv, mName),
-                               Help:        fmt.Sprintf("Number of %s backend %s", drv, mHelp),
-                               ConstLabels: lbl,
-                       },
-                       func() float64 { return float64(mVal) },
-               ))
-       }
+       opsCounters *prometheus.CounterVec
+       errCounters *prometheus.CounterVec
+       ioBytes     *prometheus.CounterVec
 }
 
 // Tick increments each of the given counters by 1 using
@@ -66,14 +47,33 @@ func (s *statsTicker) TickErr(err error, errType string) {
        }
        s.ErrorCodes[errType]++
        s.lock.Unlock()
+       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) {
+       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) {
+       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()
+       }
+}