Merge branch '13937-keepstore-prometheus'
[arvados.git] / services / keepstore / config.go
index c9c9ae1158ec323f572524adb3e7586590d8f788..43a2191111376fbf86c6943ffffff6c22668aa38 100644 (file)
@@ -9,17 +9,12 @@ import (
        "encoding/json"
        "fmt"
        "io/ioutil"
-       "net/http"
-       "strconv"
        "strings"
        "time"
 
        "git.curoverse.com/arvados.git/sdk/go/arvados"
-       "git.curoverse.com/arvados.git/sdk/go/stats"
-       "github.com/Sirupsen/logrus"
-       "github.com/golang/protobuf/jsonpb"
        "github.com/prometheus/client_golang/prometheus"
-       "github.com/prometheus/client_golang/prometheus/promhttp"
+       "github.com/sirupsen/logrus"
 )
 
 type Config struct {
@@ -53,8 +48,6 @@ type Config struct {
        debugLogf       func(string, ...interface{})
 
        ManagementToken string
-
-       metrics
 }
 
 var (
@@ -89,7 +82,7 @@ func DefaultConfig() *Config {
 
 // Start should be called exactly once: after setting all public
 // fields, and before using the config.
-func (cfg *Config) Start() error {
+func (cfg *Config) Start(reg *prometheus.Registry) error {
        if cfg.Debug {
                log.Level = logrus.DebugLevel
                cfg.debugLogf = log.Printf
@@ -151,8 +144,9 @@ func (cfg *Config) Start() error {
                        return fmt.Errorf("no volumes found")
                }
        }
+       vm := newVolumeMetricsVecs(reg)
        for _, v := range cfg.Volumes {
-               if err := v.Start(); err != nil {
+               if err := v.Start(vm); err != nil {
                        return fmt.Errorf("volume %s: %s", v, err)
                }
                log.Printf("Using volume %v (writable=%v)", v, v.Writable())
@@ -160,62 +154,6 @@ func (cfg *Config) Start() error {
        return nil
 }
 
-type metrics struct {
-       registry     *prometheus.Registry
-       reqDuration  *prometheus.SummaryVec
-       timeToStatus *prometheus.SummaryVec
-       exportProm   http.Handler
-}
-
-func (*metrics) Levels() []logrus.Level {
-       return logrus.AllLevels
-}
-
-func (m *metrics) Fire(ent *logrus.Entry) error {
-       if tts, ok := ent.Data["timeToStatus"].(stats.Duration); !ok {
-       } else if method, ok := ent.Data["reqMethod"].(string); !ok {
-       } else if code, ok := ent.Data["respStatusCode"].(int); !ok {
-       } else {
-               m.timeToStatus.WithLabelValues(strconv.Itoa(code), strings.ToLower(method)).Observe(time.Duration(tts).Seconds())
-       }
-       return nil
-}
-
-func (m *metrics) setup() {
-       m.registry = prometheus.NewRegistry()
-       m.timeToStatus = prometheus.NewSummaryVec(prometheus.SummaryOpts{
-               Name: "time_to_status_seconds",
-               Help: "Summary of request TTFB.",
-       }, []string{"code", "method"})
-       m.reqDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
-               Name: "request_duration_seconds",
-               Help: "Summary of request duration.",
-       }, []string{"code", "method"})
-       m.registry.MustRegister(m.timeToStatus)
-       m.registry.MustRegister(m.reqDuration)
-       m.exportProm = promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{
-               ErrorLog: log,
-       })
-       log.AddHook(m)
-}
-
-func (m *metrics) exportJSON(w http.ResponseWriter, req *http.Request) {
-       jm := jsonpb.Marshaler{Indent: "  "}
-       mfs, _ := m.registry.Gather()
-       w.Write([]byte{'['})
-       for i, mf := range mfs {
-               if i > 0 {
-                       w.Write([]byte{','})
-               }
-               jm.Marshal(w, mf)
-       }
-       w.Write([]byte{']'})
-}
-
-func (m *metrics) Instrument(next http.Handler) http.Handler {
-       return promhttp.InstrumentHandlerDuration(m.reqDuration, next)
-}
-
 // VolumeTypes is built up by init() funcs in the source files that
 // define the volume types.
 var VolumeTypes = []func() VolumeWithExamples{}
@@ -229,7 +167,7 @@ func (vl *VolumeList) UnmarshalJSON(data []byte) error {
        for _, factory := range VolumeTypes {
                t := factory().Type()
                if _, ok := typeMap[t]; ok {
-                       log.Fatal("volume type %+q is claimed by multiple VolumeTypes")
+                       log.Fatalf("volume type %+q is claimed by multiple VolumeTypes", t)
                }
                typeMap[t] = factory
        }