...

Source file src/github.com/go-kit/kit/metrics/prometheus/prometheus.go

Documentation: github.com/go-kit/kit/metrics/prometheus

     1  // Package prometheus provides Prometheus implementations for metrics.
     2  // Individual metrics are mapped to their Prometheus counterparts, and
     3  // (depending on the constructor used) may be automatically registered in the
     4  // global Prometheus metrics registry.
     5  package prometheus
     6  
     7  import (
     8  	"github.com/prometheus/client_golang/prometheus"
     9  
    10  	"github.com/go-kit/kit/metrics"
    11  	"github.com/go-kit/kit/metrics/internal/lv"
    12  )
    13  
    14  // Counter implements Counter, via a Prometheus CounterVec.
    15  type Counter struct {
    16  	cv  *prometheus.CounterVec
    17  	lvs lv.LabelValues
    18  }
    19  
    20  // NewCounterFrom constructs and registers a Prometheus CounterVec,
    21  // and returns a usable Counter object.
    22  func NewCounterFrom(opts prometheus.CounterOpts, labelNames []string) *Counter {
    23  	cv := prometheus.NewCounterVec(opts, labelNames)
    24  	prometheus.MustRegister(cv)
    25  	return NewCounter(cv)
    26  }
    27  
    28  // NewCounter wraps the CounterVec and returns a usable Counter object.
    29  func NewCounter(cv *prometheus.CounterVec) *Counter {
    30  	return &Counter{
    31  		cv: cv,
    32  	}
    33  }
    34  
    35  // With implements Counter.
    36  func (c *Counter) With(labelValues ...string) metrics.Counter {
    37  	return &Counter{
    38  		cv:  c.cv,
    39  		lvs: c.lvs.With(labelValues...),
    40  	}
    41  }
    42  
    43  // Add implements Counter.
    44  func (c *Counter) Add(delta float64) {
    45  	c.cv.With(makeLabels(c.lvs...)).Add(delta)
    46  }
    47  
    48  // Gauge implements Gauge, via a Prometheus GaugeVec.
    49  type Gauge struct {
    50  	gv  *prometheus.GaugeVec
    51  	lvs lv.LabelValues
    52  }
    53  
    54  // NewGaugeFrom constructs and registers a Prometheus GaugeVec,
    55  // and returns a usable Gauge object.
    56  func NewGaugeFrom(opts prometheus.GaugeOpts, labelNames []string) *Gauge {
    57  	gv := prometheus.NewGaugeVec(opts, labelNames)
    58  	prometheus.MustRegister(gv)
    59  	return NewGauge(gv)
    60  }
    61  
    62  // NewGauge wraps the GaugeVec and returns a usable Gauge object.
    63  func NewGauge(gv *prometheus.GaugeVec) *Gauge {
    64  	return &Gauge{
    65  		gv: gv,
    66  	}
    67  }
    68  
    69  // With implements Gauge.
    70  func (g *Gauge) With(labelValues ...string) metrics.Gauge {
    71  	return &Gauge{
    72  		gv:  g.gv,
    73  		lvs: g.lvs.With(labelValues...),
    74  	}
    75  }
    76  
    77  // Set implements Gauge.
    78  func (g *Gauge) Set(value float64) {
    79  	g.gv.With(makeLabels(g.lvs...)).Set(value)
    80  }
    81  
    82  // Add is supported by Prometheus GaugeVecs.
    83  func (g *Gauge) Add(delta float64) {
    84  	g.gv.With(makeLabels(g.lvs...)).Add(delta)
    85  }
    86  
    87  // Summary implements Histogram, via a Prometheus SummaryVec. The difference
    88  // between a Summary and a Histogram is that Summaries don't require predefined
    89  // quantile buckets, but cannot be statistically aggregated.
    90  type Summary struct {
    91  	sv  *prometheus.SummaryVec
    92  	lvs lv.LabelValues
    93  }
    94  
    95  // NewSummaryFrom constructs and registers a Prometheus SummaryVec,
    96  // and returns a usable Summary object.
    97  func NewSummaryFrom(opts prometheus.SummaryOpts, labelNames []string) *Summary {
    98  	sv := prometheus.NewSummaryVec(opts, labelNames)
    99  	prometheus.MustRegister(sv)
   100  	return NewSummary(sv)
   101  }
   102  
   103  // NewSummary wraps the SummaryVec and returns a usable Summary object.
   104  func NewSummary(sv *prometheus.SummaryVec) *Summary {
   105  	return &Summary{
   106  		sv: sv,
   107  	}
   108  }
   109  
   110  // With implements Histogram.
   111  func (s *Summary) With(labelValues ...string) metrics.Histogram {
   112  	return &Summary{
   113  		sv:  s.sv,
   114  		lvs: s.lvs.With(labelValues...),
   115  	}
   116  }
   117  
   118  // Observe implements Histogram.
   119  func (s *Summary) Observe(value float64) {
   120  	s.sv.With(makeLabels(s.lvs...)).Observe(value)
   121  }
   122  
   123  // Histogram implements Histogram via a Prometheus HistogramVec. The difference
   124  // between a Histogram and a Summary is that Histograms require predefined
   125  // quantile buckets, and can be statistically aggregated.
   126  type Histogram struct {
   127  	hv  *prometheus.HistogramVec
   128  	lvs lv.LabelValues
   129  }
   130  
   131  // NewHistogramFrom constructs and registers a Prometheus HistogramVec,
   132  // and returns a usable Histogram object.
   133  func NewHistogramFrom(opts prometheus.HistogramOpts, labelNames []string) *Histogram {
   134  	hv := prometheus.NewHistogramVec(opts, labelNames)
   135  	prometheus.MustRegister(hv)
   136  	return NewHistogram(hv)
   137  }
   138  
   139  // NewHistogram wraps the HistogramVec and returns a usable Histogram object.
   140  func NewHistogram(hv *prometheus.HistogramVec) *Histogram {
   141  	return &Histogram{
   142  		hv: hv,
   143  	}
   144  }
   145  
   146  // With implements Histogram.
   147  func (h *Histogram) With(labelValues ...string) metrics.Histogram {
   148  	return &Histogram{
   149  		hv:  h.hv,
   150  		lvs: h.lvs.With(labelValues...),
   151  	}
   152  }
   153  
   154  // Observe implements Histogram.
   155  func (h *Histogram) Observe(value float64) {
   156  	h.hv.With(makeLabels(h.lvs...)).Observe(value)
   157  }
   158  
   159  func makeLabels(labelValues ...string) prometheus.Labels {
   160  	labels := prometheus.Labels{}
   161  	for i := 0; i < len(labelValues); i += 2 {
   162  		labels[labelValues[i]] = labelValues[i+1]
   163  	}
   164  	return labels
   165  }
   166  

View as plain text