github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/network/prometheus.go (about)

     1  package network
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"github.com/prometheus/client_golang/prometheus"
     8  )
     9  
    10  // Metric used in monitoring service.
    11  var (
    12  	estimatedNetworkSize = prometheus.NewGauge(
    13  		prometheus.GaugeOpts{
    14  			Help:      "Estimated network size",
    15  			Name:      "network_size",
    16  			Namespace: "neogo",
    17  		},
    18  	)
    19  
    20  	peersConnected = prometheus.NewGauge(
    21  		prometheus.GaugeOpts{
    22  			Help:      "Number of connected peers",
    23  			Name:      "peers_connected",
    24  			Namespace: "neogo",
    25  		},
    26  	)
    27  
    28  	// Deprecated: please, use neogoVersion and serverID instead.
    29  	servAndNodeVersion = prometheus.NewGaugeVec(
    30  		prometheus.GaugeOpts{
    31  			Help:      "Server and Node versions",
    32  			Name:      "serv_node_version",
    33  			Namespace: "neogo",
    34  		},
    35  		[]string{"description", "value"},
    36  	)
    37  
    38  	neogoVersion = prometheus.NewGaugeVec(
    39  		prometheus.GaugeOpts{
    40  			Help:      "NeoGo version",
    41  			Name:      "version",
    42  			Namespace: "neogo",
    43  		},
    44  		[]string{"version"})
    45  
    46  	serverID = prometheus.NewGaugeVec(
    47  		prometheus.GaugeOpts{
    48  			Help:      "network server ID",
    49  			Name:      "server_id",
    50  			Namespace: "neogo",
    51  		},
    52  		[]string{"server_id"})
    53  
    54  	poolCount = prometheus.NewGauge(
    55  		prometheus.GaugeOpts{
    56  			Help:      "Number of available node addresses",
    57  			Name:      "pool_count",
    58  			Namespace: "neogo",
    59  		},
    60  	)
    61  
    62  	blockQueueLength = prometheus.NewGauge(
    63  		prometheus.GaugeOpts{
    64  			Help:      "Block queue length",
    65  			Name:      "block_queue_length",
    66  			Namespace: "neogo",
    67  		},
    68  	)
    69  	p2pCmds = make(map[CommandType]prometheus.Histogram)
    70  
    71  	// notarypoolUnsortedTx prometheus metric.
    72  	notarypoolUnsortedTx = prometheus.NewGauge(
    73  		prometheus.GaugeOpts{
    74  			Help:      "Notary request pool fallback txs",
    75  			Name:      "notarypool_unsorted_tx",
    76  			Namespace: "neogo",
    77  		},
    78  	)
    79  )
    80  
    81  func init() {
    82  	prometheus.MustRegister(
    83  		estimatedNetworkSize,
    84  		peersConnected,
    85  		servAndNodeVersion,
    86  		neogoVersion,
    87  		serverID,
    88  		poolCount,
    89  		blockQueueLength,
    90  		notarypoolUnsortedTx,
    91  	)
    92  	for _, cmd := range []CommandType{CMDVersion, CMDVerack, CMDGetAddr,
    93  		CMDAddr, CMDPing, CMDPong, CMDGetHeaders, CMDHeaders, CMDGetBlocks,
    94  		CMDMempool, CMDInv, CMDGetData, CMDGetBlockByIndex, CMDNotFound,
    95  		CMDTX, CMDBlock, CMDExtensible, CMDP2PNotaryRequest, CMDGetMPTData,
    96  		CMDMPTData, CMDReject, CMDFilterLoad, CMDFilterAdd, CMDFilterClear,
    97  		CMDMerkleBlock, CMDAlert} {
    98  		p2pCmds[cmd] = prometheus.NewHistogram(
    99  			prometheus.HistogramOpts{
   100  				Help:      "P2P " + cmd.String() + " handling time",
   101  				Name:      "p2p_" + strings.ToLower(cmd.String()) + "_time",
   102  				Namespace: "neogo",
   103  			},
   104  		)
   105  		prometheus.MustRegister(p2pCmds[cmd])
   106  	}
   107  }
   108  
   109  func updateNetworkSizeMetric(sz int) {
   110  	estimatedNetworkSize.Set(float64(sz))
   111  }
   112  
   113  func updateBlockQueueLenMetric(bqLen int) {
   114  	blockQueueLength.Set(float64(bqLen))
   115  }
   116  
   117  func updatePoolCountMetric(pCount int) {
   118  	poolCount.Set(float64(pCount))
   119  }
   120  
   121  func updatePeersConnectedMetric(pConnected int) {
   122  	peersConnected.Set(float64(pConnected))
   123  }
   124  
   125  // Deprecated: please, use setNeoGoVersion and setSeverID instead.
   126  func setServerAndNodeVersions(nodeVer string, serverID string) {
   127  	servAndNodeVersion.WithLabelValues("Node version: ", nodeVer).Add(0)
   128  	servAndNodeVersion.WithLabelValues("Server id: ", serverID).Add(0)
   129  }
   130  
   131  func setNeoGoVersion(nodeVer string) {
   132  	neogoVersion.WithLabelValues(nodeVer).Add(1)
   133  }
   134  
   135  func setSeverID(id string) {
   136  	serverID.WithLabelValues(id).Add(1)
   137  }
   138  
   139  func addCmdTimeMetric(cmd CommandType, t time.Duration) {
   140  	// Shouldn't happen, message decoder checks the type, but better safe than sorry.
   141  	if p2pCmds[cmd] == nil {
   142  		return
   143  	}
   144  	p2pCmds[cmd].Observe(t.Seconds())
   145  }
   146  
   147  // updateNotarypoolMetrics updates metric of the number of fallback txs inside
   148  // the notary request pool.
   149  func updateNotarypoolMetrics(unsortedTxnLen int) {
   150  	notarypoolUnsortedTx.Set(float64(unsortedTxnLen))
   151  }