Vendor in fixed go-metrics

This commit is contained in:
Alex Dadgar 2017-11-16 10:42:49 -08:00
parent 05b1588cea
commit 7e509e24ba
3 changed files with 97 additions and 25 deletions

View File

@ -70,7 +70,7 @@ func NewIntervalMetrics(intv time.Time) *IntervalMetrics {
// about a sample // about a sample
type AggregateSample struct { type AggregateSample struct {
Count int // The count of emitted pairs Count int // The count of emitted pairs
Rate float64 `json:"-"` // The count of emitted pairs per time unit (usually 1 second) Rate float64 // The values rate per time unit (usually 1 second)
Sum float64 // The sum of values Sum float64 // The sum of values
SumSq float64 `json:"-"` // The sum of squared values SumSq float64 `json:"-"` // The sum of squared values
Min float64 // Minimum value Min float64 // Minimum value
@ -107,7 +107,7 @@ func (a *AggregateSample) Ingest(v float64, rateDenom float64) {
if v > a.Max || a.Count == 1 { if v > a.Max || a.Count == 1 {
a.Max = v a.Max = v
} }
a.Rate = float64(a.Count) / rateDenom a.Rate = float64(a.Sum) / rateDenom
a.LastUpdated = time.Now() a.LastUpdated = time.Now()
} }

View File

@ -13,19 +13,91 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
var (
// DefaultPrometheusOpts is the default set of options used when creating a
// PrometheusSink.
DefaultPrometheusOpts = PrometheusOpts{
Expiration: 60 * time.Second,
}
)
// PrometheusOpts is used to configure the Prometheus Sink
type PrometheusOpts struct {
// Expiration is the duration a metric is valid for, after which it will be
// untracked. If the value is zero, a metric is never expired.
Expiration time.Duration
}
type PrometheusSink struct { type PrometheusSink struct {
mu sync.Mutex mu sync.Mutex
gauges map[string]prometheus.Gauge gauges map[string]prometheus.Gauge
summaries map[string]prometheus.Summary summaries map[string]prometheus.Summary
counters map[string]prometheus.Counter counters map[string]prometheus.Counter
updates map[string]time.Time
expiration time.Duration
} }
// NewPrometheusSink creates a new PrometheusSink using the default options.
func NewPrometheusSink() (*PrometheusSink, error) { func NewPrometheusSink() (*PrometheusSink, error) {
return &PrometheusSink{ return NewPrometheusSinkFrom(DefaultPrometheusOpts)
}
// NewPrometheusSinkFrom creates a new PrometheusSink using the passed options.
func NewPrometheusSinkFrom(opts PrometheusOpts) (*PrometheusSink, error) {
sink := &PrometheusSink{
gauges: make(map[string]prometheus.Gauge), gauges: make(map[string]prometheus.Gauge),
summaries: make(map[string]prometheus.Summary), summaries: make(map[string]prometheus.Summary),
counters: make(map[string]prometheus.Counter), counters: make(map[string]prometheus.Counter),
}, nil updates: make(map[string]time.Time),
expiration: opts.Expiration,
}
return sink, prometheus.Register(sink)
}
// Describe is needed to meet the Collector interface.
func (p *PrometheusSink) Describe(c chan<- *prometheus.Desc) {
// We must emit some description otherwise an error is returned. This
// description isn't shown to the user!
prometheus.NewGauge(prometheus.GaugeOpts{Name: "Dummy", Help: "Dummy"}).Describe(c)
}
// Collect meets the collection interface and allows us to enforce our expiration
// logic to clean up ephemeral metrics if their value haven't been set for a
// duration exceeding our allowed expiration time.
func (p *PrometheusSink) Collect(c chan<- prometheus.Metric) {
p.mu.Lock()
defer p.mu.Unlock()
expire := p.expiration != 0
now := time.Now()
for k, v := range p.gauges {
last := p.updates[k]
if expire && last.Add(p.expiration).Before(now) {
delete(p.updates, k)
delete(p.gauges, k)
} else {
v.Collect(c)
}
}
for k, v := range p.summaries {
last := p.updates[k]
if expire && last.Add(p.expiration).Before(now) {
delete(p.updates, k)
delete(p.summaries, k)
} else {
v.Collect(c)
}
}
for k, v := range p.counters {
last := p.updates[k]
if expire && last.Add(p.expiration).Before(now) {
delete(p.updates, k)
delete(p.counters, k)
} else {
v.Collect(c)
}
}
} }
var forbiddenChars = regexp.MustCompile("[ .=\\-]") var forbiddenChars = regexp.MustCompile("[ .=\\-]")
@ -65,10 +137,10 @@ func (p *PrometheusSink) SetGaugeWithLabels(parts []string, val float32, labels
Help: key, Help: key,
ConstLabels: prometheusLabels(labels), ConstLabels: prometheusLabels(labels),
}) })
prometheus.MustRegister(g)
p.gauges[hash] = g p.gauges[hash] = g
} }
g.Set(float64(val)) g.Set(float64(val))
p.updates[hash] = time.Now()
} }
func (p *PrometheusSink) AddSample(parts []string, val float32) { func (p *PrometheusSink) AddSample(parts []string, val float32) {
@ -87,10 +159,10 @@ func (p *PrometheusSink) AddSampleWithLabels(parts []string, val float32, labels
MaxAge: 10 * time.Second, MaxAge: 10 * time.Second,
ConstLabels: prometheusLabels(labels), ConstLabels: prometheusLabels(labels),
}) })
prometheus.MustRegister(g)
p.summaries[hash] = g p.summaries[hash] = g
} }
g.Observe(float64(val)) g.Observe(float64(val))
p.updates[hash] = time.Now()
} }
// EmitKey is not implemented. Prometheus doesnt offer a type for which an // EmitKey is not implemented. Prometheus doesnt offer a type for which an
@ -114,8 +186,8 @@ func (p *PrometheusSink) IncrCounterWithLabels(parts []string, val float32, labe
Help: key, Help: key,
ConstLabels: prometheusLabels(labels), ConstLabels: prometheusLabels(labels),
}) })
prometheus.MustRegister(g)
p.counters[hash] = g p.counters[hash] = g
} }
g.Add(float64(val)) g.Add(float64(val))
p.updates[hash] = time.Now()
} }

20
vendor/vendor.json vendored
View File

@ -88,28 +88,28 @@
"revision": "bbbad097214e2918d8543d5201d12bfd7bca254d" "revision": "bbbad097214e2918d8543d5201d12bfd7bca254d"
}, },
{ {
"checksumSHA1": "0et4hA6AYqZCgYiY+c6Z17t3k3k=", "checksumSHA1": "xp/2s4XclLL17DThGBI7jXZ4Crs=",
"path": "github.com/armon/go-metrics", "path": "github.com/armon/go-metrics",
"revision": "023a4bbe4bb9bfb23ee7e1afc8d0abad217641f3", "revision": "6c3acc97c61d04290a8ba2e54640151f54c1546a",
"revisionTime": "2017-08-09T01:16:44Z" "revisionTime": "2017-11-16T18:41:20Z"
}, },
{ {
"checksumSHA1": "xCsGGM9TKBogZDfSN536KtQdLko=", "checksumSHA1": "xCsGGM9TKBogZDfSN536KtQdLko=",
"path": "github.com/armon/go-metrics/circonus", "path": "github.com/armon/go-metrics/circonus",
"revision": "023a4bbe4bb9bfb23ee7e1afc8d0abad217641f3", "revision": "6c3acc97c61d04290a8ba2e54640151f54c1546a",
"revisionTime": "2017-08-09T01:16:44Z" "revisionTime": "2017-11-16T18:41:20Z"
}, },
{ {
"checksumSHA1": "Dt0n1sSivvvdZQdzc4Hu/yOG+T0=", "checksumSHA1": "Dt0n1sSivvvdZQdzc4Hu/yOG+T0=",
"path": "github.com/armon/go-metrics/datadog", "path": "github.com/armon/go-metrics/datadog",
"revision": "023a4bbe4bb9bfb23ee7e1afc8d0abad217641f3", "revision": "6c3acc97c61d04290a8ba2e54640151f54c1546a",
"revisionTime": "2017-08-09T01:16:44Z" "revisionTime": "2017-11-16T18:41:20Z"
}, },
{ {
"checksumSHA1": "NER1U5W8xgC+tAxVUuEckTffFsE=", "checksumSHA1": "XfPPXw55zKziOWnZbkEGEJ96O9c=",
"path": "github.com/armon/go-metrics/prometheus", "path": "github.com/armon/go-metrics/prometheus",
"revision": "0a12dc6f6b9da6da644031a1b9b5a85478c5ee27", "revision": "6c3acc97c61d04290a8ba2e54640151f54c1546a",
"revisionTime": "2017-09-13T18:48:37Z" "revisionTime": "2017-11-16T18:41:20Z"
}, },
{ {
"checksumSHA1": "gNO0JNpLzYOdInGeq7HqMZUzx9M=", "checksumSHA1": "gNO0JNpLzYOdInGeq7HqMZUzx9M=",