github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/metrics/access.go (about)

     1  package metrics
     2  
     3  import (
     4  	"github.com/prometheus/client_golang/prometheus"
     5  	"github.com/prometheus/client_golang/prometheus/promauto"
     6  
     7  	"github.com/onflow/flow-go/module"
     8  	"github.com/onflow/flow-go/module/counters"
     9  )
    10  
    11  type AccessCollectorOpts func(*AccessCollector)
    12  
    13  func WithTransactionMetrics(m module.TransactionMetrics) AccessCollectorOpts {
    14  	return func(ac *AccessCollector) {
    15  		ac.TransactionMetrics = m
    16  	}
    17  }
    18  
    19  func WithBackendScriptsMetrics(m module.BackendScriptsMetrics) AccessCollectorOpts {
    20  	return func(ac *AccessCollector) {
    21  		ac.BackendScriptsMetrics = m
    22  	}
    23  }
    24  
    25  func WithRestMetrics(m module.RestMetrics) AccessCollectorOpts {
    26  	return func(ac *AccessCollector) {
    27  		ac.RestMetrics = m
    28  	}
    29  }
    30  
    31  type AccessCollector struct {
    32  	module.RestMetrics
    33  	module.TransactionMetrics
    34  	module.BackendScriptsMetrics
    35  
    36  	connectionReused      prometheus.Counter
    37  	connectionsInPool     *prometheus.GaugeVec
    38  	connectionAdded       prometheus.Counter
    39  	connectionEstablished prometheus.Counter
    40  	connectionInvalidated prometheus.Counter
    41  	connectionUpdated     prometheus.Counter
    42  	connectionEvicted     prometheus.Counter
    43  	lastFullBlockHeight   prometheus.Gauge
    44  	maxReceiptHeight      prometheus.Gauge
    45  
    46  	// used to skip heights that are lower than the current max height
    47  	maxReceiptHeightValue counters.StrictMonotonousCounter
    48  }
    49  
    50  var _ module.AccessMetrics = (*AccessCollector)(nil)
    51  
    52  func NewAccessCollector(opts ...AccessCollectorOpts) *AccessCollector {
    53  	ac := &AccessCollector{
    54  		connectionReused: promauto.NewCounter(prometheus.CounterOpts{
    55  			Name:      "connection_reused",
    56  			Namespace: namespaceAccess,
    57  			Subsystem: subsystemConnectionPool,
    58  			Help:      "counter for the number of times connections get reused",
    59  		}),
    60  		connectionsInPool: promauto.NewGaugeVec(prometheus.GaugeOpts{
    61  			Name:      "connections_in_pool",
    62  			Namespace: namespaceAccess,
    63  			Subsystem: subsystemConnectionPool,
    64  			Help:      "counter for the number of connections in the pool against max number tne pool can hold",
    65  		}, []string{"result"}),
    66  		connectionAdded: promauto.NewCounter(prometheus.CounterOpts{
    67  			Name:      "connection_added",
    68  			Namespace: namespaceAccess,
    69  			Subsystem: subsystemConnectionPool,
    70  			Help:      "counter for the number of times connections are added to the pool",
    71  		}),
    72  		connectionEstablished: promauto.NewCounter(prometheus.CounterOpts{
    73  			Name:      "connection_established",
    74  			Namespace: namespaceAccess,
    75  			Subsystem: subsystemConnectionPool,
    76  			Help:      "counter for the number of times connections are established",
    77  		}),
    78  		connectionInvalidated: promauto.NewCounter(prometheus.CounterOpts{
    79  			Name:      "connection_invalidated",
    80  			Namespace: namespaceAccess,
    81  			Subsystem: subsystemConnectionPool,
    82  			Help:      "counter for the number of times connections are invalidated",
    83  		}),
    84  		connectionUpdated: promauto.NewCounter(prometheus.CounterOpts{
    85  			Name:      "connection_updated",
    86  			Namespace: namespaceAccess,
    87  			Subsystem: subsystemConnectionPool,
    88  			Help:      "counter for the number of times existing connections from the pool are updated",
    89  		}),
    90  		connectionEvicted: promauto.NewCounter(prometheus.CounterOpts{
    91  			Name:      "connection_evicted",
    92  			Namespace: namespaceAccess,
    93  			Subsystem: subsystemConnectionPool,
    94  			Help:      "counter for the number of times a cached connection is evicted from the connection pool",
    95  		}),
    96  		lastFullBlockHeight: promauto.NewGauge(prometheus.GaugeOpts{
    97  			Name:      "last_full_finalized_block_height",
    98  			Namespace: namespaceAccess,
    99  			Subsystem: subsystemIngestion,
   100  			Help:      "gauge to track the highest consecutive finalized block height with all collections indexed",
   101  		}),
   102  		maxReceiptHeight: promauto.NewGauge(prometheus.GaugeOpts{
   103  			Name:      "max_receipt_height",
   104  			Namespace: namespaceAccess,
   105  			Subsystem: subsystemIngestion,
   106  			Help:      "gauge to track the maximum block height of execution receipts received",
   107  		}),
   108  		maxReceiptHeightValue: counters.NewMonotonousCounter(0),
   109  	}
   110  
   111  	for _, opt := range opts {
   112  		opt(ac)
   113  	}
   114  
   115  	return ac
   116  }
   117  
   118  func (ac *AccessCollector) ConnectionFromPoolReused() {
   119  	ac.connectionReused.Inc()
   120  }
   121  
   122  func (ac *AccessCollector) TotalConnectionsInPool(connectionCount uint, connectionPoolSize uint) {
   123  	ac.connectionsInPool.WithLabelValues("connections").Set(float64(connectionCount))
   124  	ac.connectionsInPool.WithLabelValues("pool_size").Set(float64(connectionPoolSize))
   125  }
   126  
   127  func (ac *AccessCollector) ConnectionAddedToPool() {
   128  	ac.connectionAdded.Inc()
   129  }
   130  
   131  func (ac *AccessCollector) NewConnectionEstablished() {
   132  	ac.connectionEstablished.Inc()
   133  }
   134  
   135  func (ac *AccessCollector) ConnectionFromPoolInvalidated() {
   136  	ac.connectionInvalidated.Inc()
   137  }
   138  
   139  func (ac *AccessCollector) ConnectionFromPoolUpdated() {
   140  	ac.connectionUpdated.Inc()
   141  }
   142  
   143  func (ac *AccessCollector) ConnectionFromPoolEvicted() {
   144  	ac.connectionEvicted.Inc()
   145  }
   146  
   147  func (ac *AccessCollector) UpdateLastFullBlockHeight(height uint64) {
   148  	ac.lastFullBlockHeight.Set(float64(height))
   149  }
   150  
   151  func (ac *AccessCollector) UpdateExecutionReceiptMaxHeight(height uint64) {
   152  	if ac.maxReceiptHeightValue.Set(height) {
   153  		ac.maxReceiptHeight.Set(float64(height))
   154  	}
   155  }