forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
metrics.go
73 lines (63 loc) · 1.82 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
71
72
73
// Copyright (C) 2019-2023, 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/MetalBlockchain/metalgo/utils"
)
type metrics struct {
numProcessing *prometheus.GaugeVec
numCalls *prometheus.CounterVec
totalDuration *prometheus.GaugeVec
}
func newMetrics(namespace string, registerer prometheus.Registerer) (*metrics, error) {
m := &metrics{
numProcessing: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "calls_processing",
Help: "The number of calls this API is currently processing",
},
[]string{"base"},
),
numCalls: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "calls",
Help: "The number of calls this API has processed",
},
[]string{"base"},
),
totalDuration: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
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)
})
}