Merge pull request #162 from prometheus/grobie/reduce-indentation

Break out event handling into its own function
This commit is contained in:
Matthias Rampke 2018-11-05 09:56:57 +00:00 committed by GitHub
commit 040760ec22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -253,6 +253,8 @@ func escapeMetricName(metricName string) string {
return metricName return metricName
} }
// Listen handles all events sent to the given channel sequentially. It
// terminates when the channel is closed.
func (b *Exporter) Listen(e <-chan Events) { func (b *Exporter) Listen(e <-chan Events) {
for { for {
events, ok := <-e events, ok := <-e
@ -261,24 +263,29 @@ func (b *Exporter) Listen(e <-chan Events) {
return return
} }
for _, event := range events { for _, event := range events {
var help string b.handleEvent(event)
metricName := "" }
prometheusLabels := event.Labels() }
}
// handleEvent processes a single Event according to the configured mapping.
func (b *Exporter) handleEvent(event Event) {
mapping, labels, present := b.mapper.GetMapping(event.MetricName(), event.MetricType()) mapping, labels, present := b.mapper.GetMapping(event.MetricName(), event.MetricType())
if mapping == nil { if mapping == nil {
mapping = &mapper.MetricMapping{} mapping = &mapper.MetricMapping{}
} }
if mapping.Action == mapper.ActionTypeDrop { if mapping.Action == mapper.ActionTypeDrop {
continue return
} }
if mapping.HelpText == "" { help := defaultHelp
help = defaultHelp if mapping.HelpText != "" {
} else {
help = mapping.HelpText help = mapping.HelpText
} }
metricName := ""
prometheusLabels := event.Labels()
if present { if present {
metricName = escapeMetricName(mapping.Name) metricName = escapeMetricName(mapping.Name)
for label, value := range labels { for label, value := range labels {
@ -296,7 +303,7 @@ func (b *Exporter) Listen(e <-chan Events) {
if event.Value() < 0.0 { if event.Value() < 0.0 {
log.Debugf("Counter %q is: '%f' (counter must be non-negative value)", metricName, event.Value()) log.Debugf("Counter %q is: '%f' (counter must be non-negative value)", metricName, event.Value())
eventStats.WithLabelValues("illegal_negative_counter").Inc() eventStats.WithLabelValues("illegal_negative_counter").Inc()
continue return
} }
counter, err := b.Counters.Get( counter, err := b.Counters.Get(
@ -381,8 +388,6 @@ func (b *Exporter) Listen(e <-chan Events) {
log.Debugln("Unsupported event type") log.Debugln("Unsupported event type")
eventStats.WithLabelValues("illegal").Inc() eventStats.WithLabelValues("illegal").Inc()
} }
}
}
} }
func NewExporter(mapper *mapper.MetricMapper) *Exporter { func NewExporter(mapper *mapper.MetricMapper) *Exporter {