network: add prometheus histogram with cmd processing time

It can be useful to detect some performance issues.
This commit is contained in:
Roman Khimov 2022-10-17 15:31:39 +03:00
parent 73079745ab
commit 2791127ee4
2 changed files with 29 additions and 0 deletions

View file

@ -1,6 +1,9 @@
package network package network
import ( import (
"strings"
"time"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
@ -46,6 +49,7 @@ var (
Namespace: "neogo", Namespace: "neogo",
}, },
) )
p2pCmds = make(map[CommandType]prometheus.Histogram)
) )
func init() { func init() {
@ -56,6 +60,21 @@ func init() {
poolCount, poolCount,
blockQueueLength, blockQueueLength,
) )
for _, cmd := range []CommandType{CMDVersion, CMDVerack, CMDGetAddr,
CMDAddr, CMDPing, CMDPong, CMDGetHeaders, CMDHeaders, CMDGetBlocks,
CMDMempool, CMDInv, CMDGetData, CMDGetBlockByIndex, CMDNotFound,
CMDTX, CMDBlock, CMDExtensible, CMDP2PNotaryRequest, CMDGetMPTData,
CMDMPTData, CMDReject, CMDFilterLoad, CMDFilterAdd, CMDFilterClear,
CMDMerkleBlock, CMDAlert} {
p2pCmds[cmd] = prometheus.NewHistogram(
prometheus.HistogramOpts{
Help: "P2P " + cmd.String() + " handling time",
Name: "p2p_" + strings.ToLower(cmd.String()) + "_time",
Namespace: "neogo",
},
)
prometheus.MustRegister(p2pCmds[cmd])
}
} }
func updateNetworkSizeMetric(sz int) { func updateNetworkSizeMetric(sz int) {
@ -77,3 +96,10 @@ func setServerAndNodeVersions(nodeVer string, serverID string) {
servAndNodeVersion.WithLabelValues("Node version: ", nodeVer).Add(0) servAndNodeVersion.WithLabelValues("Node version: ", nodeVer).Add(0)
servAndNodeVersion.WithLabelValues("Server id: ", serverID).Add(0) servAndNodeVersion.WithLabelValues("Server id: ", serverID).Add(0)
} }
func addCmdTimeMetric(cmd CommandType, t time.Duration) {
// Shouldn't happen, message decoder checks the type, but better safe than sorry.
if p2pCmds[cmd] == nil {
return
}
p2pCmds[cmd].Observe(t.Seconds())
}

View file

@ -1198,6 +1198,9 @@ func (s *Server) handleMessage(peer Peer, msg *Message) error {
zap.Stringer("addr", peer.RemoteAddr()), zap.Stringer("addr", peer.RemoteAddr()),
zap.String("type", msg.Command.String())) zap.String("type", msg.Command.String()))
start := time.Now()
defer func() { addCmdTimeMetric(msg.Command, time.Since(start)) }()
if peer.Handshaked() { if peer.Handshaked() {
if inv, ok := msg.Payload.(*payload.Inventory); ok { if inv, ok := msg.Payload.(*payload.Inventory); ok {
if !inv.Type.Valid(s.chain.P2PSigExtensionsEnabled()) || len(inv.Hashes) == 0 { if !inv.Type.Valid(s.chain.P2PSigExtensionsEnabled()) || len(inv.Hashes) == 0 {