github.com/MetalBlockchain/metalgo@v1.11.9/cache/metercacher/metrics.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package metercacher
     5  
     6  import (
     7  	"errors"
     8  
     9  	"github.com/prometheus/client_golang/prometheus"
    10  )
    11  
    12  const (
    13  	resultLabel = "result"
    14  	hitResult   = "hit"
    15  	missResult  = "miss"
    16  )
    17  
    18  var (
    19  	resultLabels = []string{resultLabel}
    20  	hitLabels    = prometheus.Labels{
    21  		resultLabel: hitResult,
    22  	}
    23  	missLabels = prometheus.Labels{
    24  		resultLabel: missResult,
    25  	}
    26  )
    27  
    28  type metrics struct {
    29  	getCount *prometheus.CounterVec
    30  	getTime  *prometheus.GaugeVec
    31  
    32  	putCount prometheus.Counter
    33  	putTime  prometheus.Gauge
    34  
    35  	len           prometheus.Gauge
    36  	portionFilled prometheus.Gauge
    37  }
    38  
    39  func newMetrics(
    40  	namespace string,
    41  	reg prometheus.Registerer,
    42  ) (*metrics, error) {
    43  	m := &metrics{
    44  		getCount: prometheus.NewCounterVec(
    45  			prometheus.CounterOpts{
    46  				Namespace: namespace,
    47  				Name:      "get_count",
    48  				Help:      "number of get calls",
    49  			},
    50  			resultLabels,
    51  		),
    52  		getTime: prometheus.NewGaugeVec(
    53  			prometheus.GaugeOpts{
    54  				Namespace: namespace,
    55  				Name:      "get_time",
    56  				Help:      "time spent (ns) in get calls",
    57  			},
    58  			resultLabels,
    59  		),
    60  		putCount: prometheus.NewCounter(prometheus.CounterOpts{
    61  			Namespace: namespace,
    62  			Name:      "put_count",
    63  			Help:      "number of put calls",
    64  		}),
    65  		putTime: prometheus.NewGauge(prometheus.GaugeOpts{
    66  			Namespace: namespace,
    67  			Name:      "put_time",
    68  			Help:      "time spent (ns) in put calls",
    69  		}),
    70  		len: prometheus.NewGauge(prometheus.GaugeOpts{
    71  			Namespace: namespace,
    72  			Name:      "len",
    73  			Help:      "number of entries",
    74  		}),
    75  		portionFilled: prometheus.NewGauge(prometheus.GaugeOpts{
    76  			Namespace: namespace,
    77  			Name:      "portion_filled",
    78  			Help:      "fraction of cache filled",
    79  		}),
    80  	}
    81  	return m, errors.Join(
    82  		reg.Register(m.getCount),
    83  		reg.Register(m.getTime),
    84  		reg.Register(m.putCount),
    85  		reg.Register(m.putTime),
    86  		reg.Register(m.len),
    87  		reg.Register(m.portionFilled),
    88  	)
    89  }