Skip to content

Commit

Permalink
Don't crash on conflicting metric names
Browse files Browse the repository at this point in the history
This patch simply moves the error message from a log.Fatalf() to a
log.Errorf() to continue on.

Fixes #63
  • Loading branch information
jacksontj committed Jul 18, 2017
1 parent 22520f4 commit d9aa6e2
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
counter, ok := c.Elements[hash]
if !ok {
Expand All @@ -80,10 +80,10 @@ func (c *CounterContainer) Get(metricName string, labels prometheus.Labels) prom
})
c.Elements[hash] = counter
if err := prometheus.Register(counter); err != nil {
log.Fatalf(regErrF, metricName, err)
return nil, err
}
}
return counter
return counter, nil
}

type GaugeContainer struct {
Expand All @@ -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)
gauge, ok := c.Elements[hash]
if !ok {
Expand All @@ -107,10 +107,10 @@ func (c *GaugeContainer) Get(metricName string, labels prometheus.Labels) promet
})
c.Elements[hash] = gauge
if err := prometheus.Register(gauge); err != nil {
log.Fatalf(regErrF, metricName, err)
return nil, err
}
}
return gauge
return gauge, nil
}

type SummaryContainer struct {
Expand All @@ -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)
summary, ok := c.Elements[hash]
if !ok {
Expand All @@ -135,10 +135,10 @@ func (c *SummaryContainer) Get(metricName string, labels prometheus.Labels) prom
})
c.Elements[hash] = summary
if err := prometheus.Register(summary); err != nil {
log.Fatalf(regErrF, metricName, err)
return nil, err
}
}
return summary
return summary, nil
}

type Event interface {
Expand Down Expand Up @@ -240,37 +240,48 @@ func (b *Exporter) Listen(e <-chan Events) {
continue
}

counter := b.Counters.Get(
counter, err := b.Counters.Get(
b.suffix(metricName, "counter"),
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:
gauge := b.Gauges.Get(
gauge, err := b.Gauges.Get(
b.suffix(metricName, "gauge"),
prometheusLabels,
)

if ev.relative {
gauge.Add(event.Value())
if err == nil {
if ev.relative {
gauge.Add(event.Value())
} else {
gauge.Set(event.Value())
}

eventStats.WithLabelValues("gauge").Inc()
} else {
gauge.Set(event.Value())
log.Errorf(regErrF, metricName, err)
}

eventStats.WithLabelValues("gauge").Inc()

case *TimerEvent:
summary := b.Summaries.Get(
summary, err := b.Summaries.Get(
b.suffix(metricName, "timer"),
prometheusLabels,
)
summary.Observe(event.Value())
if err == nil {
summary.Observe(event.Value())

eventStats.WithLabelValues("timer").Inc()
eventStats.WithLabelValues("timer").Inc()
} else {
log.Errorf(regErrF, metricName, err)
}

default:
log.Errorln("Unsupported event type")
Expand Down

0 comments on commit d9aa6e2

Please sign in to comment.