forked from mirrors/statsd_exporter
Merge pull request #162 from prometheus/grobie/reduce-indentation
Break out event handling into its own function
This commit is contained in:
commit
040760ec22
1 changed files with 125 additions and 120 deletions
25
exporter.go
25
exporter.go
|
@ -253,6 +253,8 @@ func escapeMetricName(metricName string) string {
|
|||
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) {
|
||||
for {
|
||||
events, ok := <-e
|
||||
|
@ -261,24 +263,29 @@ func (b *Exporter) Listen(e <-chan Events) {
|
|||
return
|
||||
}
|
||||
for _, event := range events {
|
||||
var help string
|
||||
metricName := ""
|
||||
prometheusLabels := event.Labels()
|
||||
b.handleEvent(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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())
|
||||
if mapping == nil {
|
||||
mapping = &mapper.MetricMapping{}
|
||||
}
|
||||
|
||||
if mapping.Action == mapper.ActionTypeDrop {
|
||||
continue
|
||||
return
|
||||
}
|
||||
|
||||
if mapping.HelpText == "" {
|
||||
help = defaultHelp
|
||||
} else {
|
||||
help := defaultHelp
|
||||
if mapping.HelpText != "" {
|
||||
help = mapping.HelpText
|
||||
}
|
||||
|
||||
metricName := ""
|
||||
prometheusLabels := event.Labels()
|
||||
if present {
|
||||
metricName = escapeMetricName(mapping.Name)
|
||||
for label, value := range labels {
|
||||
|
@ -296,7 +303,7 @@ func (b *Exporter) Listen(e <-chan Events) {
|
|||
if event.Value() < 0.0 {
|
||||
log.Debugf("Counter %q is: '%f' (counter must be non-negative value)", metricName, event.Value())
|
||||
eventStats.WithLabelValues("illegal_negative_counter").Inc()
|
||||
continue
|
||||
return
|
||||
}
|
||||
|
||||
counter, err := b.Counters.Get(
|
||||
|
@ -382,8 +389,6 @@ func (b *Exporter) Listen(e <-chan Events) {
|
|||
eventStats.WithLabelValues("illegal").Inc()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewExporter(mapper *mapper.MetricMapper) *Exporter {
|
||||
return &Exporter{
|
||||
|
|
Loading…
Reference in a new issue