github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/dbaccessor/metrics.go (about)

     1  // Copyright 2023 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package dbaccessor
     5  
     6  import "github.com/prometheus/client_golang/prometheus"
     7  
     8  const (
     9  	dbaccessorMetricsNamespace   = "juju"
    10  	dbaccessorSubsystemNamespace = "db"
    11  )
    12  
    13  // Collector defines a prometheus collector for the dbaccessor.
    14  type Collector struct {
    15  	DBRequests  *prometheus.GaugeVec
    16  	DBDuration  *prometheus.HistogramVec
    17  	TxnRequests *prometheus.CounterVec
    18  }
    19  
    20  // NewMetricsCollector returns a new Collector.
    21  func NewMetricsCollector() *Collector {
    22  	return &Collector{
    23  		DBRequests: prometheus.NewGaugeVec(prometheus.GaugeOpts{
    24  			Namespace: dbaccessorMetricsNamespace,
    25  			Subsystem: dbaccessorSubsystemNamespace,
    26  			Name:      "requests_total",
    27  			Help:      "Number of active db requests.",
    28  		}, []string{"namespace"}),
    29  		DBDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
    30  			Namespace: dbaccessorMetricsNamespace,
    31  			Subsystem: dbaccessorSubsystemNamespace,
    32  			Name:      "duration_seconds",
    33  			Help:      "Total time spent in db requests.",
    34  		}, []string{"namespace", "result"}),
    35  		TxnRequests: prometheus.NewCounterVec(prometheus.CounterOpts{
    36  			Namespace: dbaccessorMetricsNamespace,
    37  			Subsystem: dbaccessorSubsystemNamespace,
    38  			Name:      "txn_requests_total",
    39  			Help:      "Total number of txn requests including retries.",
    40  		}, []string{"namespace"}),
    41  	}
    42  }
    43  
    44  // Describe is part of the prometheus.Collector interface.
    45  func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
    46  	c.DBRequests.Describe(ch)
    47  	c.DBDuration.Describe(ch)
    48  	c.TxnRequests.Describe(ch)
    49  }
    50  
    51  // Collect is part of the prometheus.Collector interface.
    52  func (c *Collector) Collect(ch chan<- prometheus.Metric) {
    53  	c.DBRequests.Collect(ch)
    54  	c.DBDuration.Collect(ch)
    55  	c.TxnRequests.Collect(ch)
    56  }