Validate timer type while reading config

This commit is contained in:
Brian Akins 2017-07-26 14:50:29 -04:00
parent e8997f7cd9
commit b467a537d5
3 changed files with 59 additions and 11 deletions

View file

@ -309,15 +309,16 @@ func (b *Exporter) Listen(e <-chan Events) {
} }
case *TimerEvent: case *TimerEvent:
timerType := "" t := timerTypeDefault
if mapping != nil { if mapping != nil {
timerType = mapping.TimerType t = mapping.TimerType
} }
if timerType == "" { if t == timerTypeDefault {
timerType = b.mapper.Defaults.TimerType t = b.mapper.Defaults.TimerType
} }
if timerType == "histogram" { switch t {
case "histogram":
histogram, err := b.Histograms.Get( histogram, err := b.Histograms.Get(
b.suffix(metricName, "timer"), b.suffix(metricName, "timer"),
prometheusLabels, prometheusLabels,
@ -330,7 +331,8 @@ func (b *Exporter) Listen(e <-chan Events) {
log.Errorf(regErrF, metricName, err) log.Errorf(regErrF, metricName, err)
conflictingEventStats.WithLabelValues("timer").Inc() conflictingEventStats.WithLabelValues("timer").Inc()
} }
} else {
case timerTypeDefault, timerTypeSummary:
summary, err := b.Summaries.Get( summary, err := b.Summaries.Get(
b.suffix(metricName, "timer"), b.suffix(metricName, "timer"),
prometheusLabels, prometheusLabels,
@ -342,6 +344,9 @@ func (b *Exporter) Listen(e <-chan Events) {
log.Errorf(regErrF, metricName, err) log.Errorf(regErrF, metricName, err)
conflictingEventStats.WithLabelValues("timer").Inc() conflictingEventStats.WithLabelValues("timer").Inc()
} }
default:
panic(fmt.Sprintf("unknown timer type '%s'", t))
} }
default: default:

View file

@ -35,7 +35,7 @@ var (
) )
type mapperConfigDefaults struct { type mapperConfigDefaults struct {
TimerType string `yaml:"timer_type"` TimerType timerType `yaml:"timer_type"`
Buckets []float64 `yaml:"buckets"` Buckets []float64 `yaml:"buckets"`
} }
@ -48,8 +48,8 @@ type metricMapper struct {
type metricMapping struct { type metricMapping struct {
Match string `yaml:"match"` Match string `yaml:"match"`
regex *regexp.Regexp regex *regexp.Regexp
Labels prometheus.Labels Labels prometheus.Labels `yaml:"labels"`
TimerType string `yaml:"timer_type"` TimerType timerType `yaml:"timer_type"`
Buckets []float64 `yaml:"buckets"` Buckets []float64 `yaml:"buckets"`
} }
@ -126,13 +126,15 @@ func (m *metricMapper) initFromString(fileContents string) error {
} }
func (m *metricMapper) initFromYAMLString(fileContents string) error { func (m *metricMapper) initFromYAMLString(fileContents string) error {
var n metricMapper var n metricMapper
fmt.Println(fileContents)
if err := yaml.Unmarshal([]byte(fileContents), &n); err != nil { if err := yaml.Unmarshal([]byte(fileContents), &n); err != nil {
return err return err
} }
fmt.Printf("%#v\n", n)
if n.Defaults.Buckets == nil || len(n.Defaults.Buckets) == 0 { if n.Defaults.Buckets == nil || len(n.Defaults.Buckets) == 0 {
n.Defaults.Buckets = prometheus.DefBuckets n.Defaults.Buckets = prometheus.DefBuckets
} }

41
timer.go Normal file
View file

@ -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
}