From b467a537d5e8474ed586d0990308304d6654078c Mon Sep 17 00:00:00 2001 From: Brian Akins Date: Wed, 26 Jul 2017 14:50:29 -0400 Subject: [PATCH] Validate timer type while reading config --- exporter.go | 17 +++++++++++------ mapper.go | 12 +++++++----- timer.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 timer.go diff --git a/exporter.go b/exporter.go index 4577730..c00a4a8 100644 --- a/exporter.go +++ b/exporter.go @@ -309,15 +309,16 @@ func (b *Exporter) Listen(e <-chan Events) { } case *TimerEvent: - timerType := "" + t := timerTypeDefault if mapping != nil { - timerType = mapping.TimerType + t = mapping.TimerType } - if timerType == "" { - timerType = b.mapper.Defaults.TimerType + if t == timerTypeDefault { + t = b.mapper.Defaults.TimerType } - if timerType == "histogram" { + switch t { + case "histogram": histogram, err := b.Histograms.Get( b.suffix(metricName, "timer"), prometheusLabels, @@ -330,7 +331,8 @@ func (b *Exporter) Listen(e <-chan Events) { log.Errorf(regErrF, metricName, err) conflictingEventStats.WithLabelValues("timer").Inc() } - } else { + + case timerTypeDefault, timerTypeSummary: summary, err := b.Summaries.Get( b.suffix(metricName, "timer"), prometheusLabels, @@ -342,6 +344,9 @@ func (b *Exporter) Listen(e <-chan Events) { log.Errorf(regErrF, metricName, err) conflictingEventStats.WithLabelValues("timer").Inc() } + + default: + panic(fmt.Sprintf("unknown timer type '%s'", t)) } default: diff --git a/mapper.go b/mapper.go index 9faaa75..4d1e172 100644 --- a/mapper.go +++ b/mapper.go @@ -35,7 +35,7 @@ var ( ) type mapperConfigDefaults struct { - TimerType string `yaml:"timer_type"` + TimerType timerType `yaml:"timer_type"` Buckets []float64 `yaml:"buckets"` } @@ -48,9 +48,9 @@ type metricMapper struct { type metricMapping struct { Match string `yaml:"match"` regex *regexp.Regexp - Labels prometheus.Labels - TimerType string `yaml:"timer_type"` - Buckets []float64 `yaml:"buckets"` + Labels prometheus.Labels `yaml:"labels"` + TimerType timerType `yaml:"timer_type"` + Buckets []float64 `yaml:"buckets"` } type configLoadStates int @@ -126,13 +126,15 @@ func (m *metricMapper) initFromString(fileContents string) error { } func (m *metricMapper) initFromYAMLString(fileContents string) error { - var n metricMapper + fmt.Println(fileContents) + if err := yaml.Unmarshal([]byte(fileContents), &n); err != nil { return err } + fmt.Printf("%#v\n", n) if n.Defaults.Buckets == nil || len(n.Defaults.Buckets) == 0 { n.Defaults.Buckets = prometheus.DefBuckets } diff --git a/timer.go b/timer.go new file mode 100644 index 0000000..8cf82d2 --- /dev/null +++ b/timer.go @@ -0,0 +1,41 @@ +// Copyright 2013 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import "fmt" + +type timerType string + +const ( + timerTypeHistogram timerType = "histogram" + timerTypeSummary timerType = "summary" + timerTypeDefault timerType = "" +) + +func (t *timerType) UnmarshalYAML(unmarshal func(interface{}) error) error { + var v string + if err := unmarshal(&v); err != nil { + return err + } + + switch timerType(v) { + case timerTypeHistogram: + *t = timerTypeHistogram + case timerTypeSummary, timerTypeDefault: + *t = timerTypeSummary + default: + return fmt.Errorf("invalid timer type '%s'", v) + } + return nil +}