github.com/cryptohub-digital/blockbook-fork@v0.0.0-20230713133354-673c927af7f1/common/metrics.go (about)

     1  package common
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  )
     8  
     9  // Metrics holds prometheus collectors for various metrics collected by Blockbook
    10  type Metrics struct {
    11  	SocketIORequests         *prometheus.CounterVec
    12  	SocketIOSubscribes       *prometheus.CounterVec
    13  	SocketIOClients          prometheus.Gauge
    14  	SocketIOReqDuration      *prometheus.HistogramVec
    15  	WebsocketRequests        *prometheus.CounterVec
    16  	WebsocketSubscribes      *prometheus.GaugeVec
    17  	WebsocketClients         prometheus.Gauge
    18  	WebsocketReqDuration     *prometheus.HistogramVec
    19  	IndexResyncDuration      prometheus.Histogram
    20  	MempoolResyncDuration    prometheus.Histogram
    21  	TxCacheEfficiency        *prometheus.CounterVec
    22  	RPCLatency               *prometheus.HistogramVec
    23  	IndexResyncErrors        *prometheus.CounterVec
    24  	IndexDBSize              prometheus.Gauge
    25  	ExplorerViews            *prometheus.CounterVec
    26  	MempoolSize              prometheus.Gauge
    27  	EstimatedFee             *prometheus.GaugeVec
    28  	AvgBlockPeriod           prometheus.Gauge
    29  	DbColumnRows             *prometheus.GaugeVec
    30  	DbColumnSize             *prometheus.GaugeVec
    31  	BlockbookAppInfo         *prometheus.GaugeVec
    32  	BackendBestHeight        prometheus.Gauge
    33  	BlockbookBestHeight      prometheus.Gauge
    34  	ExplorerPendingRequests  *prometheus.GaugeVec
    35  	WebsocketPendingRequests *prometheus.GaugeVec
    36  	SocketIOPendingRequests  *prometheus.GaugeVec
    37  	XPubCacheSize            prometheus.Gauge
    38  	CoingeckoRequests        *prometheus.CounterVec
    39  }
    40  
    41  // Labels represents a collection of label name -> value mappings.
    42  type Labels = prometheus.Labels
    43  
    44  // GetMetrics returns struct holding prometheus collectors for various metrics collected by Blockbook
    45  func GetMetrics(coin string) (*Metrics, error) {
    46  	metrics := Metrics{}
    47  
    48  	metrics.SocketIORequests = prometheus.NewCounterVec(
    49  		prometheus.CounterOpts{
    50  			Name:        "blockbook_socketio_requests",
    51  			Help:        "Total number of socketio requests by method and status",
    52  			ConstLabels: Labels{"coin": coin},
    53  		},
    54  		[]string{"method", "status"},
    55  	)
    56  	metrics.SocketIOSubscribes = prometheus.NewCounterVec(
    57  		prometheus.CounterOpts{
    58  			Name:        "blockbook_socketio_subscribes",
    59  			Help:        "Total number of socketio subscribes by channel and status",
    60  			ConstLabels: Labels{"coin": coin},
    61  		},
    62  		[]string{"channel", "status"},
    63  	)
    64  	metrics.SocketIOClients = prometheus.NewGauge(
    65  		prometheus.GaugeOpts{
    66  			Name:        "blockbook_socketio_clients",
    67  			Help:        "Number of currently connected socketio clients",
    68  			ConstLabels: Labels{"coin": coin},
    69  		},
    70  	)
    71  	metrics.SocketIOReqDuration = prometheus.NewHistogramVec(
    72  		prometheus.HistogramOpts{
    73  			Name:        "blockbook_socketio_req_duration",
    74  			Help:        "Socketio request duration by method (in microseconds)",
    75  			Buckets:     []float64{1, 5, 10, 25, 50, 75, 100, 250},
    76  			ConstLabels: Labels{"coin": coin},
    77  		},
    78  		[]string{"method"},
    79  	)
    80  	metrics.WebsocketRequests = prometheus.NewCounterVec(
    81  		prometheus.CounterOpts{
    82  			Name:        "blockbook_websocket_requests",
    83  			Help:        "Total number of websocket requests by method and status",
    84  			ConstLabels: Labels{"coin": coin},
    85  		},
    86  		[]string{"method", "status"},
    87  	)
    88  	metrics.WebsocketSubscribes = prometheus.NewGaugeVec(
    89  		prometheus.GaugeOpts{
    90  			Name:        "blockbook_websocket_subscribes",
    91  			Help:        "Number of websocket subscriptions by method",
    92  			ConstLabels: Labels{"coin": coin},
    93  		},
    94  		[]string{"method"},
    95  	)
    96  	metrics.WebsocketClients = prometheus.NewGauge(
    97  		prometheus.GaugeOpts{
    98  			Name:        "blockbook_websocket_clients",
    99  			Help:        "Number of currently connected websocket clients",
   100  			ConstLabels: Labels{"coin": coin},
   101  		},
   102  	)
   103  	metrics.WebsocketReqDuration = prometheus.NewHistogramVec(
   104  		prometheus.HistogramOpts{
   105  			Name:        "blockbook_websocket_req_duration",
   106  			Help:        "Websocket request duration by method (in microseconds)",
   107  			Buckets:     []float64{1, 5, 10, 25, 50, 75, 100, 250},
   108  			ConstLabels: Labels{"coin": coin},
   109  		},
   110  		[]string{"method"},
   111  	)
   112  	metrics.IndexResyncDuration = prometheus.NewHistogram(
   113  		prometheus.HistogramOpts{
   114  			Name:        "blockbook_index_resync_duration",
   115  			Help:        "Duration of index resync operation (in milliseconds)",
   116  			Buckets:     []float64{50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 600, 700, 1000, 2000, 5000},
   117  			ConstLabels: Labels{"coin": coin},
   118  		},
   119  	)
   120  	metrics.MempoolResyncDuration = prometheus.NewHistogram(
   121  		prometheus.HistogramOpts{
   122  			Name:        "blockbook_mempool_resync_duration",
   123  			Help:        "Duration of mempool resync operation (in milliseconds)",
   124  			Buckets:     []float64{10, 25, 50, 75, 100, 150, 250, 500, 750, 1000, 2000, 5000},
   125  			ConstLabels: Labels{"coin": coin},
   126  		},
   127  	)
   128  	metrics.TxCacheEfficiency = prometheus.NewCounterVec(
   129  		prometheus.CounterOpts{
   130  			Name:        "blockbook_txcache_efficiency",
   131  			Help:        "Efficiency of txCache",
   132  			ConstLabels: Labels{"coin": coin},
   133  		},
   134  		[]string{"status"},
   135  	)
   136  	metrics.RPCLatency = prometheus.NewHistogramVec(
   137  		prometheus.HistogramOpts{
   138  			Name:        "blockbook_rpc_latency",
   139  			Help:        "Latency of blockchain RPC by method (in milliseconds)",
   140  			Buckets:     []float64{0.1, 0.5, 1, 5, 10, 25, 50, 75, 100, 250},
   141  			ConstLabels: Labels{"coin": coin},
   142  		},
   143  		[]string{"method", "error"},
   144  	)
   145  	metrics.IndexResyncErrors = prometheus.NewCounterVec(
   146  		prometheus.CounterOpts{
   147  			Name:        "blockbook_index_resync_errors",
   148  			Help:        "Number of errors of index resync operation",
   149  			ConstLabels: Labels{"coin": coin},
   150  		},
   151  		[]string{"error"},
   152  	)
   153  	metrics.IndexDBSize = prometheus.NewGauge(
   154  		prometheus.GaugeOpts{
   155  			Name:        "blockbook_index_db_size",
   156  			Help:        "Size of index database (in bytes)",
   157  			ConstLabels: Labels{"coin": coin},
   158  		},
   159  	)
   160  	metrics.ExplorerViews = prometheus.NewCounterVec(
   161  		prometheus.CounterOpts{
   162  			Name:        "blockbook_explorer_views",
   163  			Help:        "Number of explorer views",
   164  			ConstLabels: Labels{"coin": coin},
   165  		},
   166  		[]string{"action"},
   167  	)
   168  	metrics.MempoolSize = prometheus.NewGauge(
   169  		prometheus.GaugeOpts{
   170  			Name:        "blockbook_mempool_size",
   171  			Help:        "Mempool size (number of transactions)",
   172  			ConstLabels: Labels{"coin": coin},
   173  		},
   174  	)
   175  	metrics.EstimatedFee = prometheus.NewGaugeVec(
   176  		prometheus.GaugeOpts{
   177  			Name:        "blockbook_estimated_fee",
   178  			Help:        "Estimated fee per byte (gas) for number of blocks",
   179  			ConstLabels: Labels{"coin": coin},
   180  		},
   181  		[]string{"blocks", "conservative"},
   182  	)
   183  	metrics.AvgBlockPeriod = prometheus.NewGauge(
   184  		prometheus.GaugeOpts{
   185  			Name:        "blockbook_avg_block_period",
   186  			Help:        "Average period of mining of last 100 blocks in seconds",
   187  			ConstLabels: Labels{"coin": coin},
   188  		},
   189  	)
   190  	metrics.DbColumnRows = prometheus.NewGaugeVec(
   191  		prometheus.GaugeOpts{
   192  			Name:        "blockbook_dbcolumn_rows",
   193  			Help:        "Number of rows in db column",
   194  			ConstLabels: Labels{"coin": coin},
   195  		},
   196  		[]string{"column"},
   197  	)
   198  	metrics.DbColumnSize = prometheus.NewGaugeVec(
   199  		prometheus.GaugeOpts{
   200  			Name:        "blockbook_dbcolumn_size",
   201  			Help:        "Size of db column (in bytes)",
   202  			ConstLabels: Labels{"coin": coin},
   203  		},
   204  		[]string{"column"},
   205  	)
   206  	metrics.BlockbookAppInfo = prometheus.NewGaugeVec(
   207  		prometheus.GaugeOpts{
   208  			Name:        "blockbook_app_info",
   209  			Help:        "Information about blockbook and backend application versions",
   210  			ConstLabels: Labels{"coin": coin},
   211  		},
   212  		[]string{"blockbook_version", "blockbook_commit", "blockbook_buildtime", "backend_version", "backend_subversion", "backend_protocol_version"},
   213  	)
   214  	metrics.BlockbookBestHeight = prometheus.NewGauge(
   215  		prometheus.GaugeOpts{
   216  			Name:        "blockbook_best_height",
   217  			Help:        "Block height in Blockbook",
   218  			ConstLabels: Labels{"coin": coin},
   219  		},
   220  	)
   221  	metrics.BackendBestHeight = prometheus.NewGauge(
   222  		prometheus.GaugeOpts{
   223  			Name:        "blockbook_backend_best_height",
   224  			Help:        "Block height in backend",
   225  			ConstLabels: Labels{"coin": coin},
   226  		},
   227  	)
   228  	metrics.ExplorerPendingRequests = prometheus.NewGaugeVec(
   229  		prometheus.GaugeOpts{
   230  			Name:        "blockbook_explorer_pending_requests",
   231  			Help:        "Number of unfinished requests in explorer interface",
   232  			ConstLabels: Labels{"coin": coin},
   233  		},
   234  		[]string{"method"},
   235  	)
   236  	metrics.WebsocketPendingRequests = prometheus.NewGaugeVec(
   237  		prometheus.GaugeOpts{
   238  			Name:        "blockbook_websocket_pending_requests",
   239  			Help:        "Number of unfinished requests in websocket interface",
   240  			ConstLabels: Labels{"coin": coin},
   241  		},
   242  		[]string{"method"},
   243  	)
   244  	metrics.SocketIOPendingRequests = prometheus.NewGaugeVec(
   245  		prometheus.GaugeOpts{
   246  			Name:        "blockbook_socketio_pending_requests",
   247  			Help:        "Number of unfinished requests in socketio interface",
   248  			ConstLabels: Labels{"coin": coin},
   249  		},
   250  		[]string{"method"},
   251  	)
   252  	metrics.XPubCacheSize = prometheus.NewGauge(
   253  		prometheus.GaugeOpts{
   254  			Name:        "blockbook_xpub_cache_size",
   255  			Help:        "Number of cached xpubs",
   256  			ConstLabels: Labels{"coin": coin},
   257  		},
   258  	)
   259  	metrics.CoingeckoRequests = prometheus.NewCounterVec(
   260  		prometheus.CounterOpts{
   261  			Name:        "blockbook_coingecko_requests",
   262  			Help:        "Total number of requests to coingecko",
   263  			ConstLabels: Labels{"coin": coin},
   264  		},
   265  		[]string{"endpoint", "status"},
   266  	)
   267  
   268  	v := reflect.ValueOf(metrics)
   269  	for i := 0; i < v.NumField(); i++ {
   270  		c := v.Field(i).Interface().(prometheus.Collector)
   271  		err := prometheus.Register(c)
   272  		if err != nil {
   273  			return nil, err
   274  		}
   275  	}
   276  
   277  	return &metrics, nil
   278  }