@@ -14,6 +14,14 @@ import (
14
14
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
15
15
)
16
16
17
+ type queryType string
18
+
19
+ const (
20
+ create queryType = "create"
21
+ read queryType = "read"
22
+ del queryType = "delete"
23
+ )
24
+
17
25
var (
18
26
sqlLatencyBuckets = []float64 {
19
27
float64 (1 * time .Millisecond ),
@@ -41,47 +49,63 @@ var (
41
49
Name : "log_poller_query_duration" ,
42
50
Help : "Measures duration of Log Poller's queries fetching logs" ,
43
51
Buckets : sqlLatencyBuckets ,
44
- }, []string {"evmChainID" , "query" })
52
+ }, []string {"evmChainID" , "query" , "type" })
45
53
lpQueryDataSets = promauto .NewGaugeVec (prometheus.GaugeOpts {
46
54
Name : "log_poller_query_dataset_size" ,
47
55
Help : "Measures size of the datasets returned by Log Poller's queries" ,
48
- }, []string {"evmChainID" , "query" })
56
+ }, []string {"evmChainID" , "query" , "type" })
57
+ lpLogsInserted = promauto .NewCounterVec (prometheus.CounterOpts {
58
+ Name : "log_poller_logs_inserted" ,
59
+ Help : "Counter to track number of logs inserted by Log Poller" ,
60
+ }, []string {"evmChainID" })
61
+ lpBlockInserted = promauto .NewCounterVec (prometheus.CounterOpts {
62
+ Name : "log_poller_blocks_inserted" ,
63
+ Help : "Counter to track number of blocks inserted by Log Poller" ,
64
+ }, []string {"evmChainID" })
49
65
)
50
66
51
67
// ObservedORM is a decorator layer for ORM used by LogPoller, responsible for pushing Prometheus metrics reporting duration and size of result set for the queries.
52
68
// It doesn't change internal logic, because all calls are delegated to the origin ORM
53
69
type ObservedORM struct {
54
70
ORM
55
- queryDuration * prometheus.HistogramVec
56
- datasetSize * prometheus.GaugeVec
57
- chainId string
71
+ queryDuration * prometheus.HistogramVec
72
+ datasetSize * prometheus.GaugeVec
73
+ logsInserted * prometheus.CounterVec
74
+ blocksInserted * prometheus.CounterVec
75
+ chainId string
58
76
}
59
77
60
78
// NewObservedORM creates an observed version of log poller's ORM created by NewORM
61
79
// Please see ObservedLogPoller for more details on how latencies are measured
62
80
func NewObservedORM (chainID * big.Int , db * sqlx.DB , lggr logger.Logger , cfg pg.QConfig ) * ObservedORM {
63
81
return & ObservedORM {
64
- ORM : NewORM (chainID , db , lggr , cfg ),
65
- queryDuration : lpQueryDuration ,
66
- datasetSize : lpQueryDataSets ,
67
- chainId : chainID .String (),
82
+ ORM : NewORM (chainID , db , lggr , cfg ),
83
+ queryDuration : lpQueryDuration ,
84
+ datasetSize : lpQueryDataSets ,
85
+ logsInserted : lpLogsInserted ,
86
+ blocksInserted : lpBlockInserted ,
87
+ chainId : chainID .String (),
68
88
}
69
89
}
70
90
71
91
func (o * ObservedORM ) InsertLogs (logs []Log , qopts ... pg.QOpt ) error {
72
- return withObservedExec (o , "InsertLogs" , func () error {
92
+ err := withObservedExec (o , "InsertLogs" , create , func () error {
73
93
return o .ORM .InsertLogs (logs , qopts ... )
74
94
})
95
+ trackInsertedLogsAndBlock (o , logs , nil , err )
96
+ return err
75
97
}
76
98
77
99
func (o * ObservedORM ) InsertLogsWithBlock (logs []Log , block LogPollerBlock , qopts ... pg.QOpt ) error {
78
- return withObservedExec (o , "InsertLogsWithBlock" , func () error {
100
+ err := withObservedExec (o , "InsertLogsWithBlock" , create , func () error {
79
101
return o .ORM .InsertLogsWithBlock (logs , block , qopts ... )
80
102
})
103
+ trackInsertedLogsAndBlock (o , logs , & block , err )
104
+ return err
81
105
}
82
106
83
107
func (o * ObservedORM ) InsertFilter (filter Filter , qopts ... pg.QOpt ) error {
84
- return withObservedExec (o , "InsertFilter" , func () error {
108
+ return withObservedExec (o , "InsertFilter" , create , func () error {
85
109
return o .ORM .InsertFilter (filter , qopts ... )
86
110
})
87
111
}
@@ -93,25 +117,25 @@ func (o *ObservedORM) LoadFilters(qopts ...pg.QOpt) (map[string]Filter, error) {
93
117
}
94
118
95
119
func (o * ObservedORM ) DeleteFilter (name string , qopts ... pg.QOpt ) error {
96
- return withObservedExec (o , "DeleteFilter" , func () error {
120
+ return withObservedExec (o , "DeleteFilter" , del , func () error {
97
121
return o .ORM .DeleteFilter (name , qopts ... )
98
122
})
99
123
}
100
124
101
125
func (o * ObservedORM ) DeleteBlocksBefore (end int64 , qopts ... pg.QOpt ) error {
102
- return withObservedExec (o , "DeleteBlocksBefore" , func () error {
126
+ return withObservedExec (o , "DeleteBlocksBefore" , del , func () error {
103
127
return o .ORM .DeleteBlocksBefore (end , qopts ... )
104
128
})
105
129
}
106
130
107
131
func (o * ObservedORM ) DeleteLogsAndBlocksAfter (start int64 , qopts ... pg.QOpt ) error {
108
- return withObservedExec (o , "DeleteLogsAndBlocksAfter" , func () error {
132
+ return withObservedExec (o , "DeleteLogsAndBlocksAfter" , del , func () error {
109
133
return o .ORM .DeleteLogsAndBlocksAfter (start , qopts ... )
110
134
})
111
135
}
112
136
113
137
func (o * ObservedORM ) DeleteExpiredLogs (qopts ... pg.QOpt ) error {
114
- return withObservedExec (o , "DeleteExpiredLogs" , func () error {
138
+ return withObservedExec (o , "DeleteExpiredLogs" , del , func () error {
115
139
return o .ORM .DeleteExpiredLogs (qopts ... )
116
140
})
117
141
}
@@ -176,9 +200,9 @@ func (o *ObservedORM) SelectLogs(start, end int64, address common.Address, event
176
200
})
177
201
}
178
202
179
- func (o * ObservedORM ) IndexedLogsByTxHash ( eventSig common.Hash , txHash common.Hash , qopts ... pg.QOpt ) ([]Log , error ) {
180
- return withObservedQueryAndResults (o , "IndexedLogsByTxHash " , func () ([]Log , error ) {
181
- return o .ORM .SelectIndexedLogsByTxHash (eventSig , txHash , qopts ... )
203
+ func (o * ObservedORM ) SelectIndexedLogsByTxHash ( address common. Address , eventSig common.Hash , txHash common.Hash , qopts ... pg.QOpt ) ([]Log , error ) {
204
+ return withObservedQueryAndResults (o , "SelectIndexedLogsByTxHash " , func () ([]Log , error ) {
205
+ return o .ORM .SelectIndexedLogsByTxHash (address , eventSig , txHash , qopts ... )
182
206
})
183
207
}
184
208
@@ -212,6 +236,12 @@ func (o *ObservedORM) SelectLogsDataWordGreaterThan(address common.Address, even
212
236
})
213
237
}
214
238
239
+ func (o * ObservedORM ) SelectLogsDataWordBetween (address common.Address , eventSig common.Hash , wordIndexMin int , wordIndexMax int , wordValue common.Hash , confs Confirmations , qopts ... pg.QOpt ) ([]Log , error ) {
240
+ return withObservedQueryAndResults (o , "SelectLogsDataWordBetween" , func () ([]Log , error ) {
241
+ return o .ORM .SelectLogsDataWordBetween (address , eventSig , wordIndexMin , wordIndexMax , wordValue , confs , qopts ... )
242
+ })
243
+ }
244
+
215
245
func (o * ObservedORM ) SelectIndexedLogsTopicGreaterThan (address common.Address , eventSig common.Hash , topicIndex int , topicValueMin common.Hash , confs Confirmations , qopts ... pg.QOpt ) ([]Log , error ) {
216
246
return withObservedQueryAndResults (o , "SelectIndexedLogsTopicGreaterThan" , func () ([]Log , error ) {
217
247
return o .ORM .SelectIndexedLogsTopicGreaterThan (address , eventSig , topicIndex , topicValueMin , confs , qopts ... )
@@ -228,7 +258,7 @@ func withObservedQueryAndResults[T any](o *ObservedORM, queryName string, query
228
258
results , err := withObservedQuery (o , queryName , query )
229
259
if err == nil {
230
260
o .datasetSize .
231
- WithLabelValues (o .chainId , queryName ).
261
+ WithLabelValues (o .chainId , queryName , string ( read ) ).
232
262
Set (float64 (len (results )))
233
263
}
234
264
return results , err
@@ -238,18 +268,33 @@ func withObservedQuery[T any](o *ObservedORM, queryName string, query func() (T,
238
268
queryStarted := time .Now ()
239
269
defer func () {
240
270
o .queryDuration .
241
- WithLabelValues (o .chainId , queryName ).
271
+ WithLabelValues (o .chainId , queryName , string ( read ) ).
242
272
Observe (float64 (time .Since (queryStarted )))
243
273
}()
244
274
return query ()
245
275
}
246
276
247
- func withObservedExec (o * ObservedORM , query string , exec func () error ) error {
277
+ func withObservedExec (o * ObservedORM , query string , queryType queryType , exec func () error ) error {
248
278
queryStarted := time .Now ()
249
279
defer func () {
250
280
o .queryDuration .
251
- WithLabelValues (o .chainId , query ).
281
+ WithLabelValues (o .chainId , query , string ( queryType ) ).
252
282
Observe (float64 (time .Since (queryStarted )))
253
283
}()
254
284
return exec ()
255
285
}
286
+
287
+ func trackInsertedLogsAndBlock (o * ObservedORM , logs []Log , block * LogPollerBlock , err error ) {
288
+ if err != nil {
289
+ return
290
+ }
291
+ o .logsInserted .
292
+ WithLabelValues (o .chainId ).
293
+ Add (float64 (len (logs )))
294
+
295
+ if block != nil {
296
+ o .blocksInserted .
297
+ WithLabelValues (o .chainId ).
298
+ Inc ()
299
+ }
300
+ }
0 commit comments