From 4a64979563442b948c6b929daee10f3b15493681 Mon Sep 17 00:00:00 2001 From: glightfoot Date: Mon, 15 Jun 2020 21:37:22 -0400 Subject: [PATCH] move mapping and mapper_defaults into their own files with their unmarshalers Signed-off-by: glightfoot --- pkg/mapper/mapper.go | 88 ----------------------------------- pkg/mapper/mapper_defaults.go | 51 ++++++++++++++++++++ pkg/mapper/mapping.go | 76 ++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 88 deletions(-) create mode 100644 pkg/mapper/mapper_defaults.go create mode 100644 pkg/mapper/mapping.go diff --git a/pkg/mapper/mapper.go b/pkg/mapper/mapper.go index 95d3b0c..e44e581 100644 --- a/pkg/mapper/mapper.go +++ b/pkg/mapper/mapper.go @@ -35,16 +35,6 @@ var ( labelNameRE = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]+$`) ) -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"` - Quantiles []metricObjective `yaml:"quantiles"` - MatchType MatchType `yaml:"match_type"` - GlobDisableOrdering bool `yaml:"glob_disable_ordering"` - Ttl time.Duration `yaml:"ttl"` -} - type MetricMapper struct { Defaults mapperConfigDefaults `yaml:"defaults"` Mappings []MetricMapping `yaml:"mappings"` @@ -57,27 +47,6 @@ type MetricMapper struct { MappingsCount prometheus.Gauge } -type MetricMapping struct { - Match string `yaml:"match"` - Name string `yaml:"name"` - nameFormatter *fsm.TemplateFormatter - regex *regexp.Regexp - Labels prometheus.Labels `yaml:"labels"` - labelKeys []string - labelFormatters []*fsm.TemplateFormatter - ObserverType ObserverType `yaml:"observer_type"` - TimerType ObserverType `yaml:"timer_type,omitempty"` // DEPRECATED - field only present to preserve backwards compatibility in configs. Always empty - LegacyBuckets []float64 `yaml:"buckets"` - LegacyQuantiles []metricObjective `yaml:"quantiles"` - MatchType MatchType `yaml:"match_type"` - HelpText string `yaml:"help"` - Action ActionType `yaml:"action"` - MatchMetricType MetricType `yaml:"match_metric_type"` - Ttl time.Duration `yaml:"ttl"` - SummaryOptions *SummaryOptions `yaml:"summary_options"` - HistogramOptions *HistogramOptions `yaml:"histogram_options"` -} - type SummaryOptions struct { Quantiles []metricObjective `yaml:"quantiles"` MaxAge time.Duration `yaml:"max_age"` @@ -100,63 +69,6 @@ var defaultQuantiles = []metricObjective{ {Quantile: 0.99, Error: 0.001}, } -// UnmarshalYAML is a custom unmarshal function to allow use of deprecated config keys -// observer_type will override timer_type -func (d *mapperConfigDefaults) UnmarshalYAML(unmarshal func(interface{}) error) error { - type mapperConfigDefaultsAlias mapperConfigDefaults - var tmp mapperConfigDefaultsAlias - if err := unmarshal(&tmp); err != nil { - return err - } - - // Copy defaults - d.ObserverType = tmp.ObserverType - d.Buckets = tmp.Buckets - d.Quantiles = tmp.Quantiles - d.MatchType = tmp.MatchType - d.GlobDisableOrdering = tmp.GlobDisableOrdering - d.Ttl = tmp.Ttl - - // Use deprecated TimerType if necessary - if tmp.ObserverType == "" { - d.ObserverType = tmp.TimerType - } - - return nil -} - -// UnmarshalYAML is a custom unmarshal function to allow use of deprecated config keys -// observer_type will override timer_type -func (m *MetricMapping) UnmarshalYAML(unmarshal func(interface{}) error) error { - type MetricMappingAlias MetricMapping - var tmp MetricMappingAlias - if err := unmarshal(&tmp); err != nil { - return err - } - - // Copy defaults - m.Match = tmp.Match - m.Name = tmp.Name - m.Labels = tmp.Labels - m.ObserverType = tmp.ObserverType - m.LegacyBuckets = tmp.LegacyBuckets - m.LegacyQuantiles = tmp.LegacyQuantiles - m.MatchType = tmp.MatchType - m.HelpText = tmp.HelpText - m.Action = tmp.Action - m.MatchMetricType = tmp.MatchMetricType - m.Ttl = tmp.Ttl - m.SummaryOptions = tmp.SummaryOptions - m.HistogramOptions = tmp.HistogramOptions - - // Use deprecated TimerType if necessary - if tmp.ObserverType == "" { - m.ObserverType = tmp.TimerType - } - - return nil -} - func (m *MetricMapper) InitFromYAMLString(fileContents string, cacheSize int, options ...CacheOption) error { var n MetricMapper diff --git a/pkg/mapper/mapper_defaults.go b/pkg/mapper/mapper_defaults.go new file mode 100644 index 0000000..7fba9fe --- /dev/null +++ b/pkg/mapper/mapper_defaults.go @@ -0,0 +1,51 @@ +// Copyright 2020 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 mapper + +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"` + Quantiles []metricObjective `yaml:"quantiles"` + MatchType MatchType `yaml:"match_type"` + GlobDisableOrdering bool `yaml:"glob_disable_ordering"` + Ttl time.Duration `yaml:"ttl"` +} + +// UnmarshalYAML is a custom unmarshal function to allow use of deprecated config keys +// observer_type will override timer_type +func (d *mapperConfigDefaults) UnmarshalYAML(unmarshal func(interface{}) error) error { + type mapperConfigDefaultsAlias mapperConfigDefaults + var tmp mapperConfigDefaultsAlias + if err := unmarshal(&tmp); err != nil { + return err + } + + // Copy defaults + d.ObserverType = tmp.ObserverType + d.Buckets = tmp.Buckets + d.Quantiles = tmp.Quantiles + d.MatchType = tmp.MatchType + d.GlobDisableOrdering = tmp.GlobDisableOrdering + d.Ttl = tmp.Ttl + + // Use deprecated TimerType if necessary + if tmp.ObserverType == "" { + d.ObserverType = tmp.TimerType + } + + return nil +} diff --git a/pkg/mapper/mapping.go b/pkg/mapper/mapping.go new file mode 100644 index 0000000..cdba27a --- /dev/null +++ b/pkg/mapper/mapping.go @@ -0,0 +1,76 @@ +// Copyright 2020 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 xpress or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mapper + +import ( + "regexp" + "time" + + "github.com/prometheus/client_golang/prometheus" + + "github.com/prometheus/statsd_exporter/pkg/mapper/fsm" +) + +type MetricMapping struct { + Match string `yaml:"match"` + Name string `yaml:"name"` + nameFormatter *fsm.TemplateFormatter + regex *regexp.Regexp + Labels prometheus.Labels `yaml:"labels"` + labelKeys []string + labelFormatters []*fsm.TemplateFormatter + ObserverType ObserverType `yaml:"observer_type"` + TimerType ObserverType `yaml:"timer_type,omitempty"` // DEPRECATED - field only present to preserve backwards compatibility in configs. Always empty + LegacyBuckets []float64 `yaml:"buckets"` + LegacyQuantiles []metricObjective `yaml:"quantiles"` + MatchType MatchType `yaml:"match_type"` + HelpText string `yaml:"help"` + Action ActionType `yaml:"action"` + MatchMetricType MetricType `yaml:"match_metric_type"` + 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 +// observer_type will override timer_type +func (m *MetricMapping) UnmarshalYAML(unmarshal func(interface{}) error) error { + type MetricMappingAlias MetricMapping + var tmp MetricMappingAlias + if err := unmarshal(&tmp); err != nil { + return err + } + + // Copy defaults + m.Match = tmp.Match + m.Name = tmp.Name + m.Labels = tmp.Labels + m.ObserverType = tmp.ObserverType + m.LegacyBuckets = tmp.LegacyBuckets + m.LegacyQuantiles = tmp.LegacyQuantiles + m.MatchType = tmp.MatchType + m.HelpText = tmp.HelpText + m.Action = tmp.Action + m.MatchMetricType = tmp.MatchMetricType + m.Ttl = tmp.Ttl + m.SummaryOptions = tmp.SummaryOptions + m.HistogramOptions = tmp.HistogramOptions + + // Use deprecated TimerType if necessary + if tmp.ObserverType == "" { + m.ObserverType = tmp.TimerType + } + + return nil +}