forked from okx/xlayer-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
97 lines (81 loc) · 2.77 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package metrics
import (
"time"
"github.com/0xPolygonHermez/zkevm-node/metrics"
"github.com/prometheus/client_golang/prometheus"
)
const (
prefix = "jsonrpc_"
requestPrefix = prefix + "request_"
requestsHandledName = requestPrefix + "handled"
requestDurationName = requestPrefix + "duration"
connName = requestPrefix + "connection"
requestHandledTypeLabelName = "type"
)
// RequestHandledLabel represents the possible values for the
// `jsonrpc_request_handled` metric `type` label.
type RequestHandledLabel string
// ConnLabel represents the possible values for the
// `jsonrpc_request_connection` metric `type` label.
type ConnLabel string
const (
// RequestHandledLabelInvalid represents an request of type invalid
RequestHandledLabelInvalid RequestHandledLabel = "invalid"
// RequestHandledLabelError represents an request of type error
RequestHandledLabelError RequestHandledLabel = "error"
// RequestHandledLabelSingle represents an request of type single
RequestHandledLabelSingle RequestHandledLabel = "single"
// RequestHandledLabelBatch represents an request of type batch
RequestHandledLabelBatch RequestHandledLabel = "batch"
// HTTPConnLabel represents a HTTP connection
HTTPConnLabel ConnLabel = "HTTP"
// WSConnLabel represents a WS connection
WSConnLabel ConnLabel = "WS"
)
// Register the metrics for the jsonrpc package.
func Register() {
var (
counterVecs []metrics.CounterVecOpts
histograms []prometheus.HistogramOpts
)
counterVecs = []metrics.CounterVecOpts{
{
CounterOpts: prometheus.CounterOpts{
Name: requestsHandledName,
Help: "[JSONRPC] number of requests handled",
},
Labels: []string{requestHandledTypeLabelName},
},
}
start := 0.1
width := 0.1
count := 10
histograms = []prometheus.HistogramOpts{
{
Name: requestDurationName,
Help: "[JSONRPC] Histogram for the runtime of requests",
Buckets: prometheus.LinearBuckets(start, width, count),
},
}
metrics.RegisterCounterVecs(counterVecs...)
metrics.RegisterHistograms(histograms...)
// XLayer handler
metrics.RegisterCounterVecs(counterVecsXLayer...)
metrics.RegisterHistogramVecs(histogramVecs...)
metrics.RegisterGaugeVecs(gaugeVecs...)
}
// CountConn increments the connection counter vector by one for the
// given label.
func CountConn(label ConnLabel) {
metrics.CounterVecInc(connName, string(label))
}
// RequestHandled increments the requests handled counter vector by one for the
// given label.
func RequestHandled(label RequestHandledLabel) {
metrics.CounterVecInc(requestsHandledName, string(label))
}
// RequestDuration observes (histogram) the duration of a request from the
// provided starting time.
func RequestDuration(start time.Time) {
metrics.HistogramObserve(requestDurationName, time.Since(start).Seconds())
}