github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/metrics/inner_data.go (about)

     1  package metrics
     2  
     3  import (
     4  	"github.com/cozy/cozy-stack/pkg/consts"
     5  	"github.com/cozy/cozy-stack/pkg/couchdb"
     6  	"github.com/cozy/cozy-stack/pkg/prefixer"
     7  	"github.com/prometheus/client_golang/prometheus"
     8  )
     9  
    10  // innerDataCollector collects data from the database, like the number of cozy
    11  // instances. These data are global and collected on demand from the database.
    12  type innerDataCollector struct {
    13  	instancesCountDesc *prometheus.Desc
    14  }
    15  
    16  func (i *innerDataCollector) Describe(ch chan<- *prometheus.Desc) {
    17  	ch <- i.instancesCountDesc
    18  }
    19  
    20  func (i *innerDataCollector) Collect(ch chan<- prometheus.Metric) {
    21  	if count, err := couchdb.CountAllDocs(prefixer.GlobalPrefixer, consts.Instances); err == nil {
    22  		ch <- prometheus.MustNewConstMetric(
    23  			i.instancesCountDesc,
    24  			prometheus.CounterValue,
    25  			float64(count),
    26  		)
    27  	}
    28  }
    29  
    30  func init() {
    31  	prometheus.MustRegister(&innerDataCollector{
    32  		instancesCountDesc: prometheus.NewDesc(
    33  			prometheus.BuildFQName("inner_data", "instances", "count"), /* fqName*/
    34  			"Number of existing instances.",                            /* help */
    35  			[]string{},                                                 /* variableLabels */
    36  			prometheus.Labels{},                                        /* constLabels */
    37  		),
    38  	})
    39  }