-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathplugin.go
361 lines (317 loc) · 13.6 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// promwrapper wraps another OCR2 reporting plugin and provides standardized prometheus metrics
// for each of the OCR2 phases (Query, Observation, Report, ShouldAcceptFinalizedReport,
// ShouldTransmitAcceptedReport, and Close).
package promwrapper
import (
"context"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/patrickmn/go-cache"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
)
const (
// defaultExpiration is the default expiration time for cache items.
defaultExpiration = 30 * time.Minute
// defaultCleanupInterval is the default interval for cache cleanup.
defaultCleanupInterval = 5 * time.Minute
)
// Type assertions, buckets and labels.
var (
_ types.ReportingPlugin = &promPlugin{}
_ PrometheusBackend = &defaultPrometheusBackend{}
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", "oracleID", "configDigest"}
getLabelsValues = func(p *promPlugin, t types.ReportTimestamp) []string {
return []string{
p.chainType, // chainType
p.chainID.String(), // chainID
p.name, // plugin
p.oracleID, // oracleID
common.Bytes2Hex(t.ConfigDigest[:]), // configDigest
}
}
)
// Prometheus queries.
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,
},
[]string{"chainType", "chainID", "plugin", "oracleID", "configDigest"},
)
promQueryToObservationLatency = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ocr2_reporting_plugin_end_query_to_begin_observation",
Help: "The amount of time elapsed after the OCR2 node's Query() method and before its Observation() method",
Buckets: buckets,
},
labels,
)
promObservationToReportLatency = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ocr2_reporting_plugin_end_observation_to_begin_report_time",
Help: "The amount of time elapsed after the OCR2 node's Observation() method and before its Report() method",
Buckets: buckets,
},
labels,
)
promReportToAcceptFinalizedReportLatency = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ocr2_reporting_plugin_end_report_to_begin_accept_finalized_report",
Help: "The amount of time elapsed after the OCR2 node's Report() method and before its ShouldAcceptFinalizedReport() method",
Buckets: buckets,
},
labels,
)
promAcceptFinalizedReportToTransmitAcceptedReportLatency = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ocr2_reporting_plugin_end_accept_finalized_report_to_begin_transmit_accepted_report",
Help: "The amount of time elapsed after the OCR2 node's ShouldAcceptFinalizedReport() method and before its ShouldTransmitAcceptedReport() method",
Buckets: buckets,
},
labels,
)
)
type (
// Contains interface for logging OCR telemetry.
PrometheusBackend interface {
// Intra-phase latency.
SetQueryDuration([]string, float64)
SetObservationDuration([]string, float64)
SetReportDuration([]string, float64)
SetShouldAcceptFinalizedReportDuration([]string, float64)
SetShouldTransmitAcceptedReportDuration([]string, float64)
SetCloseDuration([]string, float64)
// Inter-phase latency.
SetQueryToObservationLatency([]string, float64)
SetObservationToReportLatency([]string, float64)
SetReportToAcceptFinalizedReportLatency([]string, float64)
SetAcceptFinalizedReportToTransmitAcceptedReportLatency([]string, float64)
}
defaultPrometheusBackend struct{} // implements PrometheusBackend
// promPlugin consumes a report plugin and wraps its core functions e.g Report(), Observe()...
promPlugin struct {
wrapped types.ReportingPlugin
name string
chainType string
chainID *big.Int
oracleID string
configDigest string
queryEndTimes *cache.Cache
observationEndTimes *cache.Cache
reportEndTimes *cache.Cache
acceptFinalizedReportEndTimes *cache.Cache
prometheusBackend PrometheusBackend
}
)
func (*defaultPrometheusBackend) SetQueryDuration(labelValues []string, duration float64) {
promQuery.WithLabelValues(labelValues...).Observe(duration)
}
func (*defaultPrometheusBackend) SetObservationDuration(labelValues []string, duration float64) {
promObservation.WithLabelValues(labelValues...).Observe(duration)
}
func (*defaultPrometheusBackend) SetReportDuration(labelValues []string, duration float64) {
promReport.WithLabelValues(labelValues...).Observe(duration)
}
func (*defaultPrometheusBackend) SetShouldAcceptFinalizedReportDuration(labelValues []string, duration float64) {
promShouldAcceptFinalizedReport.WithLabelValues(labelValues...).Observe(duration)
}
func (*defaultPrometheusBackend) SetShouldTransmitAcceptedReportDuration(labelValues []string, duration float64) {
promShouldTransmitAcceptedReport.WithLabelValues(labelValues...).Observe(duration)
}
func (*defaultPrometheusBackend) SetCloseDuration(labelValues []string, duration float64) {
promClose.WithLabelValues(labelValues...).Observe(duration)
}
func (*defaultPrometheusBackend) SetQueryToObservationLatency(labelValues []string, latency float64) {
promQueryToObservationLatency.WithLabelValues(labelValues...).Observe(latency)
}
func (*defaultPrometheusBackend) SetObservationToReportLatency(labelValues []string, latency float64) {
promObservationToReportLatency.WithLabelValues(labelValues...).Observe(latency)
}
func (*defaultPrometheusBackend) SetReportToAcceptFinalizedReportLatency(labelValues []string, latency float64) {
promReportToAcceptFinalizedReportLatency.WithLabelValues(labelValues...).Observe(latency)
}
func (*defaultPrometheusBackend) SetAcceptFinalizedReportToTransmitAcceptedReportLatency(labelValues []string, latency float64) {
promAcceptFinalizedReportToTransmitAcceptedReportLatency.WithLabelValues(labelValues...).Observe(latency)
}
func New(
plugin types.ReportingPlugin,
name string,
chainType string,
chainID *big.Int,
config types.ReportingPluginConfig,
backend PrometheusBackend,
) types.ReportingPlugin {
// Apply passed-in Prometheus backend if one is given.
var prometheusBackend PrometheusBackend = &defaultPrometheusBackend{}
if backend != nil {
prometheusBackend = backend
}
return &promPlugin{
wrapped: plugin,
name: name,
chainType: chainType,
chainID: chainID,
oracleID: fmt.Sprintf("%d", config.OracleID),
configDigest: common.Bytes2Hex(config.ConfigDigest[:]),
prometheusBackend: prometheusBackend,
queryEndTimes: cache.New(defaultExpiration, defaultCleanupInterval),
observationEndTimes: cache.New(defaultExpiration, defaultCleanupInterval),
reportEndTimes: cache.New(defaultExpiration, defaultCleanupInterval),
acceptFinalizedReportEndTimes: cache.New(defaultExpiration, defaultCleanupInterval),
}
}
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))
p.prometheusBackend.SetQueryDuration(getLabelsValues(p, timestamp), duration)
p.setEndTime(timestamp, p.queryEndTimes) // note time at end of Query()
}()
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()
// Report latency between Query() and Observation().
labelValues := getLabelsValues(p, timestamp)
if queryEndTime, ok := p.queryEndTimes.Get(timestampToKey(timestamp)); ok {
latency := float64(start.Sub(queryEndTime.(time.Time)))
p.prometheusBackend.SetQueryToObservationLatency(labelValues, latency)
}
// Report latency for Observation() at end of call.
defer func() {
duration := float64(time.Now().UTC().Sub(start))
p.prometheusBackend.SetObservationDuration(labelValues, duration)
p.setEndTime(timestamp, p.observationEndTimes) // note time at end of Observe()
}()
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()
// Report latency between Observation() and Report().
labelValues := getLabelsValues(p, timestamp)
if observationEndTime, ok := p.observationEndTimes.Get(timestampToKey(timestamp)); ok {
latency := float64(start.Sub(observationEndTime.(time.Time)))
p.prometheusBackend.SetObservationToReportLatency(labelValues, latency)
}
// Report latency for Report() at end of call.
defer func() {
duration := float64(time.Now().UTC().Sub(start))
p.prometheusBackend.SetReportDuration(labelValues, duration)
p.setEndTime(timestamp, p.reportEndTimes) // note time at end of Report()
}()
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()
// Report latency between Report() and ShouldAcceptFinalizedReport().
labelValues := getLabelsValues(p, timestamp)
if reportEndTime, ok := p.reportEndTimes.Get(timestampToKey(timestamp)); ok {
latency := float64(start.Sub(reportEndTime.(time.Time)))
p.prometheusBackend.SetReportToAcceptFinalizedReportLatency(labelValues, latency)
}
// Report latency for ShouldAcceptFinalizedReport() at end of call.
defer func() {
duration := float64(time.Now().UTC().Sub(start))
p.prometheusBackend.SetShouldAcceptFinalizedReportDuration(labelValues, duration)
p.setEndTime(timestamp, p.acceptFinalizedReportEndTimes) // note time at end of ShouldAcceptFinalizedReport()
}()
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()
// Report latency between ShouldAcceptFinalizedReport() and ShouldTransmitAcceptedReport().
labelValues := getLabelsValues(p, timestamp)
if acceptFinalizedReportEndTime, ok := p.acceptFinalizedReportEndTimes.Get(timestampToKey(timestamp)); ok {
latency := float64(start.Sub(acceptFinalizedReportEndTime.(time.Time)))
p.prometheusBackend.SetAcceptFinalizedReportToTransmitAcceptedReportLatency(labelValues, latency)
}
defer func() {
duration := float64(time.Now().UTC().Sub(start))
p.prometheusBackend.SetShouldTransmitAcceptedReportDuration(labelValues, duration)
}()
return p.wrapped.ShouldTransmitAcceptedReport(ctx, timestamp, report)
}
// Note: the 'Close' method does not have access to a report timestamp, as it is not part of report generation.
func (p *promPlugin) Close() error {
start := time.Now().UTC()
defer func() {
duration := float64(time.Now().UTC().Sub(start))
labelValues := []string{
p.chainType, // chainType
p.chainID.String(), // chainID
p.name, // plugin
p.oracleID, // oracleID
p.configDigest, // configDigest
}
p.prometheusBackend.SetCloseDuration(labelValues, duration)
}()
return p.wrapped.Close()
}
func (p *promPlugin) setEndTime(timestamp types.ReportTimestamp, cache *cache.Cache) {
cache.SetDefault(timestampToKey(timestamp), time.Now().UTC())
}
func timestampToKey(timestamp types.ReportTimestamp) string {
return fmt.Sprintf("%x_%d_%d", timestamp.ConfigDigest[:], timestamp.Epoch, timestamp.Round)
}