add support for setting histogram_options and summary_options in defaults

Signed-off-by: glightfoot <glightfoot@rsglab.com>
This commit is contained in:
glightfoot 2021-01-28 13:37:56 -05:00
parent 64dd103e3f
commit 06411336c7
5 changed files with 650 additions and 130 deletions

View file

@ -341,9 +341,9 @@ mappings:
error: 0.05 error: 0.05
- quantile: 0.5 - quantile: 0.5
error: 0.005 error: 0.005
max_summary_age: 30s max_age: 30s
summary_age_buckets: 3 age_buckets: 3
stream_buffer_size: 1000 buf_cap: 1000
``` ```
The default quantiles are 0.99, 0.9, and 0.5. The default quantiles are 0.99, 0.9, and 0.5.
@ -414,16 +414,34 @@ mappings:
### Global defaults ### Global defaults
One may also set defaults for the observer type, buckets or quantiles, and match type. One may also set defaults for the observer type, histogram options, summary options, and match type.
These will be used by all mappings that do not define them. These will be used by all mappings that do not define them.
An option that can only be configured in `defaults` is `glob_disable_ordering`, which is `false` if omitted. An option that can only be configured in `defaults` is `glob_disable_ordering`, which is `false` if omitted.
By setting this to `true`, `glob` match type will not honor the occurance of rules in the mapping rules file and always treat `*` as lower priority than a concrete string. By setting this to `true`, `glob` match type will not honor the occurance of rules in the mapping rules file and always treat `*` as lower priority than a concrete string.
Setting `buckets` or `quantiles` in the defaults is deprecated in favor of `histogram_options` and `summary_options`, which will override the deprecated values.
If `summary_options` is present in a mapping config, it will only override the fields set in the mapping. Unset fields in the mapping will take the values from the defaults.
```yaml ```yaml
defaults: defaults:
observer_type: histogram observer_type: histogram
buckets: [.005, .01, .025, .05, .1, .25, .5, 1, 2.5 ] histogram_options:
buckets: [.005, .01, .025, .05, .1, .25, .5, 1, 2.5 ]
summary_options:
quantiles:
- quantile: 0.99
error: 0.001
- quantile: 0.95
error: 0.01
- quantile: 0.9
error: 0.05
- quantile: 0.5
error: 0.005
max_age: 5m
age_buckets: 2
buf_cap: 1000
match_type: glob match_type: glob
glob_disable_ordering: false glob_disable_ordering: false
ttl: 0 # metrics do not expire ttl: 0 # metrics do not expire
@ -435,7 +453,7 @@ mappings:
provider: "$2" provider: "$2"
outcome: "$3" outcome: "$3"
job: "${1}_server" job: "${1}_server"
# This will be a summary timer. # This will be a summary using the summary_options set in `defaults`
- match: "other.distribution.*.*.*" - match: "other.distribution.*.*.*"
observer_type: summary observer_type: summary
name: "other_distribution" name: "other_distribution"

View file

@ -22,8 +22,9 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log" "github.com/prometheus/common/log"
"github.com/prometheus/statsd_exporter/pkg/mapper/fsm"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
"github.com/prometheus/statsd_exporter/pkg/mapper/fsm"
) )
var ( var (
@ -77,12 +78,12 @@ func (m *MetricMapper) InitFromYAMLString(fileContents string, cacheSize int, op
return err return err
} }
if n.Defaults.Buckets == nil || len(n.Defaults.Buckets) == 0 { if len(n.Defaults.HistogramOptions.Buckets) == 0 {
n.Defaults.Buckets = prometheus.DefBuckets n.Defaults.HistogramOptions.Buckets = prometheus.DefBuckets
} }
if n.Defaults.Quantiles == nil || len(n.Defaults.Quantiles) == 0 { if len(n.Defaults.SummaryOptions.Quantiles) == 0 {
n.Defaults.Quantiles = defaultQuantiles n.Defaults.SummaryOptions.Quantiles = defaultQuantiles
} }
if n.Defaults.MatchType == MatchTypeDefault { if n.Defaults.MatchType == MatchTypeDefault {
@ -190,7 +191,7 @@ func (m *MetricMapper) InitFromYAMLString(fileContents string, cacheSize int, op
currentMapping.HistogramOptions.Buckets = currentMapping.LegacyBuckets currentMapping.HistogramOptions.Buckets = currentMapping.LegacyBuckets
} }
if currentMapping.HistogramOptions.Buckets == nil || len(currentMapping.HistogramOptions.Buckets) == 0 { if currentMapping.HistogramOptions.Buckets == nil || len(currentMapping.HistogramOptions.Buckets) == 0 {
currentMapping.HistogramOptions.Buckets = n.Defaults.Buckets currentMapping.HistogramOptions.Buckets = n.Defaults.HistogramOptions.Buckets
} }
} }
@ -205,7 +206,16 @@ func (m *MetricMapper) InitFromYAMLString(fileContents string, cacheSize int, op
currentMapping.SummaryOptions.Quantiles = currentMapping.LegacyQuantiles currentMapping.SummaryOptions.Quantiles = currentMapping.LegacyQuantiles
} }
if currentMapping.SummaryOptions.Quantiles == nil || len(currentMapping.SummaryOptions.Quantiles) == 0 { if currentMapping.SummaryOptions.Quantiles == nil || len(currentMapping.SummaryOptions.Quantiles) == 0 {
currentMapping.SummaryOptions.Quantiles = n.Defaults.Quantiles currentMapping.SummaryOptions.Quantiles = n.Defaults.SummaryOptions.Quantiles
}
if currentMapping.SummaryOptions.MaxAge == 0 {
currentMapping.SummaryOptions.MaxAge = n.Defaults.SummaryOptions.MaxAge
}
if currentMapping.SummaryOptions.AgeBuckets == 0 {
currentMapping.SummaryOptions.AgeBuckets = n.Defaults.SummaryOptions.AgeBuckets
}
if currentMapping.SummaryOptions.BufCap == 0 {
currentMapping.SummaryOptions.BufCap = n.Defaults.SummaryOptions.BufCap
} }
} }

View file

@ -18,11 +18,13 @@ import "time"
type mapperConfigDefaults struct { type mapperConfigDefaults struct {
ObserverType ObserverType `yaml:"observer_type"` ObserverType ObserverType `yaml:"observer_type"`
TimerType ObserverType `yaml:"timer_type,omitempty"` // DEPRECATED - field only present to preserve backwards compatibility in configs. Always empty TimerType ObserverType `yaml:"timer_type,omitempty"` // DEPRECATED - field only present to preserve backwards compatibility in configs. Always empty
Buckets []float64 `yaml:"buckets"` Buckets []float64 `yaml:"buckets"` // DEPRECATED - field only present to preserve backwards compatibility in configs. Always empty
Quantiles []metricObjective `yaml:"quantiles"` Quantiles []metricObjective `yaml:"quantiles"` // DEPRECATED - field only present to preserve backwards compatibility in configs. Always empty
MatchType MatchType `yaml:"match_type"` MatchType MatchType `yaml:"match_type"`
GlobDisableOrdering bool `yaml:"glob_disable_ordering"` GlobDisableOrdering bool `yaml:"glob_disable_ordering"`
Ttl time.Duration `yaml:"ttl"` Ttl time.Duration `yaml:"ttl"`
SummaryOptions SummaryOptions `yaml:"summary_options"`
HistogramOptions HistogramOptions `yaml:"histogram_options"`
} }
// UnmarshalYAML is a custom unmarshal function to allow use of deprecated config keys // UnmarshalYAML is a custom unmarshal function to allow use of deprecated config keys
@ -36,16 +38,26 @@ func (d *mapperConfigDefaults) UnmarshalYAML(unmarshal func(interface{}) error)
// Copy defaults // Copy defaults
d.ObserverType = tmp.ObserverType d.ObserverType = tmp.ObserverType
d.Buckets = tmp.Buckets
d.Quantiles = tmp.Quantiles
d.MatchType = tmp.MatchType d.MatchType = tmp.MatchType
d.GlobDisableOrdering = tmp.GlobDisableOrdering d.GlobDisableOrdering = tmp.GlobDisableOrdering
d.Ttl = tmp.Ttl d.Ttl = tmp.Ttl
d.SummaryOptions = tmp.SummaryOptions
d.HistogramOptions = tmp.HistogramOptions
// Use deprecated TimerType if necessary // Use deprecated TimerType if necessary
if tmp.ObserverType == "" { if tmp.ObserverType == "" {
d.ObserverType = tmp.TimerType d.ObserverType = tmp.TimerType
} }
// Use deprecated quantiles if necessary
if len(tmp.SummaryOptions.Quantiles) == 0 && len(tmp.Quantiles) > 0 {
d.SummaryOptions = SummaryOptions{Quantiles: tmp.Quantiles}
}
// Use deprecated buckets if necessary
if len(tmp.HistogramOptions.Buckets) == 0 && len(tmp.Buckets) > 0 {
d.HistogramOptions = HistogramOptions{Buckets: tmp.Buckets}
}
return nil return nil
} }

File diff suppressed because it is too large Load diff

View file

@ -23,6 +23,7 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/statsd_exporter/pkg/clock" "github.com/prometheus/statsd_exporter/pkg/clock"
"github.com/prometheus/statsd_exporter/pkg/mapper" "github.com/prometheus/statsd_exporter/pkg/mapper"
"github.com/prometheus/statsd_exporter/pkg/metrics" "github.com/prometheus/statsd_exporter/pkg/metrics"
@ -248,7 +249,7 @@ func (r *Registry) GetHistogram(metricName string, labels prometheus.Labels, hel
var histogramVec *prometheus.HistogramVec var histogramVec *prometheus.HistogramVec
if vh == nil { if vh == nil {
metricsCount.WithLabelValues("histogram").Inc() metricsCount.WithLabelValues("histogram").Inc()
buckets := r.Mapper.Defaults.Buckets buckets := r.Mapper.Defaults.HistogramOptions.Buckets
if mapping.HistogramOptions != nil && len(mapping.HistogramOptions.Buckets) > 0 { if mapping.HistogramOptions != nil && len(mapping.HistogramOptions.Buckets) > 0 {
buckets = mapping.HistogramOptions.Buckets buckets = mapping.HistogramOptions.Buckets
} }
@ -295,7 +296,7 @@ func (r *Registry) GetSummary(metricName string, labels prometheus.Labels, help
var summaryVec *prometheus.SummaryVec var summaryVec *prometheus.SummaryVec
if vh == nil { if vh == nil {
metricsCount.WithLabelValues("summary").Inc() metricsCount.WithLabelValues("summary").Inc()
quantiles := r.Mapper.Defaults.Quantiles quantiles := r.Mapper.Defaults.SummaryOptions.Quantiles
if mapping != nil && mapping.SummaryOptions != nil && len(mapping.SummaryOptions.Quantiles) > 0 { if mapping != nil && mapping.SummaryOptions != nil && len(mapping.SummaryOptions.Quantiles) > 0 {
quantiles = mapping.SummaryOptions.Quantiles quantiles = mapping.SummaryOptions.Quantiles
} }