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

     1  package metrics
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  	"github.com/prometheus/client_golang/prometheus/promauto"
     8  
     9  	"github.com/onflow/flow-go/module"
    10  )
    11  
    12  var _ module.ExecutionStateIndexerMetrics = (*ExecutionStateIndexerCollector)(nil)
    13  
    14  type ExecutionStateIndexerCollector struct {
    15  	indexDuration        prometheus.Histogram
    16  	highestIndexedHeight prometheus.Gauge
    17  
    18  	indexedEvents             prometheus.Counter
    19  	indexedRegisters          prometheus.Counter
    20  	indexedTransactionResults prometheus.Counter
    21  	reindexedHeightCount      prometheus.Counter
    22  }
    23  
    24  func NewExecutionStateIndexerCollector() module.ExecutionStateIndexerMetrics {
    25  	indexDuration := promauto.NewHistogram(prometheus.HistogramOpts{
    26  		Namespace: namespaceAccess,
    27  		Subsystem: subsystemExecutionStateIndexer,
    28  		Name:      "index_duration_ms",
    29  		Help:      "the duration of execution state indexing operation",
    30  		Buckets:   []float64{1, 5, 10, 50, 100},
    31  	})
    32  
    33  	highestIndexedHeight := promauto.NewGauge(prometheus.GaugeOpts{
    34  		Namespace: namespaceAccess,
    35  		Subsystem: subsystemExecutionStateIndexer,
    36  		Name:      "highest_indexed_height",
    37  		Help:      "highest block height that has been indexed",
    38  	})
    39  
    40  	indexedEvents := promauto.NewCounter(prometheus.CounterOpts{
    41  		Namespace: namespaceAccess,
    42  		Subsystem: subsystemExecutionStateIndexer,
    43  		Name:      "indexed_events",
    44  		Help:      "number of events indexed",
    45  	})
    46  
    47  	indexedRegisters := promauto.NewCounter(prometheus.CounterOpts{
    48  		Namespace: namespaceAccess,
    49  		Subsystem: subsystemExecutionStateIndexer,
    50  		Name:      "indexed_registers",
    51  		Help:      "number of registers indexed",
    52  	})
    53  
    54  	indexedTransactionResults := promauto.NewCounter(prometheus.CounterOpts{
    55  		Namespace: namespaceAccess,
    56  		Subsystem: subsystemExecutionStateIndexer,
    57  		Name:      "indexed_transaction_results",
    58  		Help:      "number of transaction results indexed",
    59  	})
    60  
    61  	reindexedHeightCount := promauto.NewCounter(prometheus.CounterOpts{
    62  		Namespace: namespaceAccess,
    63  		Subsystem: subsystemExecutionStateIndexer,
    64  		Name:      "reindexed_height_count",
    65  		Help:      "number of times a previously indexed height is reindexed",
    66  	})
    67  
    68  	return &ExecutionStateIndexerCollector{
    69  		indexDuration:             indexDuration,
    70  		highestIndexedHeight:      highestIndexedHeight,
    71  		indexedEvents:             indexedEvents,
    72  		indexedRegisters:          indexedRegisters,
    73  		indexedTransactionResults: indexedTransactionResults,
    74  		reindexedHeightCount:      reindexedHeightCount,
    75  	}
    76  }
    77  
    78  // InitializeLatestHeight records the latest height that has been indexed.
    79  // This should only be used during startup. After startup, use BlockIndexed to record newly
    80  // indexed heights.
    81  func (c *ExecutionStateIndexerCollector) InitializeLatestHeight(height uint64) {
    82  	c.highestIndexedHeight.Set(float64(height))
    83  }
    84  
    85  // BlockIndexed records metrics from indexing execution data from a single block.
    86  func (c *ExecutionStateIndexerCollector) BlockIndexed(height uint64, duration time.Duration, events, registers, transactionResults int) {
    87  	c.indexDuration.Observe(float64(duration.Milliseconds()))
    88  	c.highestIndexedHeight.Set(float64(height))
    89  	c.indexedEvents.Add(float64(events))
    90  	c.indexedRegisters.Add(float64(registers))
    91  	c.indexedTransactionResults.Add(float64(transactionResults))
    92  }
    93  
    94  // BlockReindexed records that a previously indexed block was indexed again.
    95  func (c *ExecutionStateIndexerCollector) BlockReindexed() {
    96  	c.reindexedHeightCount.Inc()
    97  }