github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/node/prometheus.go (about)

     1  package node
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/prometheus/client_golang/prometheus"
     8  )
     9  
    10  type bcnodeCollector struct {
    11  	DiskBeaconchainBytesTotal *prometheus.Desc
    12  	dbPath                    string
    13  }
    14  
    15  func newBeaconNodePromCollector(dbPath string) (*bcnodeCollector, error) {
    16  	namespace := "bcnode"
    17  	c := &bcnodeCollector{
    18  		DiskBeaconchainBytesTotal: prometheus.NewDesc(
    19  			prometheus.BuildFQName(namespace, "", "disk_beaconchain_bytes_total"),
    20  			"Total hard disk space used by the beaconchain database, in bytes.",
    21  			nil,
    22  			nil,
    23  		),
    24  		dbPath: dbPath,
    25  	}
    26  	_, err := c.getCurrentDbBytes()
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	return c, prometheus.Register(c)
    31  }
    32  
    33  func (bc *bcnodeCollector) Describe(ch chan<- *prometheus.Desc) {
    34  	ch <- bc.DiskBeaconchainBytesTotal
    35  }
    36  
    37  func (bc *bcnodeCollector) Collect(ch chan<- prometheus.Metric) {
    38  	dbBytes, err := bc.getCurrentDbBytes()
    39  	if err != nil {
    40  		log.Warn(err)
    41  		return
    42  	}
    43  
    44  	ch <- prometheus.MustNewConstMetric(
    45  		bc.DiskBeaconchainBytesTotal,
    46  		prometheus.GaugeValue,
    47  		dbBytes,
    48  	)
    49  }
    50  
    51  func (bc *bcnodeCollector) getCurrentDbBytes() (float64, error) {
    52  	fs, err := os.Stat(bc.dbPath)
    53  	if err != nil {
    54  		return 0, fmt.Errorf("could not collect database file size for prometheus, path=%s, err=%s", bc.dbPath, err)
    55  	}
    56  	return float64(fs.Size()), nil
    57  }
    58  
    59  func (bc *bcnodeCollector) unregister() {
    60  	prometheus.Unregister(bc)
    61  }