-
Notifications
You must be signed in to change notification settings - Fork 662
/
metrics.go
70 lines (60 loc) · 1.69 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package server
import (
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/utils"
)
type metrics struct {
numProcessing *prometheus.GaugeVec
numCalls *prometheus.CounterVec
totalDuration *prometheus.GaugeVec
}
func newMetrics(registerer prometheus.Registerer) (*metrics, error) {
m := &metrics{
numProcessing: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "calls_processing",
Help: "The number of calls this API is currently processing",
},
[]string{"base"},
),
numCalls: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "calls",
Help: "The number of calls this API has processed",
},
[]string{"base"},
),
totalDuration: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "calls_duration",
Help: "The total amount of time, in nanoseconds, spent handling API calls",
},
[]string{"base"},
),
}
err := utils.Err(
registerer.Register(m.numProcessing),
registerer.Register(m.numCalls),
registerer.Register(m.totalDuration),
)
return m, err
}
func (m *metrics) wrapHandler(chainName string, handler http.Handler) http.Handler {
numProcessing := m.numProcessing.WithLabelValues(chainName)
numCalls := m.numCalls.WithLabelValues(chainName)
totalDuration := m.totalDuration.WithLabelValues(chainName)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
numProcessing.Inc()
defer func() {
numProcessing.Dec()
numCalls.Inc()
totalDuration.Add(float64(time.Since(startTime)))
}()
handler.ServeHTTP(w, r)
})
}