OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / go-kit / kit / metrics / multi / multi.go
1 // Package multi provides adapters that send observations to multiple metrics
2 // simultaneously. This is useful if your service needs to emit to multiple
3 // instrumentation systems at the same time, for example if your organization is
4 // transitioning from one system to another.
5 package multi
6
7 import "github.com/go-kit/kit/metrics"
8
9 // Counter collects multiple individual counters and treats them as a unit.
10 type Counter []metrics.Counter
11
12 // NewCounter returns a multi-counter, wrapping the passed counters.
13 func NewCounter(c ...metrics.Counter) Counter {
14         return Counter(c)
15 }
16
17 // Add implements counter.
18 func (c Counter) Add(delta float64) {
19         for _, counter := range c {
20                 counter.Add(delta)
21         }
22 }
23
24 // With implements counter.
25 func (c Counter) With(labelValues ...string) metrics.Counter {
26         next := make(Counter, len(c))
27         for i := range c {
28                 next[i] = c[i].With(labelValues...)
29         }
30         return next
31 }
32
33 // Gauge collects multiple individual gauges and treats them as a unit.
34 type Gauge []metrics.Gauge
35
36 // NewGauge returns a multi-gauge, wrapping the passed gauges.
37 func NewGauge(g ...metrics.Gauge) Gauge {
38         return Gauge(g)
39 }
40
41 // Set implements Gauge.
42 func (g Gauge) Set(value float64) {
43         for _, gauge := range g {
44                 gauge.Set(value)
45         }
46 }
47
48 // With implements gauge.
49 func (g Gauge) With(labelValues ...string) metrics.Gauge {
50         next := make(Gauge, len(g))
51         for i := range g {
52                 next[i] = g[i].With(labelValues...)
53         }
54         return next
55 }
56
57 // Add implements metrics.Gauge.
58 func (g Gauge) Add(delta float64) {
59         for _, gauge := range g {
60                 gauge.Add(delta)
61         }
62 }
63
64 // Histogram collects multiple individual histograms and treats them as a unit.
65 type Histogram []metrics.Histogram
66
67 // NewHistogram returns a multi-histogram, wrapping the passed histograms.
68 func NewHistogram(h ...metrics.Histogram) Histogram {
69         return Histogram(h)
70 }
71
72 // Observe implements Histogram.
73 func (h Histogram) Observe(value float64) {
74         for _, histogram := range h {
75                 histogram.Observe(value)
76         }
77 }
78
79 // With implements histogram.
80 func (h Histogram) With(labelValues ...string) metrics.Histogram {
81         next := make(Histogram, len(h))
82         for i := range h {
83                 next[i] = h[i].With(labelValues...)
84         }
85         return next
86 }