mirror of
https://github.com/prometheus/statsd_exporter.git
synced 2025-01-27 06:38:08 +00:00
Break out event handling into its own function
It's idiomatic in go to keep code indentation due to nested loops to a minimum. Decoupling the events handling function into its own function makes it easier to read the code and test the behavior in isolation of the channel handling. It's now also easily possible to process events in parallel without having to touch actual business code. Signed-off-by: Tobias Schmidt <tobidt@gmail.com>
This commit is contained in:
parent
3b846b33a8
commit
ab844a3f63
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(
|
||||
|
@ -381,8 +388,6 @@ func (b *Exporter) Listen(e <-chan Events) {
|
|||
log.Debugln("Unsupported event type")
|
||||
eventStats.WithLabelValues("illegal").Inc()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewExporter(mapper *mapper.MetricMapper) *Exporter {
|
||||
|
|
Loading…
Reference in a new issue