@@ -183,6 +183,7 @@ func (c *conn) statusExchange(packet *eth.StatusPacket) error {
183183 errc := make (chan error , 2 )
184184
185185 go func () {
186+ c .countMsgSent ((& eth.StatusPacket {}).Name (), 1 )
186187 errc <- ethp2p .Send (c .rw , eth .StatusMsg , & packet )
187188 }()
188189
@@ -207,9 +208,21 @@ func (c *conn) statusExchange(packet *eth.StatusPacket) error {
207208 return nil
208209}
209210
210- // AddCount increments the prometheus counter for this connection with the given message name and count.
211- func (c * conn ) AddCount (messageName string , count float64 ) {
212- c .counter .WithLabelValues (messageName , c .node .URLv4 (), c .peer .Fullname ()).Add (count )
211+ // countMsg increments the prometheus counter for this connection with the given direction, message name, and count.
212+ func (c * conn ) countMsg (direction Direction , messageName string , count float64 ) {
213+ c .counter .WithLabelValues (messageName , c .node .URLv4 (), c .peer .Fullname (), string (direction )).Add (count )
214+ }
215+
216+ // countMsgReceived increments the prometheus counter for received messages.
217+ func (c * conn ) countMsgReceived (messageName string , count float64 ) {
218+ c .countMsg (MsgReceived , messageName , count )
219+ c .countMsg (MsgReceived , messageName + PacketSuffix , 1 )
220+ }
221+
222+ // countMsgSent increments the prometheus counter for sent messages.
223+ func (c * conn ) countMsgSent (messageName string , count float64 ) {
224+ c .countMsg (MsgSent , messageName , count )
225+ c .countMsg (MsgSent , messageName + PacketSuffix , 1 )
213226}
214227
215228func (c * conn ) readStatus (packet * eth.StatusPacket ) error {
@@ -260,6 +273,7 @@ func (c *conn) getBlockData(hash common.Hash) error {
260273 },
261274 }
262275
276+ c .countMsgSent (headersRequest .Name (), 1 )
263277 if err := ethp2p .Send (c .rw , eth .GetBlockHeadersMsg , headersRequest ); err != nil {
264278 return err
265279 }
@@ -284,6 +298,7 @@ func (c *conn) getBlockData(hash common.Hash) error {
284298 GetBlockBodiesRequest : []common.Hash {hash },
285299 }
286300
301+ c .countMsgSent (bodiesRequest .Name (), 1 )
287302 return ethp2p .Send (c .rw , eth .GetBlockBodiesMsg , bodiesRequest )
288303}
289304
@@ -320,7 +335,7 @@ func (c *conn) handleNewBlockHashes(ctx context.Context, msg ethp2p.Msg) error {
320335
321336 tfs := time .Now ()
322337
323- c .AddCount (packet .Name (), float64 (len (packet )))
338+ c .countMsgReceived (packet .Name (), float64 (len (packet )))
324339
325340 // Collect unique hashes for database write.
326341 uniqueHashes := make ([]common.Hash , 0 , len (packet ))
@@ -389,7 +404,7 @@ func (c *conn) handleTransactions(ctx context.Context, msg ethp2p.Msg) error {
389404
390405 tfs := time .Now ()
391406
392- c .AddCount (txs .Name (), float64 (len (txs )))
407+ c .countMsgReceived (txs .Name (), float64 (len (txs )))
393408
394409 c .db .WriteTransactions (ctx , c .node , txs , tfs )
395410
@@ -402,13 +417,11 @@ func (c *conn) handleGetBlockHeaders(msg ethp2p.Msg) error {
402417 return err
403418 }
404419
405- c .AddCount (request .Name (), 1 )
420+ c .countMsgReceived (request .Name (), 1 )
406421
407- return ethp2p .Send (
408- c .rw ,
409- eth .BlockHeadersMsg ,
410- & eth.BlockHeadersPacket {RequestId : request .RequestId },
411- )
422+ response := & eth.BlockHeadersPacket {RequestId : request .RequestId }
423+ c .countMsgSent (response .Name (), 0 )
424+ return ethp2p .Send (c .rw , eth .BlockHeadersMsg , response )
412425}
413426
414427func (c * conn ) handleBlockHeaders (ctx context.Context , msg ethp2p.Msg ) error {
@@ -420,7 +433,7 @@ func (c *conn) handleBlockHeaders(ctx context.Context, msg ethp2p.Msg) error {
420433 tfs := time .Now ()
421434
422435 headers := packet .BlockHeadersRequest
423- c .AddCount (packet .Name (), float64 (len (headers )))
436+ c .countMsgReceived (packet .Name (), float64 (len (headers )))
424437
425438 for _ , header := range headers {
426439 if err := c .getParentBlock (ctx , header ); err != nil {
@@ -438,13 +451,11 @@ func (c *conn) handleGetBlockBodies(msg ethp2p.Msg) error {
438451 return err
439452 }
440453
441- c .AddCount (request .Name (), float64 (len (request .GetBlockBodiesRequest )))
454+ c .countMsgReceived (request .Name (), float64 (len (request .GetBlockBodiesRequest )))
442455
443- return ethp2p .Send (
444- c .rw ,
445- eth .BlockBodiesMsg ,
446- & eth.BlockBodiesPacket {RequestId : request .RequestId },
447- )
456+ response := & eth.BlockBodiesPacket {RequestId : request .RequestId }
457+ c .countMsgSent (response .Name (), 0 )
458+ return ethp2p .Send (c .rw , eth .BlockBodiesMsg , response )
448459}
449460
450461func (c * conn ) handleBlockBodies (ctx context.Context , msg ethp2p.Msg ) error {
@@ -459,7 +470,7 @@ func (c *conn) handleBlockBodies(ctx context.Context, msg ethp2p.Msg) error {
459470 return nil
460471 }
461472
462- c .AddCount (packet .Name (), float64 (len (packet .BlockBodiesResponse )))
473+ c .countMsgReceived (packet .Name (), float64 (len (packet .BlockBodiesResponse )))
463474
464475 var hash * common.Hash
465476 for e := c .requests .Front (); e != nil ; e = e .Next () {
@@ -490,7 +501,7 @@ func (c *conn) handleNewBlock(ctx context.Context, msg ethp2p.Msg) error {
490501
491502 tfs := time .Now ()
492503
493- c .AddCount (block .Name (), 1 )
504+ c .countMsgReceived (block .Name (), 1 )
494505
495506 // Set the head block if newer.
496507 c .headMutex .Lock ()
@@ -520,12 +531,11 @@ func (c *conn) handleGetPooledTransactions(msg ethp2p.Msg) error {
520531 return err
521532 }
522533
523- c .AddCount (request .Name (), float64 (len (request .GetPooledTransactionsRequest )))
534+ c .countMsgReceived (request .Name (), float64 (len (request .GetPooledTransactionsRequest )))
524535
525- return ethp2p .Send (
526- c .rw ,
527- eth .PooledTransactionsMsg ,
528- & eth.PooledTransactionsPacket {RequestId : request .RequestId })
536+ response := & eth.PooledTransactionsPacket {RequestId : request .RequestId }
537+ c .countMsgSent (response .Name (), 0 )
538+ return ethp2p .Send (c .rw , eth .PooledTransactionsMsg , response )
529539}
530540
531541func (c * conn ) handleNewPooledTransactionHashes (version uint , msg ethp2p.Msg ) error {
@@ -544,17 +554,15 @@ func (c *conn) handleNewPooledTransactionHashes(version uint, msg ethp2p.Msg) er
544554 return errors .New ("protocol version not found" )
545555 }
546556
547- c .AddCount (name , float64 (len (hashes )))
557+ c .countMsgReceived (name , float64 (len (hashes )))
548558
549559 if ! c .db .ShouldWriteTransactions () || ! c .db .ShouldWriteTransactionEvents () {
550560 return nil
551561 }
552562
553- return ethp2p .Send (
554- c .rw ,
555- eth .GetPooledTransactionsMsg ,
556- & eth.GetPooledTransactionsPacket {GetPooledTransactionsRequest : hashes },
557- )
563+ request := & eth.GetPooledTransactionsPacket {GetPooledTransactionsRequest : hashes }
564+ c .countMsgSent (request .Name (), float64 (len (hashes )))
565+ return ethp2p .Send (c .rw , eth .GetPooledTransactionsMsg , request )
558566}
559567
560568func (c * conn ) handlePooledTransactions (ctx context.Context , msg ethp2p.Msg ) error {
@@ -565,7 +573,7 @@ func (c *conn) handlePooledTransactions(ctx context.Context, msg ethp2p.Msg) err
565573
566574 tfs := time .Now ()
567575
568- c .AddCount (packet .Name (), float64 (len (packet .PooledTransactionsResponse )))
576+ c .countMsgReceived (packet .Name (), float64 (len (packet .PooledTransactionsResponse )))
569577
570578 c .db .WriteTransactions (ctx , c .node , packet .PooledTransactionsResponse , tfs )
571579
@@ -577,9 +585,10 @@ func (c *conn) handleGetReceipts(msg ethp2p.Msg) error {
577585 if err := msg .Decode (& request ); err != nil {
578586 return err
579587 }
580- return ethp2p .Send (
581- c .rw ,
582- eth .ReceiptsMsg ,
583- & eth.ReceiptsPacket {RequestId : request .RequestId },
584- )
588+
589+ c .countMsgReceived (request .Name (), float64 (len (request .GetReceiptsRequest )))
590+
591+ response := & eth.ReceiptsPacket {RequestId : request .RequestId }
592+ c .countMsgSent (response .Name (), 0 )
593+ return ethp2p .Send (c .rw , eth .ReceiptsMsg , response )
585594}
0 commit comments