github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/jsonrpc/metrics/metrics.go (about) 1 package metrics 2 3 import ( 4 "time" 5 6 "github.com/0xPolygon/supernets2-node/metrics" 7 "github.com/prometheus/client_golang/prometheus" 8 ) 9 10 const ( 11 prefix = "jsonrpc_" 12 requestPrefix = prefix + "request_" 13 requestsHandledName = requestPrefix + "handled" 14 requestDurationName = requestPrefix + "duration" 15 16 requestHandledTypeLabelName = "type" 17 ) 18 19 // RequestHandledLabel represents the possible values for the 20 // `jsonrpc_request_handled` metric `type` label. 21 type RequestHandledLabel string 22 23 const ( 24 // RequestHandledLabelInvalid represents an request of type invalid 25 RequestHandledLabelInvalid RequestHandledLabel = "invalid" 26 // RequestHandledLabelSingle represents an request of type single 27 RequestHandledLabelSingle RequestHandledLabel = "single" 28 // RequestHandledLabelBatch represents an request of type batch 29 RequestHandledLabelBatch RequestHandledLabel = "batch" 30 ) 31 32 // Register the metrics for the jsonrpc package. 33 func Register() { 34 var ( 35 counterVecs []metrics.CounterVecOpts 36 histograms []prometheus.HistogramOpts 37 ) 38 39 counterVecs = []metrics.CounterVecOpts{ 40 { 41 CounterOpts: prometheus.CounterOpts{ 42 Name: requestsHandledName, 43 Help: "[JSONRPC] number of requests handled", 44 }, 45 Labels: []string{requestHandledTypeLabelName}, 46 }, 47 } 48 49 start := 0.1 50 width := 0.1 51 count := 10 52 histograms = []prometheus.HistogramOpts{ 53 { 54 Name: requestDurationName, 55 Help: "[JSONRPC] Histogram for the runtime of requests", 56 Buckets: prometheus.LinearBuckets(start, width, count), 57 }, 58 } 59 60 metrics.RegisterCounterVecs(counterVecs...) 61 metrics.RegisterHistograms(histograms...) 62 } 63 64 // RequestHandled increments the requests handled counter vector by one for the 65 // given label. 66 func RequestHandled(label RequestHandledLabel) { 67 metrics.CounterVecInc(requestsHandledName, string(label)) 68 } 69 70 // RequestDuration observes (histogram) the duration of a request from the 71 // provided starting time. 72 func RequestDuration(start time.Time) { 73 metrics.HistogramObserve(requestDurationName, time.Since(start).Seconds()) 74 }