|
0 |
package pcp
|
|
1 |
|
|
2 |
import (
|
|
3 |
"github.com/go-kit/kit/metrics"
|
|
4 |
"github.com/performancecopilot/speed"
|
|
5 |
)
|
|
6 |
|
|
7 |
// Counter implements metrics.Counter via a single dimensional speed.Counter
|
|
8 |
// for now, see https://github.com/performancecopilot/speed/issues/32
|
|
9 |
type Counter struct {
|
|
10 |
c speed.Counter
|
|
11 |
}
|
|
12 |
|
|
13 |
// NewCounter creates a new Counter
|
|
14 |
func NewCounter(name string) *Counter {
|
|
15 |
c, err := speed.NewPCPCounter(0, name)
|
|
16 |
if err != nil {
|
|
17 |
panic(err)
|
|
18 |
}
|
|
19 |
return &Counter{c}
|
|
20 |
}
|
|
21 |
|
|
22 |
// With is a no-op.
|
|
23 |
func (c *Counter) With(labelValues ...string) metrics.Counter { return c }
|
|
24 |
|
|
25 |
// Add implements Counter.
|
|
26 |
// speed Counters only take int64
|
|
27 |
// if it is important, instead use speed.SingletonMetric with DoubleType, CounterSemantics and OneUnit
|
|
28 |
// but that will mean this will need a mutex, to be safe
|
|
29 |
func (c *Counter) Add(delta float64) { c.c.Inc(int64(delta)) }
|
|
30 |
|
|
31 |
////////////////////////////////////////////////////////////////////////////////////////
|
|
32 |
|
|
33 |
// Gauge implements metrics.Gauge
|
|
34 |
// also singleton for now, for same reasons as Counter
|
|
35 |
type Gauge struct {
|
|
36 |
g speed.Gauge
|
|
37 |
}
|
|
38 |
|
|
39 |
// NewGauge creates a new Gauge
|
|
40 |
func NewGauge(name string) *Gauge {
|
|
41 |
g, err := speed.NewPCPGauge(0, name)
|
|
42 |
if err != nil {
|
|
43 |
panic(err)
|
|
44 |
}
|
|
45 |
return &Gauge{g}
|
|
46 |
}
|
|
47 |
|
|
48 |
// With is a no-op.
|
|
49 |
func (g *Gauge) With(labelValues ...string) metrics.Gauge { return g }
|
|
50 |
|
|
51 |
// Set sets the value of the gauge
|
|
52 |
func (g *Gauge) Set(value float64) { g.g.Set(value) }
|
|
53 |
|
|
54 |
// Add adds a value to the gauge
|
|
55 |
func (g *Gauge) Add(value float64) { g.g.Inc(value) }
|
|
56 |
|
|
57 |
////////////////////////////////////////////////////////////////////////////////////////
|
|
58 |
|
|
59 |
// Histogram wraps a PCP Histogram
|
|
60 |
type Histogram struct {
|
|
61 |
h speed.Histogram
|
|
62 |
}
|
|
63 |
|
|
64 |
// NewHistogram creates a new Histogram
|
|
65 |
// minimum observeable value is 0
|
|
66 |
// maximum observeable value is 3600000000
|
|
67 |
func NewHistogram(name string, min, max int64) *Histogram {
|
|
68 |
h, err := speed.NewPCPHistogram(name, min, max, 5)
|
|
69 |
if err != nil {
|
|
70 |
panic(err)
|
|
71 |
}
|
|
72 |
return &Histogram{h}
|
|
73 |
}
|
|
74 |
|
|
75 |
// With is a no-op.
|
|
76 |
func (h *Histogram) With(labelValues ...string) metrics.Histogram { return h }
|
|
77 |
|
|
78 |
// Observe observes a value
|
|
79 |
//
|
|
80 |
// this converts float64 value to int64, as the Histogram in speed
|
|
81 |
// is backed using codahale/hdrhistogram, which only observes int64 values
|
|
82 |
func (h *Histogram) Observe(value float64) { h.h.MustRecord(int64(value)) }
|
|
83 |
|
|
84 |
// Mean returns the mean of the values observed so far
|
|
85 |
func (h *Histogram) Mean() float64 { return h.h.Mean() }
|
|
86 |
|
|
87 |
// Percentile returns a percentile between 0 and 100
|
|
88 |
func (h *Histogram) Percentile(p float64) int64 { return h.h.Percentile(p) }
|