github.com/crptec/blockbook@v0.3.2/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.CounterVec
    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  	DbColumnRows          *prometheus.GaugeVec
    28  	DbColumnSize          *prometheus.GaugeVec
    29  	BlockbookAppInfo      *prometheus.GaugeVec
    30  }
    31  
    32  // Labels represents a collection of label name -> value mappings.
    33  type Labels = prometheus.Labels
    34  
    35  // GetMetrics returns struct holding prometheus collectors for various metrics collected by Blockbook
    36  func GetMetrics(coin string) (*Metrics, error) {
    37  	metrics := Metrics{}
    38  
    39  	metrics.SocketIORequests = prometheus.NewCounterVec(
    40  		prometheus.CounterOpts{
    41  			Name:        "blockbook_socketio_requests",
    42  			Help:        "Total number of socketio requests by method and status",
    43  			ConstLabels: Labels{"coin": coin},
    44  		},
    45  		[]string{"method", "status"},
    46  	)
    47  	metrics.SocketIOSubscribes = prometheus.NewCounterVec(
    48  		prometheus.CounterOpts{
    49  			Name:        "blockbook_socketio_subscribes",
    50  			Help:        "Total number of socketio subscribes by channel and status",
    51  			ConstLabels: Labels{"coin": coin},
    52  		},
    53  		[]string{"channel", "status"},
    54  	)
    55  	metrics.SocketIOClients = prometheus.NewGauge(
    56  		prometheus.GaugeOpts{
    57  			Name:        "blockbook_socketio_clients",
    58  			Help:        "Number of currently connected socketio clients",
    59  			ConstLabels: Labels{"coin": coin},
    60  		},
    61  	)
    62  	metrics.SocketIOReqDuration = prometheus.NewHistogramVec(
    63  		prometheus.HistogramOpts{
    64  			Name:        "blockbook_socketio_req_duration",
    65  			Help:        "Socketio request duration by method (in microseconds)",
    66  			Buckets:     []float64{1, 5, 10, 25, 50, 75, 100, 250},
    67  			ConstLabels: Labels{"coin": coin},
    68  		},
    69  		[]string{"method"},
    70  	)
    71  	metrics.WebsocketRequests = prometheus.NewCounterVec(
    72  		prometheus.CounterOpts{
    73  			Name:        "blockbook_websocket_requests",
    74  			Help:        "Total number of websocket requests by method and status",
    75  			ConstLabels: Labels{"coin": coin},
    76  		},
    77  		[]string{"method", "status"},
    78  	)
    79  	metrics.WebsocketSubscribes = prometheus.NewCounterVec(
    80  		prometheus.CounterOpts{
    81  			Name:        "blockbook_websocket_subscribes",
    82  			Help:        "Total number of websocket subscribes by channel and status",
    83  			ConstLabels: Labels{"coin": coin},
    84  		},
    85  		[]string{"channel", "status"},
    86  	)
    87  	metrics.WebsocketClients = prometheus.NewGauge(
    88  		prometheus.GaugeOpts{
    89  			Name:        "blockbook_websocket_clients",
    90  			Help:        "Number of currently connected websocket clients",
    91  			ConstLabels: Labels{"coin": coin},
    92  		},
    93  	)
    94  	metrics.WebsocketReqDuration = prometheus.NewHistogramVec(
    95  		prometheus.HistogramOpts{
    96  			Name:        "blockbook_websocket_req_duration",
    97  			Help:        "Websocket request duration by method (in microseconds)",
    98  			Buckets:     []float64{1, 5, 10, 25, 50, 75, 100, 250},
    99  			ConstLabels: Labels{"coin": coin},
   100  		},
   101  		[]string{"method"},
   102  	)
   103  	metrics.IndexResyncDuration = prometheus.NewHistogram(
   104  		prometheus.HistogramOpts{
   105  			Name:        "blockbook_index_resync_duration",
   106  			Help:        "Duration of index resync operation (in milliseconds)",
   107  			Buckets:     []float64{50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 600, 700, 1000, 2000, 5000},
   108  			ConstLabels: Labels{"coin": coin},
   109  		},
   110  	)
   111  	metrics.MempoolResyncDuration = prometheus.NewHistogram(
   112  		prometheus.HistogramOpts{
   113  			Name:        "blockbook_mempool_resync_duration",
   114  			Help:        "Duration of mempool resync operation (in milliseconds)",
   115  			Buckets:     []float64{10, 25, 50, 75, 100, 150, 250, 500, 750, 1000, 2000, 5000},
   116  			ConstLabels: Labels{"coin": coin},
   117  		},
   118  	)
   119  	metrics.TxCacheEfficiency = prometheus.NewCounterVec(
   120  		prometheus.CounterOpts{
   121  			Name:        "blockbook_txcache_efficiency",
   122  			Help:        "Efficiency of txCache",
   123  			ConstLabels: Labels{"coin": coin},
   124  		},
   125  		[]string{"status"},
   126  	)
   127  	metrics.RPCLatency = prometheus.NewHistogramVec(
   128  		prometheus.HistogramOpts{
   129  			Name:        "blockbook_rpc_latency",
   130  			Help:        "Latency of blockchain RPC by method (in milliseconds)",
   131  			Buckets:     []float64{0.1, 0.5, 1, 5, 10, 25, 50, 75, 100, 250},
   132  			ConstLabels: Labels{"coin": coin},
   133  		},
   134  		[]string{"method", "error"},
   135  	)
   136  	metrics.IndexResyncErrors = prometheus.NewCounterVec(
   137  		prometheus.CounterOpts{
   138  			Name:        "blockbook_index_resync_errors",
   139  			Help:        "Number of errors of index resync operation",
   140  			ConstLabels: Labels{"coin": coin},
   141  		},
   142  		[]string{"error"},
   143  	)
   144  	metrics.IndexDBSize = prometheus.NewGauge(
   145  		prometheus.GaugeOpts{
   146  			Name:        "blockbook_index_db_size",
   147  			Help:        "Size of index database (in bytes)",
   148  			ConstLabels: Labels{"coin": coin},
   149  		},
   150  	)
   151  	metrics.ExplorerViews = prometheus.NewCounterVec(
   152  		prometheus.CounterOpts{
   153  			Name:        "blockbook_explorer_views",
   154  			Help:        "Number of explorer views",
   155  			ConstLabels: Labels{"coin": coin},
   156  		},
   157  		[]string{"action"},
   158  	)
   159  	metrics.MempoolSize = prometheus.NewGauge(
   160  		prometheus.GaugeOpts{
   161  			Name:        "blockbook_mempool_size",
   162  			Help:        "Mempool size (number of transactions)",
   163  			ConstLabels: Labels{"coin": coin},
   164  		},
   165  	)
   166  	metrics.DbColumnRows = prometheus.NewGaugeVec(
   167  		prometheus.GaugeOpts{
   168  			Name:        "blockbook_dbcolumn_rows",
   169  			Help:        "Number of rows in db column",
   170  			ConstLabels: Labels{"coin": coin},
   171  		},
   172  		[]string{"column"},
   173  	)
   174  	metrics.DbColumnSize = prometheus.NewGaugeVec(
   175  		prometheus.GaugeOpts{
   176  			Name:        "blockbook_dbcolumn_size",
   177  			Help:        "Size of db column (in bytes)",
   178  			ConstLabels: Labels{"coin": coin},
   179  		},
   180  		[]string{"column"},
   181  	)
   182  	metrics.BlockbookAppInfo = prometheus.NewGaugeVec(
   183  		prometheus.GaugeOpts{
   184  			Name:        "blockbook_app_info",
   185  			Help:        "Information about blockbook and backend application versions",
   186  			ConstLabels: Labels{"coin": coin},
   187  		},
   188  		[]string{"blockbook_version", "blockbook_commit", "blockbook_buildtime", "backend_version", "backend_subversion", "backend_protocol_version"},
   189  	)
   190  
   191  	v := reflect.ValueOf(metrics)
   192  	for i := 0; i < v.NumField(); i++ {
   193  		c := v.Field(i).Interface().(prometheus.Collector)
   194  		err := prometheus.Register(c)
   195  		if err != nil {
   196  			return nil, err
   197  		}
   198  	}
   199  
   200  	return &metrics, nil
   201  }