Don't crash on conflicting metric names

This patch simply moves the error message from a log.Fatalf() to a
log.Errorf() to continue on.

Fixes #63
This commit is contained in:
Thomas Jackson 2017-07-18 08:42:12 -07:00
parent 22520f4c7b
commit d9aa6e2867

View file

@ -69,7 +69,7 @@ func NewCounterContainer() *CounterContainer {
} }
} }
func (c *CounterContainer) Get(metricName string, labels prometheus.Labels) prometheus.Counter { func (c *CounterContainer) Get(metricName string, labels prometheus.Labels) (prometheus.Counter, error) {
hash := hashNameAndLabels(metricName, labels) hash := hashNameAndLabels(metricName, labels)
counter, ok := c.Elements[hash] counter, ok := c.Elements[hash]
if !ok { if !ok {
@ -80,10 +80,10 @@ func (c *CounterContainer) Get(metricName string, labels prometheus.Labels) prom
}) })
c.Elements[hash] = counter c.Elements[hash] = counter
if err := prometheus.Register(counter); err != nil { if err := prometheus.Register(counter); err != nil {
log.Fatalf(regErrF, metricName, err) return nil, err
} }
} }
return counter return counter, nil
} }
type GaugeContainer struct { type GaugeContainer struct {
@ -96,7 +96,7 @@ func NewGaugeContainer() *GaugeContainer {
} }
} }
func (c *GaugeContainer) Get(metricName string, labels prometheus.Labels) prometheus.Gauge { func (c *GaugeContainer) Get(metricName string, labels prometheus.Labels) (prometheus.Gauge, error) {
hash := hashNameAndLabels(metricName, labels) hash := hashNameAndLabels(metricName, labels)
gauge, ok := c.Elements[hash] gauge, ok := c.Elements[hash]
if !ok { if !ok {
@ -107,10 +107,10 @@ func (c *GaugeContainer) Get(metricName string, labels prometheus.Labels) promet
}) })
c.Elements[hash] = gauge c.Elements[hash] = gauge
if err := prometheus.Register(gauge); err != nil { if err := prometheus.Register(gauge); err != nil {
log.Fatalf(regErrF, metricName, err) return nil, err
} }
} }
return gauge return gauge, nil
} }
type SummaryContainer struct { type SummaryContainer struct {
@ -123,7 +123,7 @@ func NewSummaryContainer() *SummaryContainer {
} }
} }
func (c *SummaryContainer) Get(metricName string, labels prometheus.Labels) prometheus.Summary { func (c *SummaryContainer) Get(metricName string, labels prometheus.Labels) (prometheus.Summary, error) {
hash := hashNameAndLabels(metricName, labels) hash := hashNameAndLabels(metricName, labels)
summary, ok := c.Elements[hash] summary, ok := c.Elements[hash]
if !ok { if !ok {
@ -135,10 +135,10 @@ func (c *SummaryContainer) Get(metricName string, labels prometheus.Labels) prom
}) })
c.Elements[hash] = summary c.Elements[hash] = summary
if err := prometheus.Register(summary); err != nil { if err := prometheus.Register(summary); err != nil {
log.Fatalf(regErrF, metricName, err) return nil, err
} }
} }
return summary return summary, nil
} }
type Event interface { type Event interface {
@ -240,21 +240,25 @@ func (b *Exporter) Listen(e <-chan Events) {
continue continue
} }
counter := b.Counters.Get( counter, err := b.Counters.Get(
b.suffix(metricName, "counter"), b.suffix(metricName, "counter"),
prometheusLabels, prometheusLabels,
) )
if err == nil {
counter.Add(event.Value()) counter.Add(event.Value())
eventStats.WithLabelValues("counter").Inc() eventStats.WithLabelValues("counter").Inc()
} else {
log.Errorf(regErrF, metricName, err)
}
case *GaugeEvent: case *GaugeEvent:
gauge := b.Gauges.Get( gauge, err := b.Gauges.Get(
b.suffix(metricName, "gauge"), b.suffix(metricName, "gauge"),
prometheusLabels, prometheusLabels,
) )
if err == nil {
if ev.relative { if ev.relative {
gauge.Add(event.Value()) gauge.Add(event.Value())
} else { } else {
@ -262,15 +266,22 @@ func (b *Exporter) Listen(e <-chan Events) {
} }
eventStats.WithLabelValues("gauge").Inc() eventStats.WithLabelValues("gauge").Inc()
} else {
log.Errorf(regErrF, metricName, err)
}
case *TimerEvent: case *TimerEvent:
summary := b.Summaries.Get( summary, err := b.Summaries.Get(
b.suffix(metricName, "timer"), b.suffix(metricName, "timer"),
prometheusLabels, prometheusLabels,
) )
if err == nil {
summary.Observe(event.Value()) summary.Observe(event.Value())
eventStats.WithLabelValues("timer").Inc() eventStats.WithLabelValues("timer").Inc()
} else {
log.Errorf(regErrF, metricName, err)
}
default: default:
log.Errorln("Unsupported event type") log.Errorln("Unsupported event type")