-
Notifications
You must be signed in to change notification settings - Fork 178
/
transaction.go
333 lines (288 loc) · 11.2 KB
/
transaction.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
package metrics
import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/mempool"
)
type TransactionCollector struct {
transactionTimings mempool.TransactionTimings
log zerolog.Logger
logTimeToFinalized bool
logTimeToExecuted bool
logTimeToFinalizedExecuted bool
timeToFinalized prometheus.Summary
timeToExecuted prometheus.Summary
timeToFinalizedExecuted prometheus.Summary
transactionSubmission *prometheus.CounterVec
transactionSize prometheus.Histogram
scriptExecutedDuration *prometheus.HistogramVec
scriptExecutionErrorOnExecutor *prometheus.CounterVec
scriptExecutionComparison *prometheus.CounterVec
scriptSize prometheus.Histogram
transactionResultDuration *prometheus.HistogramVec
}
// interface check
var _ module.BackendScriptsMetrics = (*TransactionCollector)(nil)
var _ module.TransactionMetrics = (*TransactionCollector)(nil)
func NewTransactionCollector(
log zerolog.Logger,
transactionTimings mempool.TransactionTimings,
logTimeToFinalized bool,
logTimeToExecuted bool,
logTimeToFinalizedExecuted bool,
) *TransactionCollector {
tc := &TransactionCollector{
transactionTimings: transactionTimings,
log: log,
logTimeToFinalized: logTimeToFinalized,
logTimeToExecuted: logTimeToExecuted,
logTimeToFinalizedExecuted: logTimeToFinalizedExecuted,
timeToFinalized: promauto.NewSummary(prometheus.SummaryOpts{
Name: "time_to_finalized_seconds",
Namespace: namespaceAccess,
Subsystem: subsystemTransactionTiming,
Help: "the duration of how long it took between the transaction was received until it was finalized",
Objectives: map[float64]float64{
0.01: 0.001,
0.5: 0.05,
0.99: 0.001,
},
MaxAge: 10 * time.Minute,
AgeBuckets: 5,
BufCap: 500,
}),
timeToExecuted: promauto.NewSummary(prometheus.SummaryOpts{
Name: "time_to_executed_seconds",
Namespace: namespaceAccess,
Subsystem: subsystemTransactionTiming,
Help: "the duration of how long it took between the transaction was received until it was executed",
Objectives: map[float64]float64{
0.01: 0.001,
0.5: 0.05,
0.99: 0.001,
},
MaxAge: 10 * time.Minute,
AgeBuckets: 5,
BufCap: 500,
}),
timeToFinalizedExecuted: promauto.NewSummary(prometheus.SummaryOpts{
Name: "time_to_finalized_executed_seconds",
Namespace: namespaceAccess,
Subsystem: subsystemTransactionTiming,
Help: "the duration of how long it took between the transaction was received until it was both " +
"finalized and executed",
Objectives: map[float64]float64{
0.01: 0.001,
0.5: 0.05,
0.99: 0.001,
},
MaxAge: 10 * time.Minute,
AgeBuckets: 5,
BufCap: 500,
}),
transactionSubmission: promauto.NewCounterVec(prometheus.CounterOpts{
Name: "transaction_submission",
Namespace: namespaceAccess,
Subsystem: subsystemTransactionSubmission,
Help: "counter for the success/failure of transaction submissions",
}, []string{"result"}),
scriptExecutedDuration: promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "script_executed_duration",
Namespace: namespaceAccess,
Subsystem: subsystemTransactionSubmission,
Help: "histogram for the duration in ms of the round trip time for executing a script",
Buckets: []float64{1, 100, 500, 1000, 2000, 5000},
}, []string{"script_size"}),
scriptExecutionErrorOnExecutor: promauto.NewCounterVec(prometheus.CounterOpts{
Name: "script_execution_error_executor",
Namespace: namespaceAccess,
Subsystem: subsystemTransactionSubmission,
Help: "counter for the internal errors while executing a script",
}, []string{"source"}),
scriptExecutionComparison: promauto.NewCounterVec(prometheus.CounterOpts{
Name: "script_execution_comparison",
Namespace: namespaceAccess,
Subsystem: subsystemTransactionSubmission,
Help: "counter for the comparison outcomes of executing a script locally and on execution node",
}, []string{"outcome"}),
transactionResultDuration: promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "transaction_result_fetched_duration",
Namespace: namespaceAccess,
Subsystem: subsystemTransactionSubmission,
Help: "histogram for the duration in ms of the round trip time for getting a transaction result",
Buckets: []float64{1, 100, 500, 1000, 2000, 5000},
}, []string{"payload_size"}),
scriptSize: promauto.NewHistogram(prometheus.HistogramOpts{
Name: "script_size",
Namespace: namespaceAccess,
Subsystem: subsystemTransactionSubmission,
Help: "histogram for the script size in kb of scripts used in ExecuteScript",
}),
transactionSize: promauto.NewHistogram(prometheus.HistogramOpts{
Name: "transaction_size",
Namespace: namespaceAccess,
Subsystem: subsystemTransactionSubmission,
Help: "histogram for the transaction size in kb of scripts used in GetTransactionResult",
}),
}
return tc
}
// Script exec metrics
func (tc *TransactionCollector) ScriptExecuted(dur time.Duration, size int) {
// record the execute script duration and script size
tc.scriptSize.Observe(float64(size / 1024))
tc.scriptExecutedDuration.With(prometheus.Labels{
"script_size": tc.sizeLabel(size),
}).Observe(float64(dur.Milliseconds()))
}
func (tc *TransactionCollector) ScriptExecutionErrorLocal() {
// record the execution error count
tc.scriptExecutionErrorOnExecutor.WithLabelValues("local").Inc()
}
func (tc *TransactionCollector) ScriptExecutionErrorOnExecutionNode() {
// record the execution error count
tc.scriptExecutionErrorOnExecutor.WithLabelValues("execution").Inc()
}
func (tc *TransactionCollector) ScriptExecutionResultMismatch() {
// record the execution error count
tc.scriptExecutionComparison.WithLabelValues("result_mismatch").Inc()
}
func (tc *TransactionCollector) ScriptExecutionResultMatch() {
// record the execution error count
tc.scriptExecutionComparison.WithLabelValues("result_match").Inc()
}
func (tc *TransactionCollector) ScriptExecutionErrorMismatch() {
// record the execution error count
tc.scriptExecutionComparison.WithLabelValues("error_mismatch").Inc()
}
func (tc *TransactionCollector) ScriptExecutionErrorMatch() {
// record the execution error count
tc.scriptExecutionComparison.WithLabelValues("error_match").Inc()
}
// ScriptExecutionNotIndexed records script execution matches where data for the block is not
// indexed locally yet
func (tc *TransactionCollector) ScriptExecutionNotIndexed() {
tc.scriptExecutionComparison.WithLabelValues("not_indexed").Inc()
}
// TransactionResult metrics
func (tc *TransactionCollector) TransactionResultFetched(dur time.Duration, size int) {
// record the transaction result duration and transaction script/payload size
tc.transactionSize.Observe(float64(size / 1024))
tc.transactionResultDuration.With(prometheus.Labels{
"payload_size": tc.sizeLabel(size),
}).Observe(float64(dur.Milliseconds()))
}
func (tc *TransactionCollector) sizeLabel(size int) string {
// determine the script size label using the size in bytes
sizeKb := size / 1024
sizeLabel := "100+kb" //"1kb,10kb,100kb, 100+kb" -> [0,1] [2,10] [11,100] [100, +inf]
if sizeKb <= 1 {
sizeLabel = "1kb"
} else if sizeKb <= 10 {
sizeLabel = "10kb"
} else if sizeKb <= 100 {
sizeLabel = "100kb"
}
return sizeLabel
}
func (tc *TransactionCollector) TransactionReceived(txID flow.Identifier, when time.Time) {
// we don't need to check whether the transaction timing already exists, it will not be overwritten by the mempool
added := tc.transactionTimings.Add(&flow.TransactionTiming{TransactionID: txID, Received: when})
if !added {
tc.log.Warn().
Str("transaction_id", txID.String()).
Msg("failed to add TransactionReceived metric")
}
}
func (tc *TransactionCollector) TransactionFinalized(txID flow.Identifier, when time.Time) {
// Count as submitted as long as it's finalized
tc.transactionSubmission.WithLabelValues("success").Inc()
t, updated := tc.transactionTimings.Adjust(txID, func(t *flow.TransactionTiming) *flow.TransactionTiming {
t.Finalized = when
return t
})
// the AN may not have received the original transaction sent by the client in which case the finalized metric
// is not updated
if !updated {
tc.log.Debug().
Str("transaction_id", txID.String()).
Msg("failed to update TransactionFinalized metric")
return
}
tc.trackTTF(t, tc.logTimeToFinalized)
tc.trackTTFE(t, tc.logTimeToFinalizedExecuted)
// remove transaction timing from mempool if finalized and executed
if !t.Finalized.IsZero() && !t.Executed.IsZero() {
tc.transactionTimings.Remove(txID)
}
}
func (tc *TransactionCollector) TransactionExecuted(txID flow.Identifier, when time.Time) {
t, updated := tc.transactionTimings.Adjust(txID, func(t *flow.TransactionTiming) *flow.TransactionTiming {
t.Executed = when
return t
})
if !updated {
tc.log.Debug().
Str("transaction_id", txID.String()).
Msg("failed to update TransactionExecuted metric")
return
}
tc.trackTTE(t, tc.logTimeToExecuted)
tc.trackTTFE(t, tc.logTimeToFinalizedExecuted)
// remove transaction timing from mempool if finalized and executed
if !t.Finalized.IsZero() && !t.Executed.IsZero() {
tc.transactionTimings.Remove(txID)
}
}
func (tc *TransactionCollector) trackTTF(t *flow.TransactionTiming, log bool) {
if t.Received.IsZero() || t.Finalized.IsZero() {
return
}
duration := t.Finalized.Sub(t.Received).Seconds()
tc.timeToFinalized.Observe(duration)
if log {
tc.log.Info().Str("transaction_id", t.TransactionID.String()).Float64("duration", duration).
Msg("transaction time to finalized")
}
}
func (tc *TransactionCollector) trackTTE(t *flow.TransactionTiming, log bool) {
if t.Received.IsZero() || t.Executed.IsZero() {
return
}
duration := t.Executed.Sub(t.Received).Seconds()
tc.timeToExecuted.Observe(duration)
if log {
tc.log.Info().Str("transaction_id", t.TransactionID.String()).Float64("duration", duration).
Msg("transaction time to executed")
}
}
func (tc *TransactionCollector) trackTTFE(t *flow.TransactionTiming, log bool) {
if t.Received.IsZero() || t.Finalized.IsZero() || t.Executed.IsZero() {
return
}
duration := t.Finalized.Sub(t.Received).Seconds()
if t.Executed.After(t.Finalized) {
duration = t.Executed.Sub(t.Received).Seconds()
}
tc.timeToFinalizedExecuted.Observe(duration)
if log {
tc.log.Info().Str("transaction_id", t.TransactionID.String()).Float64("duration", duration).
Msg("transaction time to finalized and executed")
}
}
func (tc *TransactionCollector) TransactionSubmissionFailed() {
tc.transactionSubmission.WithLabelValues("failed").Inc()
}
func (tc *TransactionCollector) TransactionExpired(txID flow.Identifier) {
_, exist := tc.transactionTimings.ByID(txID)
if !exist {
// likely previously removed, either executed or expired
return
}
tc.transactionSubmission.WithLabelValues("expired").Inc()
tc.transactionTimings.Remove(txID)
}