8
8
"fmt"
9
9
"math/big"
10
10
"sync"
11
- "sync/atomic"
12
11
"time"
13
12
14
13
"github.com/ethereum/go-ethereum/common"
@@ -18,6 +17,7 @@ import (
18
17
"github.com/ethereum/go-ethereum/eth/protocols/eth"
19
18
ethp2p "github.com/ethereum/go-ethereum/p2p"
20
19
"github.com/ethereum/go-ethereum/p2p/enode"
20
+ "github.com/prometheus/client_golang/prometheus"
21
21
"github.com/rs/zerolog"
22
22
"github.com/rs/zerolog/log"
23
23
@@ -33,7 +33,7 @@ type conn struct {
33
33
db database.Database
34
34
head * HeadBlock
35
35
headMutex * sync.RWMutex
36
- count * MessageCount
36
+ counter * prometheus. CounterVec
37
37
38
38
// requests is used to store the request ID and the block hash. This is used
39
39
// when fetching block bodies because the eth protocol block bodies do not
@@ -56,8 +56,8 @@ type EthProtocolOptions struct {
56
56
SensorID string
57
57
NetworkID uint64
58
58
Peers chan * enode.Node
59
- Count * MessageCount
60
59
ForkID forkid.ID
60
+ MsgCounter * prometheus.CounterVec
61
61
62
62
// Head keeps track of the current head block of the chain. This is required
63
63
// when doing the status exchange.
@@ -91,7 +91,7 @@ func NewEthProtocol(version uint, opts EthProtocolOptions) ethp2p.Protocol {
91
91
requestNum : 0 ,
92
92
head : opts .Head ,
93
93
headMutex : opts .HeadMutex ,
94
- count : opts .Count ,
94
+ counter : opts .MsgCounter ,
95
95
}
96
96
97
97
c .headMutex .RLock ()
@@ -296,7 +296,7 @@ func (c *conn) handleNewBlockHashes(ctx context.Context, msg ethp2p.Msg) error {
296
296
return err
297
297
}
298
298
299
- atomic . AddInt32 ( & c . count . BlockHashes , int32 (len (packet )))
299
+ c . counter . WithLabelValues ( fmt . Sprint ( msg . Code ), packet . Name ()). Add ( float64 (len (packet )))
300
300
301
301
hashes := make ([]common.Hash , 0 , len (packet ))
302
302
for _ , hash := range packet {
@@ -317,7 +317,7 @@ func (c *conn) handleTransactions(ctx context.Context, msg ethp2p.Msg) error {
317
317
return err
318
318
}
319
319
320
- atomic . AddInt32 ( & c . count . Transactions , int32 (len (txs )))
320
+ c . counter . WithLabelValues ( fmt . Sprint ( msg . Code ), txs . Name ()). Add ( float64 (len (txs )))
321
321
322
322
c .db .WriteTransactions (ctx , c .node , txs )
323
323
@@ -330,7 +330,7 @@ func (c *conn) handleGetBlockHeaders(msg ethp2p.Msg) error {
330
330
return err
331
331
}
332
332
333
- atomic . AddInt32 ( & c . count . BlockHeaderRequests , 1 )
333
+ c . counter . WithLabelValues ( fmt . Sprint ( msg . Code ), request . Name ()). Inc ( )
334
334
335
335
return ethp2p .Send (
336
336
c .rw ,
@@ -346,7 +346,7 @@ func (c *conn) handleBlockHeaders(ctx context.Context, msg ethp2p.Msg) error {
346
346
}
347
347
348
348
headers := packet .BlockHeadersRequest
349
- atomic . AddInt32 ( & c . count . BlockHeaders , int32 (len (headers )))
349
+ c . counter . WithLabelValues ( fmt . Sprint ( msg . Code ), packet . Name ()). Add ( float64 (len (headers )))
350
350
351
351
for _ , header := range headers {
352
352
if err := c .getParentBlock (ctx , header ); err != nil {
@@ -365,7 +365,7 @@ func (c *conn) handleGetBlockBodies(msg ethp2p.Msg) error {
365
365
return err
366
366
}
367
367
368
- atomic . AddInt32 ( & c . count . BlockBodiesRequests , int32 (len (request .GetBlockBodiesRequest )))
368
+ c . counter . WithLabelValues ( fmt . Sprint ( msg . Code ), request . Name ()). Add ( float64 (len (request .GetBlockBodiesRequest )))
369
369
370
370
return ethp2p .Send (
371
371
c .rw ,
@@ -384,7 +384,7 @@ func (c *conn) handleBlockBodies(ctx context.Context, msg ethp2p.Msg) error {
384
384
return nil
385
385
}
386
386
387
- atomic . AddInt32 ( & c . count . BlockBodies , int32 (len (packet .BlockBodiesResponse )))
387
+ c . counter . WithLabelValues ( fmt . Sprint ( msg . Code ), packet . Name ()). Add ( float64 (len (packet .BlockBodiesResponse )))
388
388
389
389
var hash * common.Hash
390
390
for e := c .requests .Front (); e != nil ; e = e .Next () {
@@ -413,7 +413,7 @@ func (c *conn) handleNewBlock(ctx context.Context, msg ethp2p.Msg) error {
413
413
return err
414
414
}
415
415
416
- atomic . AddInt32 ( & c . count . Blocks , 1 )
416
+ c . counter . WithLabelValues ( fmt . Sprint ( msg . Code ), block . Name ()). Inc ( )
417
417
418
418
// Set the head block if newer.
419
419
c .headMutex .Lock ()
@@ -444,7 +444,7 @@ func (c *conn) handleGetPooledTransactions(msg ethp2p.Msg) error {
444
444
return err
445
445
}
446
446
447
- atomic . AddInt32 ( & c . count . TransactionRequests , int32 (len (request .GetPooledTransactionsRequest )))
447
+ c . counter . WithLabelValues ( fmt . Sprint ( msg . Code ), request . Name ()). Add ( float64 (len (request .GetPooledTransactionsRequest )))
448
448
449
449
return ethp2p .Send (
450
450
c .rw ,
@@ -454,6 +454,7 @@ func (c *conn) handleGetPooledTransactions(msg ethp2p.Msg) error {
454
454
455
455
func (c * conn ) handleNewPooledTransactionHashes (ctx context.Context , version uint , msg ethp2p.Msg ) error {
456
456
var hashes []common.Hash
457
+ var name string
457
458
458
459
switch version {
459
460
case 66 , 67 :
@@ -462,17 +463,19 @@ func (c *conn) handleNewPooledTransactionHashes(ctx context.Context, version uin
462
463
return err
463
464
}
464
465
hashes = txs
466
+ name = txs .Name ()
465
467
case 68 :
466
468
var txs eth.NewPooledTransactionHashesPacket68
467
469
if err := msg .Decode (& txs ); err != nil {
468
470
return err
469
471
}
470
472
hashes = txs .Hashes
473
+ name = txs .Name ()
471
474
default :
472
475
return errors .New ("protocol version not found" )
473
476
}
474
477
475
- atomic . AddInt32 ( & c . count . TransactionHashes , int32 (len (hashes )))
478
+ c . counter . WithLabelValues ( fmt . Sprint ( msg . Code ), name ). Add ( float64 (len (hashes )))
476
479
477
480
if ! c .db .ShouldWriteTransactions () || ! c .db .ShouldWriteTransactionEvents () {
478
481
return nil
@@ -491,7 +494,7 @@ func (c *conn) handlePooledTransactions(ctx context.Context, msg ethp2p.Msg) err
491
494
return err
492
495
}
493
496
494
- atomic . AddInt32 ( & c . count . Transactions , int32 (len (packet .PooledTransactionsResponse )))
497
+ c . counter . WithLabelValues ( fmt . Sprint ( msg . Code ), packet . Name ()). Add ( float64 (len (packet .PooledTransactionsResponse )))
495
498
496
499
c .db .WriteTransactions (ctx , c .node , packet .PooledTransactionsResponse )
497
500
0 commit comments