...

Source file src/github.com/go-kit/kit/metrics/internal/convert/convert.go

Documentation: github.com/go-kit/kit/metrics/internal/convert

     1  // Package convert provides a way to use Counters, Histograms, or Gauges
     2  // as one of the other types
     3  package convert
     4  
     5  import "github.com/go-kit/kit/metrics"
     6  
     7  type counterHistogram struct {
     8  	c metrics.Counter
     9  }
    10  
    11  // NewCounterAsHistogram returns a Histogram that actually writes the
    12  // value on an underlying Counter
    13  func NewCounterAsHistogram(c metrics.Counter) metrics.Histogram {
    14  	return counterHistogram{c}
    15  }
    16  
    17  // With implements Histogram.
    18  func (ch counterHistogram) With(labelValues ...string) metrics.Histogram {
    19  	return counterHistogram{ch.c.With(labelValues...)}
    20  }
    21  
    22  // Observe implements histogram.
    23  func (ch counterHistogram) Observe(value float64) {
    24  	ch.c.Add(value)
    25  }
    26  
    27  type histogramCounter struct {
    28  	h metrics.Histogram
    29  }
    30  
    31  // NewHistogramAsCounter returns a Counter that actually writes the
    32  // value on an underlying Histogram
    33  func NewHistogramAsCounter(h metrics.Histogram) metrics.Counter {
    34  	return histogramCounter{h}
    35  }
    36  
    37  // With implements Counter.
    38  func (hc histogramCounter) With(labelValues ...string) metrics.Counter {
    39  	return histogramCounter{hc.h.With(labelValues...)}
    40  }
    41  
    42  // Add implements Counter.
    43  func (hc histogramCounter) Add(delta float64) {
    44  	hc.h.Observe(delta)
    45  }
    46  
    47  type counterGauge struct {
    48  	c metrics.Counter
    49  }
    50  
    51  // NewCounterAsGauge returns a Gauge that actually writes the
    52  // value on an underlying Counter
    53  func NewCounterAsGauge(c metrics.Counter) metrics.Gauge {
    54  	return counterGauge{c}
    55  }
    56  
    57  // With implements Gauge.
    58  func (cg counterGauge) With(labelValues ...string) metrics.Gauge {
    59  	return counterGauge{cg.c.With(labelValues...)}
    60  }
    61  
    62  // Set implements Gauge.
    63  func (cg counterGauge) Set(value float64) {
    64  	cg.c.Add(value)
    65  }
    66  
    67  // Add implements metrics.Gauge.
    68  func (cg counterGauge) Add(delta float64) {
    69  	cg.c.Add(delta)
    70  }
    71  
    72  type gaugeCounter struct {
    73  	g metrics.Gauge
    74  }
    75  
    76  // NewGaugeAsCounter returns a Counter that actually writes the
    77  // value on an underlying Gauge
    78  func NewGaugeAsCounter(g metrics.Gauge) metrics.Counter {
    79  	return gaugeCounter{g}
    80  }
    81  
    82  // With implements Counter.
    83  func (gc gaugeCounter) With(labelValues ...string) metrics.Counter {
    84  	return gaugeCounter{gc.g.With(labelValues...)}
    85  }
    86  
    87  // Add implements Counter.
    88  func (gc gaugeCounter) Add(delta float64) {
    89  	gc.g.Set(delta)
    90  }
    91  
    92  type histogramGauge struct {
    93  	h metrics.Histogram
    94  }
    95  
    96  // NewHistogramAsGauge returns a Gauge that actually writes the
    97  // value on an underlying Histogram
    98  func NewHistogramAsGauge(h metrics.Histogram) metrics.Gauge {
    99  	return histogramGauge{h}
   100  }
   101  
   102  // With implements Gauge.
   103  func (hg histogramGauge) With(labelValues ...string) metrics.Gauge {
   104  	return histogramGauge{hg.h.With(labelValues...)}
   105  }
   106  
   107  // Set implements Gauge.
   108  func (hg histogramGauge) Set(value float64) {
   109  	hg.h.Observe(value)
   110  }
   111  
   112  // Add implements metrics.Gauge.
   113  func (hg histogramGauge) Add(delta float64) {
   114  	hg.h.Observe(delta)
   115  }
   116  
   117  type gaugeHistogram struct {
   118  	g metrics.Gauge
   119  }
   120  
   121  // NewGaugeAsHistogram returns a Histogram that actually writes the
   122  // value on an underlying Gauge
   123  func NewGaugeAsHistogram(g metrics.Gauge) metrics.Histogram {
   124  	return gaugeHistogram{g}
   125  }
   126  
   127  // With implements Histogram.
   128  func (gh gaugeHistogram) With(labelValues ...string) metrics.Histogram {
   129  	return gaugeHistogram{gh.g.With(labelValues...)}
   130  }
   131  
   132  // Observe implements histogram.
   133  func (gh gaugeHistogram) Observe(value float64) {
   134  	gh.g.Set(value)
   135  }
   136  

View as plain text