github.com/aychain/blockbook@v0.1.1-0.20181121092459-6d1fc7e07c5b/common/metrics.go (about)

     1  package common
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  )
     8  
     9  type Metrics struct {
    10  	SocketIORequests      *prometheus.CounterVec
    11  	SocketIOSubscribes    *prometheus.CounterVec
    12  	SocketIOClients       prometheus.Gauge
    13  	SocketIOReqDuration   *prometheus.HistogramVec
    14  	IndexResyncDuration   prometheus.Histogram
    15  	MempoolResyncDuration prometheus.Histogram
    16  	TxCacheEfficiency     *prometheus.CounterVec
    17  	RPCLatency            *prometheus.HistogramVec
    18  	IndexResyncErrors     *prometheus.CounterVec
    19  	IndexDBSize           prometheus.Gauge
    20  	ExplorerViews         *prometheus.CounterVec
    21  	MempoolSize           prometheus.Gauge
    22  	DbColumnRows          *prometheus.GaugeVec
    23  	DbColumnSize          *prometheus.GaugeVec
    24  	BlockbookAppInfo      *prometheus.GaugeVec
    25  }
    26  
    27  type Labels = prometheus.Labels
    28  
    29  func GetMetrics(coin string) (*Metrics, error) {
    30  	metrics := Metrics{}
    31  
    32  	metrics.SocketIORequests = prometheus.NewCounterVec(
    33  		prometheus.CounterOpts{
    34  			Name:        "blockbook_socketio_requests",
    35  			Help:        "Total number of socketio requests by method and status",
    36  			ConstLabels: Labels{"coin": coin},
    37  		},
    38  		[]string{"method", "status"},
    39  	)
    40  	metrics.SocketIOSubscribes = prometheus.NewCounterVec(
    41  		prometheus.CounterOpts{
    42  			Name:        "blockbook_socketio_subscribes",
    43  			Help:        "Total number of socketio subscribes by channel and status",
    44  			ConstLabels: Labels{"coin": coin},
    45  		},
    46  		[]string{"channel", "status"},
    47  	)
    48  	metrics.SocketIOClients = prometheus.NewGauge(
    49  		prometheus.GaugeOpts{
    50  			Name:        "blockbook_socketio_clients",
    51  			Help:        "Number of currently connected clients",
    52  			ConstLabels: Labels{"coin": coin},
    53  		},
    54  	)
    55  	metrics.SocketIOReqDuration = prometheus.NewHistogramVec(
    56  		prometheus.HistogramOpts{
    57  			Name:        "blockbook_socketio_req_duration",
    58  			Help:        "Socketio request duration by method (in microseconds)",
    59  			Buckets:     []float64{1, 5, 10, 25, 50, 75, 100, 250},
    60  			ConstLabels: Labels{"coin": coin},
    61  		},
    62  		[]string{"method"},
    63  	)
    64  	metrics.IndexResyncDuration = prometheus.NewHistogram(
    65  		prometheus.HistogramOpts{
    66  			Name:        "blockbook_index_resync_duration",
    67  			Help:        "Duration of index resync operation (in milliseconds)",
    68  			Buckets:     []float64{50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 600, 700, 1000, 2000, 5000},
    69  			ConstLabels: Labels{"coin": coin},
    70  		},
    71  	)
    72  	metrics.MempoolResyncDuration = prometheus.NewHistogram(
    73  		prometheus.HistogramOpts{
    74  			Name:        "blockbook_mempool_resync_duration",
    75  			Help:        "Duration of mempool resync operation (in milliseconds)",
    76  			Buckets:     []float64{10, 25, 50, 75, 100, 150, 250, 500, 750, 1000, 2000, 5000},
    77  			ConstLabels: Labels{"coin": coin},
    78  		},
    79  	)
    80  	metrics.TxCacheEfficiency = prometheus.NewCounterVec(
    81  		prometheus.CounterOpts{
    82  			Name:        "blockbook_txcache_efficiency",
    83  			Help:        "Efficiency of txCache",
    84  			ConstLabels: Labels{"coin": coin},
    85  		},
    86  		[]string{"status"},
    87  	)
    88  	metrics.RPCLatency = prometheus.NewHistogramVec(
    89  		prometheus.HistogramOpts{
    90  			Name:        "blockbook_rpc_latency",
    91  			Help:        "Latency of blockchain RPC by method (in milliseconds)",
    92  			Buckets:     []float64{0.1, 0.5, 1, 5, 10, 25, 50, 75, 100, 250},
    93  			ConstLabels: Labels{"coin": coin},
    94  		},
    95  		[]string{"method", "error"},
    96  	)
    97  	metrics.IndexResyncErrors = prometheus.NewCounterVec(
    98  		prometheus.CounterOpts{
    99  			Name:        "blockbook_index_resync_errors",
   100  			Help:        "Number of errors of index resync operation",
   101  			ConstLabels: Labels{"coin": coin},
   102  		},
   103  		[]string{"error"},
   104  	)
   105  	metrics.IndexDBSize = prometheus.NewGauge(
   106  		prometheus.GaugeOpts{
   107  			Name:        "blockbook_index_db_size",
   108  			Help:        "Size of index database (in bytes)",
   109  			ConstLabels: Labels{"coin": coin},
   110  		},
   111  	)
   112  	metrics.ExplorerViews = prometheus.NewCounterVec(
   113  		prometheus.CounterOpts{
   114  			Name:        "blockbook_explorer_views",
   115  			Help:        "Number of explorer views",
   116  			ConstLabels: Labels{"coin": coin},
   117  		},
   118  		[]string{"action"},
   119  	)
   120  	metrics.MempoolSize = prometheus.NewGauge(
   121  		prometheus.GaugeOpts{
   122  			Name:        "blockbook_mempool_size",
   123  			Help:        "Mempool size (number of transactions)",
   124  			ConstLabels: Labels{"coin": coin},
   125  		},
   126  	)
   127  	metrics.DbColumnRows = prometheus.NewGaugeVec(
   128  		prometheus.GaugeOpts{
   129  			Name:        "blockbook_dbcolumn_rows",
   130  			Help:        "Number of rows in db column",
   131  			ConstLabels: Labels{"coin": coin},
   132  		},
   133  		[]string{"column"},
   134  	)
   135  	metrics.DbColumnSize = prometheus.NewGaugeVec(
   136  		prometheus.GaugeOpts{
   137  			Name:        "blockbook_dbcolumn_size",
   138  			Help:        "Size of db column (in bytes)",
   139  			ConstLabels: Labels{"coin": coin},
   140  		},
   141  		[]string{"column"},
   142  	)
   143  	metrics.BlockbookAppInfo = prometheus.NewGaugeVec(
   144  		prometheus.GaugeOpts{
   145  			Name:        "blockbook_app_info",
   146  			Help:        "Information about blockbook and backend application versions",
   147  			ConstLabels: Labels{"coin": coin},
   148  		},
   149  		[]string{"blockbook_version", "blockbook_commit", "blockbook_buildtime", "backend_version", "backend_subversion", "backend_protocol_version"},
   150  	)
   151  
   152  	v := reflect.ValueOf(metrics)
   153  	for i := 0; i < v.NumField(); i++ {
   154  		c := v.Field(i).Interface().(prometheus.Collector)
   155  		err := prometheus.Register(c)
   156  		if err != nil {
   157  			return nil, err
   158  		}
   159  	}
   160  
   161  	return &metrics, nil
   162  }