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