diff options
author | Christopher Speller <crspeller@gmail.com> | 2017-08-17 17:19:06 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-17 17:19:06 -0700 |
commit | 96eab1202717e073782ec399a4e0820cae15b1bb (patch) | |
tree | 011012982be971c7e9ef91466f026bc0956ac9a2 /vendor/github.com/armon/go-metrics/prometheus | |
parent | 2c895ee66eed626721135acfcc48254c6e3f3b29 (diff) | |
download | chat-96eab1202717e073782ec399a4e0820cae15b1bb.tar.gz chat-96eab1202717e073782ec399a4e0820cae15b1bb.tar.bz2 chat-96eab1202717e073782ec399a4e0820cae15b1bb.zip |
Updating server dependancies. (#7246)
Diffstat (limited to 'vendor/github.com/armon/go-metrics/prometheus')
-rw-r--r-- | vendor/github.com/armon/go-metrics/prometheus/prometheus.go | 72 |
1 files changed, 52 insertions, 20 deletions
diff --git a/vendor/github.com/armon/go-metrics/prometheus/prometheus.go b/vendor/github.com/armon/go-metrics/prometheus/prometheus.go index b26d27981..f31f2f9a5 100644 --- a/vendor/github.com/armon/go-metrics/prometheus/prometheus.go +++ b/vendor/github.com/armon/go-metrics/prometheus/prometheus.go @@ -2,10 +2,14 @@ package prometheus import ( + "fmt" "strings" "sync" "time" + "regexp" + + "github.com/armon/go-metrics" "github.com/prometheus/client_golang/prometheus" ) @@ -24,24 +28,42 @@ func NewPrometheusSink() (*PrometheusSink, error) { }, nil } -func (p *PrometheusSink) flattenKey(parts []string) string { - joined := strings.Join(parts, "_") - joined = strings.Replace(joined, " ", "_", -1) - joined = strings.Replace(joined, ".", "_", -1) - joined = strings.Replace(joined, "-", "_", -1) - joined = strings.Replace(joined, "=", "_", -1) - return joined +var forbiddenChars = regexp.MustCompile("[ .=\\-]") + +func (p *PrometheusSink) flattenKey(parts []string, labels []metrics.Label) (string, string) { + key := strings.Join(parts, "_") + key = forbiddenChars.ReplaceAllString(key, "_") + + hash := key + for _, label := range labels { + hash += fmt.Sprintf(";%s=%s", label.Name, label.Value) + } + + return key, hash +} + +func prometheusLabels(labels []metrics.Label) prometheus.Labels { + l := make(prometheus.Labels) + for _, label := range labels { + l[label.Name] = label.Value + } + return l } func (p *PrometheusSink) SetGauge(parts []string, val float32) { + p.SetGaugeWithLabels(parts, val, nil) +} + +func (p *PrometheusSink) SetGaugeWithLabels(parts []string, val float32, labels []metrics.Label) { p.mu.Lock() defer p.mu.Unlock() - key := p.flattenKey(parts) - g, ok := p.gauges[key] + key, hash := p.flattenKey(parts, labels) + g, ok := p.gauges[hash] if !ok { g = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: key, - Help: key, + Name: key, + Help: key, + ConstLabels: prometheusLabels(labels), }) prometheus.MustRegister(g) p.gauges[key] = g @@ -50,15 +72,20 @@ func (p *PrometheusSink) SetGauge(parts []string, val float32) { } func (p *PrometheusSink) AddSample(parts []string, val float32) { + p.AddSampleWithLabels(parts, val, nil) +} + +func (p *PrometheusSink) AddSampleWithLabels(parts []string, val float32, labels []metrics.Label) { p.mu.Lock() defer p.mu.Unlock() - key := p.flattenKey(parts) - g, ok := p.summaries[key] + key, hash := p.flattenKey(parts, labels) + g, ok := p.summaries[hash] if !ok { g = prometheus.NewSummary(prometheus.SummaryOpts{ - Name: key, - Help: key, - MaxAge: 10 * time.Second, + Name: key, + Help: key, + MaxAge: 10 * time.Second, + ConstLabels: prometheusLabels(labels), }) prometheus.MustRegister(g) p.summaries[key] = g @@ -73,14 +100,19 @@ func (p *PrometheusSink) EmitKey(key []string, val float32) { } func (p *PrometheusSink) IncrCounter(parts []string, val float32) { + p.IncrCounterWithLabels(parts, val, nil) +} + +func (p *PrometheusSink) IncrCounterWithLabels(parts []string, val float32, labels []metrics.Label) { p.mu.Lock() defer p.mu.Unlock() - key := p.flattenKey(parts) - g, ok := p.counters[key] + key, hash := p.flattenKey(parts, labels) + g, ok := p.counters[hash] if !ok { g = prometheus.NewCounter(prometheus.CounterOpts{ - Name: key, - Help: key, + Name: key, + Help: key, + ConstLabels: prometheusLabels(labels), }) prometheus.MustRegister(g) p.counters[key] = g |