github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/keeper/metrics.go (about)

     1  package keeper
     2  
     3  import (
     4  	wasmvmtypes "github.com/CosmWasm/wasmvm/types"
     5  	"github.com/prometheus/client_golang/prometheus"
     6  )
     7  
     8  const (
     9  	labelPinned = "pinned"
    10  	labelMemory = "memory"
    11  	labelFs     = "fs"
    12  )
    13  
    14  // metricSource source of wasmvm metrics
    15  type metricSource interface {
    16  	GetMetrics() (*wasmvmtypes.Metrics, error)
    17  }
    18  
    19  var _ prometheus.Collector = (*WasmVMMetricsCollector)(nil)
    20  
    21  // WasmVMMetricsCollector custom metrics collector to be used with Prometheus
    22  type WasmVMMetricsCollector struct {
    23  	source             metricSource
    24  	CacheHitsDescr     *prometheus.Desc
    25  	CacheMissesDescr   *prometheus.Desc
    26  	CacheElementsDescr *prometheus.Desc
    27  	CacheSizeDescr     *prometheus.Desc
    28  }
    29  
    30  // NewWasmVMMetricsCollector constructor
    31  func NewWasmVMMetricsCollector(s metricSource) *WasmVMMetricsCollector {
    32  	return &WasmVMMetricsCollector{
    33  		source:             s,
    34  		CacheHitsDescr:     prometheus.NewDesc("wasmvm_cache_hits_total", "Total number of cache hits", []string{"type"}, nil),
    35  		CacheMissesDescr:   prometheus.NewDesc("wasmvm_cache_misses_total", "Total number of cache misses", nil, nil),
    36  		CacheElementsDescr: prometheus.NewDesc("wasmvm_cache_elements_total", "Total number of elements in the cache", []string{"type"}, nil),
    37  		CacheSizeDescr:     prometheus.NewDesc("wasmvm_cache_size_bytes", "Total number of elements in the cache", []string{"type"}, nil),
    38  	}
    39  }
    40  
    41  // Register registers all metrics
    42  func (p *WasmVMMetricsCollector) Register(r prometheus.Registerer) {
    43  	r.MustRegister(p)
    44  }
    45  
    46  // Describe sends the super-set of all possible descriptors of metrics
    47  func (p *WasmVMMetricsCollector) Describe(descs chan<- *prometheus.Desc) {
    48  	descs <- p.CacheHitsDescr
    49  	descs <- p.CacheMissesDescr
    50  	descs <- p.CacheElementsDescr
    51  	descs <- p.CacheSizeDescr
    52  }
    53  
    54  // Collect is called by the Prometheus registry when collecting metrics.
    55  func (p *WasmVMMetricsCollector) Collect(c chan<- prometheus.Metric) {
    56  	m, err := p.source.GetMetrics()
    57  	if err != nil {
    58  		return
    59  	}
    60  	c <- prometheus.MustNewConstMetric(p.CacheHitsDescr, prometheus.CounterValue, float64(m.HitsPinnedMemoryCache), labelPinned)
    61  	c <- prometheus.MustNewConstMetric(p.CacheHitsDescr, prometheus.CounterValue, float64(m.HitsMemoryCache), labelMemory)
    62  	c <- prometheus.MustNewConstMetric(p.CacheHitsDescr, prometheus.CounterValue, float64(m.HitsFsCache), labelFs)
    63  	c <- prometheus.MustNewConstMetric(p.CacheMissesDescr, prometheus.CounterValue, float64(m.Misses))
    64  	c <- prometheus.MustNewConstMetric(p.CacheElementsDescr, prometheus.GaugeValue, float64(m.ElementsPinnedMemoryCache), labelPinned)
    65  	c <- prometheus.MustNewConstMetric(p.CacheElementsDescr, prometheus.GaugeValue, float64(m.ElementsMemoryCache), labelMemory)
    66  	c <- prometheus.MustNewConstMetric(p.CacheSizeDescr, prometheus.GaugeValue, float64(m.SizeMemoryCache), labelMemory)
    67  	c <- prometheus.MustNewConstMetric(p.CacheSizeDescr, prometheus.GaugeValue, float64(m.SizePinnedMemoryCache), labelPinned)
    68  	// Node about fs metrics:
    69  	// The number of elements and the size of elements in the file system cache cannot easily be obtained.
    70  	// We had to either scan the whole directory of potentially thousands of files or track the values when files are added or removed.
    71  	// Such a tracking would need to be on disk such that the values are not cleared when the node is restarted.
    72  }