github.com/koko1123/flow-go-1@v0.29.6/module/metrics/chainsync.go (about)

     1  package metrics
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"github.com/prometheus/client_golang/prometheus"
     8  
     9  	"github.com/koko1123/flow-go-1/model/chainsync"
    10  )
    11  
    12  type ChainSyncCollector struct {
    13  	timeToPruned          *prometheus.HistogramVec
    14  	timeToReceived        *prometheus.HistogramVec
    15  	totalPruned           *prometheus.CounterVec
    16  	storedBlocks          *prometheus.GaugeVec
    17  	totalHeightsRequested prometheus.Counter
    18  	totalIdsRequested     prometheus.Counter
    19  }
    20  
    21  func NewChainSyncCollector() *ChainSyncCollector {
    22  	return &ChainSyncCollector{
    23  		timeToPruned: prometheus.NewHistogramVec(prometheus.HistogramOpts{
    24  			Name:      "time_to_pruned_seconds",
    25  			Namespace: namespaceChainsync,
    26  			Subsystem: subsystemSyncCore,
    27  			Help:      "the time between queueing and pruning a block in seconds",
    28  			Buckets:   []float64{.1, .25, .5, 1, 2.5, 5, 7.5, 10, 20},
    29  		}, []string{"status", "requested_by"}),
    30  		timeToReceived: prometheus.NewHistogramVec(prometheus.HistogramOpts{
    31  			Name:      "time_to_received_seconds",
    32  			Namespace: namespaceChainsync,
    33  			Subsystem: subsystemSyncCore,
    34  			Help:      "the time between queueing and receiving a block in seconds",
    35  			Buckets:   []float64{.1, .25, .5, 1, 2.5, 5, 7.5, 10, 20},
    36  		}, []string{"requested_by"}),
    37  		totalPruned: prometheus.NewCounterVec(prometheus.CounterOpts{
    38  			Name:      "blocks_pruned_total",
    39  			Namespace: namespaceChainsync,
    40  			Subsystem: subsystemSyncCore,
    41  			Help:      "the total number of blocks pruned by 'id' or 'height'",
    42  		}, []string{"requested_by"}),
    43  		storedBlocks: prometheus.NewGaugeVec(prometheus.GaugeOpts{
    44  			Name:      "blocks_stored_total",
    45  			Namespace: namespaceChainsync,
    46  			Subsystem: subsystemSyncCore,
    47  			Help:      "the total number of blocks currently stored by 'id' or 'height'",
    48  		}, []string{"requested_by"}),
    49  		totalHeightsRequested: prometheus.NewCounter(prometheus.CounterOpts{
    50  			Name:      "block_heights_requested_total",
    51  			Namespace: namespaceChainsync,
    52  			Subsystem: subsystemSyncCore,
    53  			Help:      "the total number of blocks requested by height, including retried requests for the same heights. Eg: a range of 1-10 would increase the counter by 10",
    54  		}),
    55  		totalIdsRequested: prometheus.NewCounter(prometheus.CounterOpts{
    56  			Name:      "block_ids_requested_total",
    57  			Namespace: namespaceChainsync,
    58  			Subsystem: subsystemSyncCore,
    59  			Help:      "the total number of blocks requested by id",
    60  		}),
    61  	}
    62  }
    63  
    64  func (c *ChainSyncCollector) PrunedBlockById(status *chainsync.Status) {
    65  	c.prunedBlock(status, "id")
    66  }
    67  
    68  func (c *ChainSyncCollector) PrunedBlockByHeight(status *chainsync.Status) {
    69  	c.prunedBlock(status, "height")
    70  }
    71  
    72  func (c *ChainSyncCollector) prunedBlock(status *chainsync.Status, requestedBy string) {
    73  	str := strings.ToLower(status.StatusString())
    74  
    75  	// measure the time-to-pruned
    76  	pruned := float64(time.Since(status.Queued).Milliseconds())
    77  	c.timeToPruned.With(prometheus.Labels{"status": str, "requested_by": requestedBy}).Observe(pruned)
    78  
    79  	if status.WasReceived() {
    80  		// measure the time-to-received
    81  		received := float64(status.Received.Sub(status.Queued).Milliseconds())
    82  		c.timeToReceived.With(prometheus.Labels{"requested_by": requestedBy}).Observe(received)
    83  	}
    84  }
    85  
    86  func (c *ChainSyncCollector) PrunedBlocks(totalByHeight, totalById, storedByHeight, storedById int) {
    87  	// add the total number of blocks pruned
    88  	c.totalPruned.With(prometheus.Labels{"requested_by": "id"}).Add(float64(totalById))
    89  	c.totalPruned.With(prometheus.Labels{"requested_by": "height"}).Add(float64(totalByHeight))
    90  
    91  	// update gauges
    92  	c.storedBlocks.With(prometheus.Labels{"requested_by": "id"}).Set(float64(storedById))
    93  	c.storedBlocks.With(prometheus.Labels{"requested_by": "height"}).Set(float64(storedByHeight))
    94  }
    95  
    96  func (c *ChainSyncCollector) RangeRequested(ran chainsync.Range) {
    97  	c.totalHeightsRequested.Add(float64(ran.Len()))
    98  }
    99  
   100  func (c *ChainSyncCollector) BatchRequested(batch chainsync.Batch) {
   101  	c.totalIdsRequested.Add(float64(len(batch.BlockIDs)))
   102  }