13937: Export stats as prometheus metrics. (WIP)
[arvados.git] / services / keepstore / stats_ticker.go
index f3a79c68f3691e54bfee58f54a1f890a922c2273..36fbcf98af216183afc0169b7643c5a1afcfafb2 100644 (file)
@@ -1,8 +1,15 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 package main
 
 import (
+       "fmt"
        "sync"
        "sync/atomic"
+
+       "github.com/prometheus/client_golang/prometheus"
 )
 
 type statsTicker struct {
@@ -14,6 +21,28 @@ type statsTicker struct {
        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) },
+               ))
+       }
+}
+
 // Tick increments each of the given counters by 1 using
 // atomic.AddUint64.
 func (s *statsTicker) Tick(counters ...*uint64) {