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:
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:

View file

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

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
}