-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
plugin.go
162 lines (144 loc) · 5.14 KB
/
plugin.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package promwrapper
import (
"context"
"math/big"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/smartcontractkit/libocr/offchainreporting2/types"
)
var (
_ types.ReportingPlugin = &promPlugin{}
buckets = []float64{
float64(1 * time.Millisecond),
float64(5 * time.Millisecond),
float64(10 * time.Millisecond),
float64(50 * time.Millisecond),
float64(100 * time.Millisecond),
float64(500 * time.Millisecond),
float64(time.Second),
float64(5 * time.Second),
float64(10 * time.Second),
float64(30 * time.Second),
float64(time.Minute),
float64(2 * time.Minute),
float64(5 * time.Minute),
float64(10 * time.Minute),
}
labels = []string{"chainType", "chainID", "plugin"}
)
var (
promQuery = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ocr2_reporting_plugin_query_time",
Help: "The amount of time elapsed during the OCR2 plugin's Query() method",
Buckets: buckets,
},
labels,
)
promObservation = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ocr2_reporting_plugin_observation_time",
Help: "The amount of time elapsed during the OCR2 plugin's Observation() method",
Buckets: buckets,
},
labels,
)
promReport = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ocr2_reporting_plugin_report_time",
Help: "The amount of time elapsed during the OCR2 plugin's Report() method",
Buckets: buckets,
},
labels,
)
promShouldAcceptFinalizedReport = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ocr2_reporting_plugin_should_accept_finalized_report_time",
Help: "The amount of time elapsed during the OCR2 plugin's ShouldAcceptFinalizedReport() method",
Buckets: buckets,
},
labels,
)
promShouldTransmitAcceptedReport = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ocr2_reporting_plugin_should_transmit_accepted_report_time",
Help: "The amount of time elapsed during the OCR2 plugin's ShouldTransmitAcceptedReport() method",
Buckets: buckets,
},
labels,
)
promClose = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ocr2_reporting_plugin_close_time",
Help: "The amount of time elapsed during the OCR2 plugin's Close() method",
Buckets: buckets,
},
labels,
)
)
// promPlugin wraps another OCR2 reporting plugin and provides standardized prometheus metrics
// for each of the OCR2 phases (Query, Observation, Report, ShouldAcceptFinalizedReport,
// ShouldTransmitAcceptedReport, and Close).
type promPlugin struct {
wrapped types.ReportingPlugin
name string
chainType string
chainID *big.Int
}
func New(plugin types.ReportingPlugin, name string, chainType string, chainID *big.Int) types.ReportingPlugin {
return &promPlugin{
wrapped: plugin,
name: name,
chainType: chainType,
chainID: chainID,
}
}
func (p *promPlugin) Query(ctx context.Context, timestamp types.ReportTimestamp) (types.Query, error) {
start := time.Now().UTC()
defer func() {
duration := float64(time.Now().UTC().Sub(start))
promQuery.WithLabelValues(string(p.chainType), p.chainID.String(), p.name).Observe(duration)
}()
return p.wrapped.Query(ctx, timestamp)
}
func (p *promPlugin) Observation(ctx context.Context, timestamp types.ReportTimestamp, query types.Query) (types.Observation, error) {
start := time.Now().UTC()
defer func() {
duration := float64(time.Now().UTC().Sub(start))
promObservation.WithLabelValues(string(p.chainType), p.chainID.String(), p.name).Observe(duration)
}()
return p.wrapped.Observation(ctx, timestamp, query)
}
func (p *promPlugin) Report(ctx context.Context, timestamp types.ReportTimestamp, query types.Query, observations []types.AttributedObservation) (bool, types.Report, error) {
start := time.Now().UTC()
defer func() {
duration := float64(time.Now().UTC().Sub(start))
promReport.WithLabelValues(string(p.chainType), p.chainID.String(), p.name).Observe(duration)
}()
return p.wrapped.Report(ctx, timestamp, query, observations)
}
func (p *promPlugin) ShouldAcceptFinalizedReport(ctx context.Context, timestamp types.ReportTimestamp, report types.Report) (bool, error) {
start := time.Now().UTC()
defer func() {
duration := float64(time.Now().UTC().Sub(start))
promShouldAcceptFinalizedReport.WithLabelValues(string(p.chainType), p.chainID.String(), p.name).Observe(duration)
}()
return p.wrapped.ShouldAcceptFinalizedReport(ctx, timestamp, report)
}
func (p *promPlugin) ShouldTransmitAcceptedReport(ctx context.Context, timestamp types.ReportTimestamp, report types.Report) (bool, error) {
start := time.Now().UTC()
defer func() {
duration := float64(time.Now().UTC().Sub(start))
promShouldTransmitAcceptedReport.WithLabelValues(string(p.chainType), p.chainID.String(), p.name).Observe(duration)
}()
return p.wrapped.ShouldTransmitAcceptedReport(ctx, timestamp, report)
}
func (p *promPlugin) Close() error {
start := time.Now().UTC()
defer func() {
duration := float64(time.Now().UTC().Sub(start))
promClose.WithLabelValues(string(p.chainType), p.chainID.String(), p.name).Observe(duration)
}()
return p.wrapped.Close()
}