-
Notifications
You must be signed in to change notification settings - Fork 179
/
transaction.go
209 lines (177 loc) · 6.36 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
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/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
}
func NewTransactionCollector(transactionTimings mempool.TransactionTimings, log zerolog.Logger,
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"}),
}
return tc
}
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.Rem(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.Rem(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.Rem(txID)
}