-
Notifications
You must be signed in to change notification settings - Fork 53
/
instrumented_client.go
466 lines (425 loc) · 14.4 KB
/
instrumented_client.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package eth
import (
"context"
"math/big"
"time"
rpccalls "github.com/Layr-Labs/eigensdk-go/metrics/collectors/rpc_calls"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
// InstrumentedClient is a wrapper around the geth ethclient that instruments
// all the calls made to it. It counts each eth_ call made to it, and records the duration of each call,
// and exposes these as prometheus metrics
//
// TODO: ideally this should be done at the rpcclient level, not the ethclient level, which would make
// our life much easier... but geth implemented the gethclient using an rpcClient struct instead of interface...
// see https://github.com/ethereum/go-ethereum/issues/28267
type InstrumentedClient struct {
client *ethclient.Client
rpcCallsCollector *rpccalls.Collector
// we store both client and version because that's what the web3_clientVersion jsonrpc call returns
// https://ethereum.org/en/developers/docs/apis/json-rpc/#web3_clientversion
clientAndVersion string
}
var _ HttpBackend = (*InstrumentedClient)(nil)
var _ WsBackend = (*InstrumentedClient)(nil)
func NewInstrumentedClient(rpcAddress string, rpcCallsCollector *rpccalls.Collector) (*InstrumentedClient, error) {
client, err := ethclient.Dial(rpcAddress)
if err != nil {
return nil, err
}
return NewInstrumentedClientFromClient(client, rpcCallsCollector), nil
}
func NewInstrumentedClientFromClient(
client *ethclient.Client,
rpcCallsCollector *rpccalls.Collector,
) *InstrumentedClient {
clientAndVersion := getClientAndVersion(client)
return &InstrumentedClient{
client: client,
rpcCallsCollector: rpcCallsCollector,
clientAndVersion: clientAndVersion,
}
}
// gethClient interface methods
func (iec *InstrumentedClient) ChainID(ctx context.Context) (*big.Int, error) {
chainID := func() (*big.Int, error) { return iec.client.ChainID(ctx) }
id, err := instrumentFunction[*big.Int](chainID, "eth_chainId", iec)
return id, err
}
func (iec *InstrumentedClient) BalanceAt(
ctx context.Context,
account common.Address,
blockNumber *big.Int,
) (*big.Int, error) {
balanceAt := func() (*big.Int, error) { return iec.client.BalanceAt(ctx, account, blockNumber) }
balance, err := instrumentFunction[*big.Int](balanceAt, "eth_getBalance", iec)
if err != nil {
return nil, err
}
return balance, nil
}
func (iec *InstrumentedClient) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
blockByHash := func() (*types.Block, error) { return iec.client.BlockByHash(ctx, hash) }
block, err := instrumentFunction[*types.Block](blockByHash, "eth_getBlockByHash", iec)
if err != nil {
return nil, err
}
return block, nil
}
func (iec *InstrumentedClient) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
blockByNumber := func() (*types.Block, error) { return iec.client.BlockByNumber(ctx, number) }
block, err := instrumentFunction[*types.Block](
blockByNumber,
"eth_getBlockByNumber",
iec,
)
if err != nil {
return nil, err
}
return block, nil
}
func (iec *InstrumentedClient) BlockNumber(ctx context.Context) (uint64, error) {
blockNumber := func() (uint64, error) { return iec.client.BlockNumber(ctx) }
number, err := instrumentFunction[uint64](blockNumber, "eth_blockNumber", iec)
if err != nil {
return 0, err
}
return number, nil
}
func (iec *InstrumentedClient) CallContract(
ctx context.Context,
call ethereum.CallMsg,
blockNumber *big.Int,
) ([]byte, error) {
callContract := func() ([]byte, error) { return iec.client.CallContract(ctx, call, blockNumber) }
bytes, err := instrumentFunction[[]byte](callContract, "eth_call", iec)
if err != nil {
return nil, err
}
return bytes, nil
}
func (iec *InstrumentedClient) CodeAt(
ctx context.Context,
contract common.Address,
blockNumber *big.Int,
) ([]byte, error) {
call := func() ([]byte, error) { return iec.client.CodeAt(ctx, contract, blockNumber) }
bytes, err := instrumentFunction[[]byte](call, "eth_getCode", iec)
if err != nil {
return nil, err
}
return bytes, nil
}
func (iec *InstrumentedClient) EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error) {
estimateGas := func() (uint64, error) { return iec.client.EstimateGas(ctx, call) }
gas, err := instrumentFunction[uint64](estimateGas, "eth_estimateGas", iec)
if err != nil {
return 0, err
}
return gas, nil
}
func (iec *InstrumentedClient) FeeHistory(
ctx context.Context,
blockCount uint64,
lastBlock *big.Int,
rewardPercentiles []float64,
) (*ethereum.FeeHistory, error) {
feeHistory := func() (*ethereum.FeeHistory, error) {
return iec.client.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
}
history, err := instrumentFunction[*ethereum.FeeHistory](
feeHistory,
"eth_feeHistory",
iec,
)
if err != nil {
return nil, err
}
return history, nil
}
func (iec *InstrumentedClient) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) {
filterLogs := func() ([]types.Log, error) { return iec.client.FilterLogs(ctx, query) }
logs, err := instrumentFunction[[]types.Log](filterLogs, "eth_getLogs", iec)
if err != nil {
return nil, err
}
return logs, nil
}
func (iec *InstrumentedClient) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
headerByHash := func() (*types.Header, error) { return iec.client.HeaderByHash(ctx, hash) }
header, err := instrumentFunction[*types.Header](
headerByHash,
"eth_getBlockByHash",
iec,
)
if err != nil {
return nil, err
}
return header, nil
}
func (iec *InstrumentedClient) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
headerByNumber := func() (*types.Header, error) { return iec.client.HeaderByNumber(ctx, number) }
header, err := instrumentFunction[*types.Header](
headerByNumber,
"eth_getBlockByNumber",
iec,
)
if err != nil {
return nil, err
}
return header, nil
}
func (iec *InstrumentedClient) NonceAt(
ctx context.Context,
account common.Address,
blockNumber *big.Int,
) (uint64, error) {
nonceAt := func() (uint64, error) { return iec.client.NonceAt(ctx, account, blockNumber) }
nonce, err := instrumentFunction[uint64](nonceAt, "eth_getTransactionCount", iec)
if err != nil {
return 0, err
}
return nonce, nil
}
func (iec *InstrumentedClient) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) {
pendingBalanceAt := func() (*big.Int, error) { return iec.client.PendingBalanceAt(ctx, account) }
balance, err := instrumentFunction[*big.Int](pendingBalanceAt, "eth_getBalance", iec)
if err != nil {
return nil, err
}
return balance, nil
}
func (iec *InstrumentedClient) PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) {
pendingCallContract := func() ([]byte, error) { return iec.client.PendingCallContract(ctx, call) }
bytes, err := instrumentFunction[[]byte](pendingCallContract, "eth_call", iec)
if err != nil {
return nil, err
}
return bytes, nil
}
func (iec *InstrumentedClient) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
pendingCodeAt := func() ([]byte, error) { return iec.client.PendingCodeAt(ctx, account) }
bytes, err := instrumentFunction[[]byte](pendingCodeAt, "eth_getCode", iec)
if err != nil {
return nil, err
}
return bytes, nil
}
func (iec *InstrumentedClient) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
pendingNonceAt := func() (uint64, error) { return iec.client.PendingNonceAt(ctx, account) }
nonce, err := instrumentFunction[uint64](
pendingNonceAt,
"eth_getTransactionCount",
iec,
)
if err != nil {
return 0, err
}
return nonce, nil
}
func (iec *InstrumentedClient) PendingStorageAt(
ctx context.Context,
account common.Address,
key common.Hash,
) ([]byte, error) {
pendingStorageAt := func() ([]byte, error) { return iec.client.PendingStorageAt(ctx, account, key) }
bytes, err := instrumentFunction[[]byte](pendingStorageAt, "eth_getStorageAt", iec)
if err != nil {
return nil, err
}
return bytes, nil
}
func (iec *InstrumentedClient) PendingTransactionCount(ctx context.Context) (uint, error) {
pendingTransactionCount := func() (uint, error) { return iec.client.PendingTransactionCount(ctx) }
count, err := instrumentFunction[uint](
pendingTransactionCount,
"eth_getBlockTransactionCountByNumber",
iec,
)
if err != nil {
return 0, err
}
return count, nil
}
func (iec *InstrumentedClient) SendTransaction(ctx context.Context, tx *types.Transaction) error {
// instrumentFunction takes a function that returns a value and an error
// so we just wrap the SendTransaction method in a function that returns 0 as its value,
// which we throw out below
sendTransaction := func() (int, error) { return 0, iec.client.SendTransaction(ctx, tx) }
_, err := instrumentFunction[int](sendTransaction, "eth_sendRawTransaction", iec)
return err
}
func (iec *InstrumentedClient) StorageAt(
ctx context.Context,
account common.Address,
key common.Hash,
blockNumber *big.Int,
) ([]byte, error) {
storageAt := func() ([]byte, error) { return iec.client.StorageAt(ctx, account, key, blockNumber) }
bytes, err := instrumentFunction[[]byte](storageAt, "eth_getStorageAt", iec)
if err != nil {
return nil, err
}
return bytes, nil
}
func (iec *InstrumentedClient) SubscribeFilterLogs(
ctx context.Context,
query ethereum.FilterQuery,
ch chan<- types.Log,
) (ethereum.Subscription, error) {
subscribeFilterLogs := func() (ethereum.Subscription, error) { return iec.client.SubscribeFilterLogs(ctx, query, ch) }
subscription, err := instrumentFunction[ethereum.Subscription](
subscribeFilterLogs,
"eth_subscribe",
iec,
)
if err != nil {
return nil, err
}
return subscription, nil
}
func (iec *InstrumentedClient) SubscribeNewHead(
ctx context.Context,
ch chan<- *types.Header,
) (ethereum.Subscription, error) {
subscribeNewHead := func() (ethereum.Subscription, error) { return iec.client.SubscribeNewHead(ctx, ch) }
subscription, err := instrumentFunction[ethereum.Subscription](
subscribeNewHead,
"eth_subscribe",
iec,
)
if err != nil {
return nil, err
}
return subscription, nil
}
func (iec *InstrumentedClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
suggestGasPrice := func() (*big.Int, error) { return iec.client.SuggestGasPrice(ctx) }
gasPrice, err := instrumentFunction[*big.Int](suggestGasPrice, "eth_gasPrice", iec)
if err != nil {
return nil, err
}
return gasPrice, nil
}
func (iec *InstrumentedClient) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
suggestGasTipCap := func() (*big.Int, error) { return iec.client.SuggestGasTipCap(ctx) }
gasTipCap, err := instrumentFunction[*big.Int](
suggestGasTipCap,
"eth_maxPriorityFeePerGas",
iec,
)
if err != nil {
return nil, err
}
return gasTipCap, nil
}
func (iec *InstrumentedClient) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) {
syncProgress := func() (*ethereum.SyncProgress, error) { return iec.client.SyncProgress(ctx) }
progress, err := instrumentFunction[*ethereum.SyncProgress](
syncProgress,
"eth_syncing",
iec,
)
if err != nil {
return nil, err
}
return progress, nil
}
// We write the instrumentation of this function directly because instrumentFunction[] generic fct only takes a single
// return value
func (iec *InstrumentedClient) TransactionByHash(
ctx context.Context,
hash common.Hash,
) (tx *types.Transaction, isPending bool, err error) {
start := time.Now()
tx, isPending, err = iec.client.TransactionByHash(ctx, hash)
// we count both successful and erroring calls (even though this is not well defined in the spec)
iec.rpcCallsCollector.AddRPCRequestTotal("eth_getTransactionByHash", iec.clientAndVersion)
if err != nil {
return nil, false, err
}
rpcRequestDuration := time.Since(start).Seconds()
// we only observe the duration of successful calls (even though this is not well defined in the spec)
iec.rpcCallsCollector.ObserveRPCRequestDurationSeconds(
float64(rpcRequestDuration),
"eth_getTransactionByHash",
iec.clientAndVersion,
)
return tx, isPending, nil
}
func (iec *InstrumentedClient) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) {
transactionCount := func() (uint, error) { return iec.client.TransactionCount(ctx, blockHash) }
count, err := instrumentFunction[uint](
transactionCount,
"eth_getBlockTransactionCountByHash",
iec,
)
if err != nil {
return 0, err
}
return count, nil
}
func (iec *InstrumentedClient) TransactionInBlock(
ctx context.Context,
blockHash common.Hash,
index uint,
) (*types.Transaction, error) {
transactionInBlock := func() (*types.Transaction, error) { return iec.client.TransactionInBlock(ctx, blockHash, index) }
tx, err := instrumentFunction[*types.Transaction](
transactionInBlock,
"eth_getTransactionByBlockHashAndIndex",
iec,
)
if err != nil {
return nil, err
}
return tx, nil
}
func (iec *InstrumentedClient) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
transactionReceipt := func() (*types.Receipt, error) { return iec.client.TransactionReceipt(ctx, txHash) }
receipt, err := instrumentFunction[*types.Receipt](
transactionReceipt,
"eth_getTransactionReceipt",
iec,
)
if err != nil {
return nil, err
}
return receipt, nil
}
// Not sure why this method is not exposed in the ethclient itself...
// but it is needed to comply with the rpc metrics defined in avs-node spec
// https://docs.eigenlayer.xyz/eigenlayer/avs-guides/spec/metrics/metrics-prom-spec
func getClientAndVersion(client *ethclient.Client) string {
var clientVersion string
err := client.Client().Call(&clientVersion, "web3_clientVersion")
if err != nil {
return "unavailable"
}
return clientVersion
}
// Generic function used to instrument all the eth calls that we make below
func instrumentFunction[T any](
rpcCall func() (T, error),
rpcMethodName string,
iec *InstrumentedClient,
) (value T, err error) {
start := time.Now()
result, err := rpcCall()
// we count both successful and erroring calls (even though this is not well defined in the spec)
iec.rpcCallsCollector.AddRPCRequestTotal(rpcMethodName, iec.clientAndVersion)
if err != nil {
return value, err
}
rpcRequestDuration := time.Since(start).Seconds()
// we only observe the duration of successful calls (even though this is not well defined in the spec)
iec.rpcCallsCollector.ObserveRPCRequestDurationSeconds(
float64(rpcRequestDuration),
rpcMethodName,
iec.clientAndVersion,
)
return result, nil
}