add methods for getting default histogram and summary options

Signed-off-by: glightfoot <glightfoot@rsglab.com>
This commit is contained in:
glightfoot 2021-01-28 10:33:51 -05:00
parent d739ffdbbc
commit 38fdc17c42
4 changed files with 58 additions and 23 deletions

View file

@ -420,7 +420,9 @@ 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.
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.
Setting `buckets` or `quantiles` in the defaults is deprecated in favor of `histogram_options` and `summary_options`, which will override the deprecated values.
NOTE: If `summary_options` is present in a mapping config, it will override all of the `summary_options` set in the `defaults` section.
```yaml
defaults:

View file

@ -56,7 +56,7 @@ type SummaryOptions struct {
}
// Clone returns a copy of SummaryOptions
func (s *SummaryOptions) Clone() *SummaryOptions {
func (s *SummaryOptions) Clone() SummaryOptions {
r := SummaryOptions{
Quantiles: make([]metricObjective, len(s.Quantiles)),
}
@ -65,7 +65,7 @@ func (s *SummaryOptions) Clone() *SummaryOptions {
r.AgeBuckets = s.AgeBuckets
r.BufCap = s.BufCap
return &r
return r
}
type HistogramOptions struct {
@ -73,13 +73,13 @@ type HistogramOptions struct {
}
// Clone returns a copy of HistogramOptions
func (h *HistogramOptions) Clone() *HistogramOptions {
func (h *HistogramOptions) Clone() HistogramOptions {
r := HistogramOptions{
Buckets: make([]float64, len(h.Buckets)),
}
copy(r.Buckets, h.Buckets)
return &r
return r
}
type metricObjective struct {
@ -93,6 +93,36 @@ var defaultQuantiles = []metricObjective{
{Quantile: 0.99, Error: 0.001},
}
// GetDefaultHistogramOptions returns a copy of the default HistogramOptions
func (m *MetricMapper) GetDefaultHistogramOptions() HistogramOptions {
r := HistogramOptions{}
if m.Defaults.HistogramOptions == nil {
r.Buckets = prometheus.DefBuckets
} else {
r = m.Defaults.HistogramOptions.Clone()
if m.Defaults.HistogramOptions != nil && len(m.Defaults.HistogramOptions.Buckets) == 0 {
r.Buckets = prometheus.DefBuckets
}
}
return r
}
// GetDefaultSummaryOptions returns a copy of the default SummaryOptions
func (m *MetricMapper) GetDefaultSummaryOptions() SummaryOptions {
r := SummaryOptions{}
if m.Defaults.SummaryOptions == nil {
r.Quantiles = defaultQuantiles
} else {
r = m.Defaults.SummaryOptions.Clone()
if m.Defaults.SummaryOptions != nil && len(m.Defaults.SummaryOptions.Quantiles) == 0 {
r.Quantiles = defaultQuantiles
}
}
return r
}
func (m *MetricMapper) InitFromYAMLString(fileContents string, cacheSize int, options ...CacheOption) error {
var n MetricMapper
@ -211,7 +241,8 @@ func (m *MetricMapper) InitFromYAMLString(fileContents string, cacheSize int, op
return fmt.Errorf("cannot use histogram observer and summary options at the same time")
}
if currentMapping.HistogramOptions == nil {
currentMapping.HistogramOptions = n.Defaults.HistogramOptions.Clone()
c := n.Defaults.HistogramOptions.Clone()
currentMapping.HistogramOptions = &c
}
if currentMapping.LegacyBuckets != nil && len(currentMapping.LegacyBuckets) != 0 {
currentMapping.HistogramOptions.Buckets = currentMapping.LegacyBuckets
@ -223,7 +254,8 @@ func (m *MetricMapper) InitFromYAMLString(fileContents string, cacheSize int, op
return fmt.Errorf("cannot use summary observer and histogram options at the same time")
}
if currentMapping.SummaryOptions == nil {
currentMapping.SummaryOptions = n.Defaults.SummaryOptions.Clone()
c := n.Defaults.SummaryOptions.Clone()
currentMapping.SummaryOptions = &c
}
if currentMapping.LegacyQuantiles != nil && len(currentMapping.LegacyQuantiles) != 0 {
currentMapping.SummaryOptions.Quantiles = currentMapping.LegacyQuantiles

View file

@ -18,8 +18,8 @@ import "time"
type mapperConfigDefaults struct {
ObserverType ObserverType `yaml:"observer_type"`
TimerType ObserverType `yaml:"timer_type,omitempty"` // DEPRECATED - field only present to preserve backwards compatibility in configs. Always empty
Buckets []float64 `yaml:"buckets"` // DEPREECATED - field only present to preserve backwards compatibility in configs. Always empty
Quantiles []metricObjective `yaml:"quantiles"` // DEPREECATED - field only present to preserve backwards compatibility in configs. Always empty
Buckets []float64 `yaml:"buckets"` // DEPRECATED - field only present to preserve backwards compatibility in configs. Always empty
Quantiles []metricObjective `yaml:"quantiles"` // DEPRECATED - field only present to preserve backwards compatibility in configs. Always empty
MatchType MatchType `yaml:"match_type"`
GlobDisableOrdering bool `yaml:"glob_disable_ordering"`
Ttl time.Duration `yaml:"ttl"`

View file

@ -248,14 +248,17 @@ func (r *Registry) GetHistogram(metricName string, labels prometheus.Labels, hel
var histogramVec *prometheus.HistogramVec
if vh == nil {
metricsCount.WithLabelValues("histogram").Inc()
buckets := r.Mapper.Defaults.HistogramOptions.Buckets
if mapping.HistogramOptions != nil && len(mapping.HistogramOptions.Buckets) > 0 {
buckets = mapping.HistogramOptions.Buckets
histogramOptions := r.Mapper.GetDefaultHistogramOptions()
if mapping != nil {
if mapping.HistogramOptions != nil && len(mapping.HistogramOptions.Buckets) > 0 {
histogramOptions.Buckets = mapping.HistogramOptions.Buckets
}
}
histogramVec = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: metricName,
Help: help,
Buckets: buckets,
Buckets: histogramOptions.Buckets,
}, labelNames)
if err := prometheus.Register(uncheckedCollector{histogramVec}); err != nil {
@ -295,22 +298,20 @@ func (r *Registry) GetSummary(metricName string, labels prometheus.Labels, help
var summaryVec *prometheus.SummaryVec
if vh == nil {
metricsCount.WithLabelValues("summary").Inc()
quantiles := r.Mapper.Defaults.Quantiles
if mapping != nil && mapping.SummaryOptions != nil && len(mapping.SummaryOptions.Quantiles) > 0 {
quantiles = mapping.SummaryOptions.Quantiles
}
summaryOptions := mapper.SummaryOptions{}
summaryOptions := r.Mapper.GetDefaultSummaryOptions()
if mapping != nil && mapping.SummaryOptions != nil {
summaryOptions = *mapping.SummaryOptions
}
if mapping != nil && mapping.SummaryOptions != nil && len(mapping.SummaryOptions.Quantiles) > 0 {
summaryOptions.Quantiles = mapping.SummaryOptions.Quantiles
}
objectives := make(map[float64]float64)
for _, q := range quantiles {
for _, q := range summaryOptions.Quantiles {
objectives[q.Quantile] = q.Error
}
// In the case of no mapping file, explicitly define the default quantiles
if len(objectives) == 0 {
objectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
}
summaryVec = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: metricName,
Help: help,