2015-02-18 21:40:55 +00:00
|
|
|
// 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
|
2013-07-05 17:31:53 +00:00
|
|
|
//
|
2015-02-18 21:40:55 +00:00
|
|
|
// 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.
|
2013-07-05 17:31:53 +00:00
|
|
|
|
2018-08-10 12:28:38 +00:00
|
|
|
package mapper
|
2013-07-05 17:31:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
2013-07-08 18:37:12 +00:00
|
|
|
"sync"
|
2020-01-02 13:39:39 +00:00
|
|
|
"time"
|
2013-07-08 18:37:12 +00:00
|
|
|
|
2021-06-07 14:00:08 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2013-07-08 18:37:12 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-03-13 01:11:05 +00:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2021-01-28 18:37:56 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/statsd_exporter/pkg/mapper/fsm"
|
2013-07-05 17:31:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-02-07 16:13:35 +00:00
|
|
|
// The first segment of a match cannot start with a number
|
|
|
|
statsdMetricRE = `[a-zA-Z_](-?[a-zA-Z0-9_])*`
|
|
|
|
// The subsequent segments of a match can start with a number
|
|
|
|
// See https://github.com/prometheus/statsd_exporter/issues/328
|
|
|
|
statsdMetricSubsequentRE = `[a-zA-Z0-9_](-?[a-zA-Z0-9_])*`
|
|
|
|
templateReplaceRE = `(\$\{?\d+\}?)`
|
2015-10-09 22:03:21 +00:00
|
|
|
|
2021-02-07 16:13:35 +00:00
|
|
|
metricLineRE = regexp.MustCompile(`^(\*|` + statsdMetricRE + `)(\.\*|\.` + statsdMetricSubsequentRE + `)*$`)
|
2018-09-12 00:31:26 +00:00
|
|
|
metricNameRE = regexp.MustCompile(`^([a-zA-Z_]|` + templateReplaceRE + `)([a-zA-Z0-9_]|` + templateReplaceRE + `)*$`)
|
|
|
|
labelNameRE = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]+$`)
|
2013-07-05 17:31:53 +00:00
|
|
|
)
|
|
|
|
|
2018-08-10 12:28:38 +00:00
|
|
|
type MetricMapper struct {
|
2020-11-20 19:14:14 +00:00
|
|
|
Registerer prometheus.Registerer
|
|
|
|
Defaults mapperConfigDefaults `yaml:"defaults"`
|
|
|
|
Mappings []MetricMapping `yaml:"mappings"`
|
|
|
|
FSM *fsm.FSM
|
|
|
|
doFSM bool
|
|
|
|
doRegex bool
|
|
|
|
cache MetricMapperCache
|
|
|
|
mutex sync.RWMutex
|
2018-08-14 09:31:38 +00:00
|
|
|
|
|
|
|
MappingsCount prometheus.Gauge
|
2021-06-07 14:00:08 +00:00
|
|
|
|
|
|
|
Logger log.Logger
|
2013-07-05 17:31:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 16:55:09 +00:00
|
|
|
type SummaryOptions struct {
|
2020-01-27 14:50:34 +00:00
|
|
|
Quantiles []metricObjective `yaml:"quantiles"`
|
|
|
|
MaxAge time.Duration `yaml:"max_age"`
|
|
|
|
AgeBuckets uint32 `yaml:"age_buckets"`
|
|
|
|
BufCap uint32 `yaml:"buf_cap"`
|
2020-01-16 16:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HistogramOptions struct {
|
|
|
|
Buckets []float64 `yaml:"buckets"`
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 02:41:41 +00:00
|
|
|
type metricObjective struct {
|
|
|
|
Quantile float64 `yaml:"quantile"`
|
|
|
|
Error float64 `yaml:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultQuantiles = []metricObjective{
|
|
|
|
{Quantile: 0.5, Error: 0.05},
|
|
|
|
{Quantile: 0.9, Error: 0.01},
|
|
|
|
{Quantile: 0.99, Error: 0.001},
|
|
|
|
}
|
|
|
|
|
2021-02-07 00:22:29 +00:00
|
|
|
func (m *MetricMapper) InitFromYAMLString(fileContents string) error {
|
2018-08-10 12:28:38 +00:00
|
|
|
var n MetricMapper
|
2017-03-13 01:11:05 +00:00
|
|
|
|
|
|
|
if err := yaml.Unmarshal([]byte(fileContents), &n); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-28 18:37:56 +00:00
|
|
|
if len(n.Defaults.HistogramOptions.Buckets) == 0 {
|
|
|
|
n.Defaults.HistogramOptions.Buckets = prometheus.DefBuckets
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 18:37:56 +00:00
|
|
|
if len(n.Defaults.SummaryOptions.Quantiles) == 0 {
|
|
|
|
n.Defaults.SummaryOptions.Quantiles = defaultQuantiles
|
2018-08-09 02:41:41 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 12:28:38 +00:00
|
|
|
if n.Defaults.MatchType == MatchTypeDefault {
|
|
|
|
n.Defaults.MatchType = MatchTypeGlob
|
2017-09-29 07:57:17 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 00:59:11 +00:00
|
|
|
remainingMappingsCount := len(n.Mappings)
|
2018-07-19 21:57:36 +00:00
|
|
|
|
2020-06-15 21:26:20 +00:00
|
|
|
n.FSM = fsm.NewFSM([]string{string(MetricTypeCounter), string(MetricTypeGauge), string(MetricTypeObserver)},
|
2018-09-22 00:59:11 +00:00
|
|
|
remainingMappingsCount, n.Defaults.GlobDisableOrdering)
|
2018-07-19 21:57:36 +00:00
|
|
|
|
2017-03-13 01:11:05 +00:00
|
|
|
for i := range n.Mappings {
|
2018-09-22 00:59:11 +00:00
|
|
|
remainingMappingsCount--
|
2018-07-19 21:57:36 +00:00
|
|
|
|
2017-03-13 01:11:05 +00:00
|
|
|
currentMapping := &n.Mappings[i]
|
|
|
|
|
2017-09-29 07:57:17 +00:00
|
|
|
// check that label is correct
|
2017-11-13 12:21:29 +00:00
|
|
|
for k := range currentMapping.Labels {
|
2018-01-16 13:16:15 +00:00
|
|
|
if !labelNameRE.MatchString(k) {
|
2017-08-01 11:07:44 +00:00
|
|
|
return fmt.Errorf("invalid label key: %s", k)
|
2017-07-27 20:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 12:21:29 +00:00
|
|
|
if currentMapping.Name == "" {
|
2017-08-01 11:01:06 +00:00
|
|
|
return fmt.Errorf("line %d: metric mapping didn't set a metric name", i)
|
2017-07-27 20:58:10 +00:00
|
|
|
}
|
2017-11-13 12:21:29 +00:00
|
|
|
|
|
|
|
if !metricNameRE.MatchString(currentMapping.Name) {
|
|
|
|
return fmt.Errorf("metric name '%s' doesn't match regex '%s'", currentMapping.Name, metricNameRE)
|
|
|
|
}
|
|
|
|
|
2017-09-29 07:57:17 +00:00
|
|
|
if currentMapping.MatchType == "" {
|
|
|
|
currentMapping.MatchType = n.Defaults.MatchType
|
|
|
|
}
|
2017-07-27 20:58:10 +00:00
|
|
|
|
2018-01-02 22:21:50 +00:00
|
|
|
if currentMapping.Action == "" {
|
2018-08-10 12:28:38 +00:00
|
|
|
currentMapping.Action = ActionTypeMap
|
2018-01-02 22:21:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 00:31:26 +00:00
|
|
|
if currentMapping.MatchType == MatchTypeGlob {
|
2018-09-22 00:59:11 +00:00
|
|
|
n.doFSM = true
|
2018-09-12 00:31:26 +00:00
|
|
|
if !metricLineRE.MatchString(currentMapping.Match) {
|
|
|
|
return fmt.Errorf("invalid match: %s", currentMapping.Match)
|
|
|
|
}
|
|
|
|
|
2018-10-03 23:30:01 +00:00
|
|
|
captureCount := n.FSM.AddState(currentMapping.Match, string(currentMapping.MatchMetricType),
|
2018-09-22 00:59:11 +00:00
|
|
|
remainingMappingsCount, currentMapping)
|
2018-09-12 00:31:26 +00:00
|
|
|
|
2018-09-28 20:13:06 +00:00
|
|
|
currentMapping.nameFormatter = fsm.NewTemplateFormatter(currentMapping.Name, captureCount)
|
|
|
|
|
|
|
|
labelKeys := make([]string, len(currentMapping.Labels))
|
|
|
|
labelFormatters := make([]*fsm.TemplateFormatter, len(currentMapping.Labels))
|
|
|
|
labelIndex := 0
|
|
|
|
for label, valueExpr := range currentMapping.Labels {
|
|
|
|
labelKeys[labelIndex] = label
|
|
|
|
labelFormatters[labelIndex] = fsm.NewTemplateFormatter(valueExpr, captureCount)
|
|
|
|
labelIndex++
|
|
|
|
}
|
|
|
|
currentMapping.labelFormatters = labelFormatters
|
|
|
|
currentMapping.labelKeys = labelKeys
|
2018-09-12 00:31:26 +00:00
|
|
|
} else {
|
|
|
|
if regex, err := regexp.Compile(currentMapping.Match); err != nil {
|
|
|
|
return fmt.Errorf("invalid regex %s in mapping: %v", currentMapping.Match, err)
|
|
|
|
} else {
|
|
|
|
currentMapping.regex = regex
|
|
|
|
}
|
2018-09-12 21:44:07 +00:00
|
|
|
n.doRegex = true
|
2018-09-13 18:46:41 +00:00
|
|
|
}
|
2017-03-13 01:11:05 +00:00
|
|
|
|
2020-06-15 21:26:20 +00:00
|
|
|
if currentMapping.ObserverType == "" {
|
2020-06-15 22:41:16 +00:00
|
|
|
currentMapping.ObserverType = n.Defaults.ObserverType
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 16:55:09 +00:00
|
|
|
if currentMapping.LegacyQuantiles != nil &&
|
|
|
|
(currentMapping.SummaryOptions == nil || currentMapping.SummaryOptions.Quantiles != nil) {
|
2021-06-07 14:00:08 +00:00
|
|
|
level.Warn(m.Logger).Log("msg", "using the top level quantiles is deprecated. Please use quantiles in the summary_options hierarchy")
|
2020-01-16 16:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if currentMapping.LegacyBuckets != nil &&
|
|
|
|
(currentMapping.HistogramOptions == nil || currentMapping.HistogramOptions.Buckets != nil) {
|
2021-06-07 14:00:08 +00:00
|
|
|
level.Warn(m.Logger).Log("msg", "using the top level buckets is deprecated. Please use buckets in the histogram_options hierarchy")
|
2020-01-16 16:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if currentMapping.SummaryOptions != nil &&
|
|
|
|
currentMapping.LegacyQuantiles != nil &&
|
|
|
|
currentMapping.SummaryOptions.Quantiles != nil {
|
|
|
|
return fmt.Errorf("cannot use quantiles in both the top level and summary options at the same time in %s", currentMapping.Match)
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentMapping.HistogramOptions != nil &&
|
|
|
|
currentMapping.LegacyBuckets != nil &&
|
|
|
|
currentMapping.HistogramOptions.Buckets != nil {
|
|
|
|
return fmt.Errorf("cannot use buckets in both the top level and histogram options at the same time in %s", currentMapping.Match)
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:26:20 +00:00
|
|
|
if currentMapping.ObserverType == ObserverTypeHistogram {
|
2020-01-16 16:55:09 +00:00
|
|
|
if currentMapping.SummaryOptions != nil {
|
2020-06-15 21:26:20 +00:00
|
|
|
return fmt.Errorf("cannot use histogram observer and summary options at the same time")
|
2020-01-16 16:55:09 +00:00
|
|
|
}
|
|
|
|
if currentMapping.HistogramOptions == nil {
|
|
|
|
currentMapping.HistogramOptions = &HistogramOptions{}
|
|
|
|
}
|
|
|
|
if currentMapping.LegacyBuckets != nil && len(currentMapping.LegacyBuckets) != 0 {
|
|
|
|
currentMapping.HistogramOptions.Buckets = currentMapping.LegacyBuckets
|
|
|
|
}
|
|
|
|
if currentMapping.HistogramOptions.Buckets == nil || len(currentMapping.HistogramOptions.Buckets) == 0 {
|
2021-01-28 18:37:56 +00:00
|
|
|
currentMapping.HistogramOptions.Buckets = n.Defaults.HistogramOptions.Buckets
|
2020-01-16 16:55:09 +00:00
|
|
|
}
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
2017-07-27 20:58:10 +00:00
|
|
|
|
2020-06-15 21:26:20 +00:00
|
|
|
if currentMapping.ObserverType == ObserverTypeSummary {
|
2020-01-16 16:55:09 +00:00
|
|
|
if currentMapping.HistogramOptions != nil {
|
2020-06-15 21:26:20 +00:00
|
|
|
return fmt.Errorf("cannot use summary observer and histogram options at the same time")
|
2020-01-16 16:55:09 +00:00
|
|
|
}
|
|
|
|
if currentMapping.SummaryOptions == nil {
|
|
|
|
currentMapping.SummaryOptions = &SummaryOptions{}
|
|
|
|
}
|
|
|
|
if currentMapping.LegacyQuantiles != nil && len(currentMapping.LegacyQuantiles) != 0 {
|
|
|
|
currentMapping.SummaryOptions.Quantiles = currentMapping.LegacyQuantiles
|
|
|
|
}
|
|
|
|
if currentMapping.SummaryOptions.Quantiles == nil || len(currentMapping.SummaryOptions.Quantiles) == 0 {
|
2021-01-28 18:37:56 +00:00
|
|
|
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
|
2020-01-16 16:55:09 +00:00
|
|
|
}
|
2018-08-09 02:41:41 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 12:59:01 +00:00
|
|
|
if currentMapping.Ttl == 0 && n.Defaults.Ttl > 0 {
|
|
|
|
currentMapping.Ttl = n.Defaults.Ttl
|
|
|
|
}
|
2017-03-13 01:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
m.Defaults = n.Defaults
|
|
|
|
m.Mappings = n.Mappings
|
2021-02-07 00:22:29 +00:00
|
|
|
|
|
|
|
// Reset the cache since this function can be used to reload config
|
2021-02-19 16:17:27 +00:00
|
|
|
if m.cache != nil {
|
|
|
|
m.cache.Reset()
|
|
|
|
}
|
2019-04-03 15:36:34 +00:00
|
|
|
|
2018-09-22 00:59:11 +00:00
|
|
|
if n.doFSM {
|
|
|
|
var mappings []string
|
|
|
|
for _, mapping := range n.Mappings {
|
|
|
|
if mapping.MatchType == MatchTypeGlob {
|
|
|
|
mappings = append(mappings, mapping.Match)
|
|
|
|
}
|
2018-09-12 00:31:26 +00:00
|
|
|
}
|
2021-06-07 14:00:08 +00:00
|
|
|
n.FSM.BacktrackingNeeded = fsm.TestIfNeedBacktracking(mappings, n.FSM.OrderingDisabled, m.Logger)
|
2018-09-12 21:44:07 +00:00
|
|
|
|
2018-09-22 00:59:11 +00:00
|
|
|
m.FSM = n.FSM
|
|
|
|
m.doRegex = n.doRegex
|
2018-07-19 21:57:36 +00:00
|
|
|
}
|
2018-09-22 00:59:11 +00:00
|
|
|
m.doFSM = n.doFSM
|
2017-03-13 01:11:05 +00:00
|
|
|
|
2018-08-14 09:31:38 +00:00
|
|
|
if m.MappingsCount != nil {
|
|
|
|
m.MappingsCount.Set(float64(len(n.Mappings)))
|
|
|
|
}
|
2021-06-07 14:00:08 +00:00
|
|
|
|
|
|
|
if m.Logger == nil {
|
|
|
|
m.Logger = log.NewNopLogger()
|
|
|
|
}
|
|
|
|
|
2017-03-13 01:11:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-07 00:22:29 +00:00
|
|
|
func (m *MetricMapper) InitFromFile(fileName string) error {
|
2013-07-05 17:31:53 +00:00
|
|
|
mappingStr, err := ioutil.ReadFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-03 15:36:34 +00:00
|
|
|
|
2021-02-07 00:22:29 +00:00
|
|
|
return m.InitFromYAMLString(string(mappingStr))
|
2019-04-03 15:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 19:13:03 +00:00
|
|
|
// UseCache tells the mapper to use a cache that implements the MetricMapperCache interface.
|
|
|
|
// This cache MUST be thread-safe!
|
2021-02-07 00:22:29 +00:00
|
|
|
func (m *MetricMapper) UseCache(cache MetricMapperCache) {
|
2021-02-19 16:17:27 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2021-02-07 00:22:29 +00:00
|
|
|
m.cache = cache
|
2013-07-05 17:31:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 12:28:38 +00:00
|
|
|
func (m *MetricMapper) GetMapping(statsdMetric string, statsdMetricType MetricType) (*MetricMapping, prometheus.Labels, bool) {
|
2019-04-03 15:36:34 +00:00
|
|
|
m.mutex.RLock()
|
|
|
|
defer m.mutex.RUnlock()
|
2021-02-07 00:22:29 +00:00
|
|
|
|
2021-02-19 16:17:27 +00:00
|
|
|
// only use a cache if one is present
|
|
|
|
if m.cache != nil {
|
|
|
|
result, cached := m.cache.Get(formatKey(statsdMetric, statsdMetricType))
|
|
|
|
if cached {
|
|
|
|
r := result.(MetricMapperCacheResult)
|
|
|
|
return r.Mapping, r.Labels, r.Matched
|
|
|
|
}
|
2019-04-03 15:36:34 +00:00
|
|
|
}
|
2021-02-07 00:22:29 +00:00
|
|
|
|
2018-09-12 00:31:26 +00:00
|
|
|
// glob matching
|
2018-09-22 00:59:11 +00:00
|
|
|
if m.doFSM {
|
2018-09-28 20:13:06 +00:00
|
|
|
finalState, captures := m.FSM.GetMapping(statsdMetric, string(statsdMetricType))
|
|
|
|
if finalState != nil && finalState.Result != nil {
|
2020-01-11 12:03:02 +00:00
|
|
|
v := finalState.Result.(*MetricMapping)
|
|
|
|
result := copyMetricMapping(v)
|
2018-09-28 20:13:06 +00:00
|
|
|
result.Name = result.nameFormatter.Format(captures)
|
|
|
|
|
|
|
|
labels := prometheus.Labels{}
|
|
|
|
for index, formatter := range result.labelFormatters {
|
|
|
|
labels[result.labelKeys[index]] = formatter.Format(captures)
|
|
|
|
}
|
2019-04-03 15:36:34 +00:00
|
|
|
|
2021-02-07 00:22:29 +00:00
|
|
|
r := MetricMapperCacheResult{
|
|
|
|
Mapping: result,
|
|
|
|
Matched: true,
|
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
// add match to cache
|
2021-02-19 16:17:27 +00:00
|
|
|
if m.cache != nil {
|
|
|
|
m.cache.Add(formatKey(statsdMetric, statsdMetricType), r)
|
|
|
|
}
|
2019-04-03 15:36:34 +00:00
|
|
|
|
2018-09-28 20:13:06 +00:00
|
|
|
return result, labels, true
|
|
|
|
} else if !m.doRegex {
|
2018-09-13 18:46:41 +00:00
|
|
|
// if there's no regex match type, return immediately
|
2021-02-19 16:17:27 +00:00
|
|
|
// Add miss to cache
|
|
|
|
if m.cache != nil {
|
|
|
|
m.cache.Add(formatKey(statsdMetric, statsdMetricType), MetricMapperCacheResult{})
|
|
|
|
}
|
2018-09-13 18:46:41 +00:00
|
|
|
return nil, nil, false
|
|
|
|
}
|
2018-07-19 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 00:31:26 +00:00
|
|
|
// regex matching
|
2017-03-13 01:11:05 +00:00
|
|
|
for _, mapping := range m.Mappings {
|
2018-09-12 00:31:26 +00:00
|
|
|
// if a rule don't have regex matching type, the regex field is unset
|
|
|
|
if mapping.regex == nil {
|
|
|
|
continue
|
|
|
|
}
|
2013-07-05 17:31:53 +00:00
|
|
|
matches := mapping.regex.FindStringSubmatchIndex(statsdMetric)
|
|
|
|
if len(matches) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:16:15 +00:00
|
|
|
mapping.Name = string(mapping.regex.ExpandString(
|
|
|
|
[]byte{},
|
|
|
|
mapping.Name,
|
|
|
|
statsdMetric,
|
|
|
|
matches,
|
|
|
|
))
|
|
|
|
|
2018-06-14 06:34:44 +00:00
|
|
|
if mt := mapping.MatchMetricType; mt != "" && mt != statsdMetricType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-06-26 13:56:21 +00:00
|
|
|
labels := prometheus.Labels{}
|
2017-03-13 01:11:05 +00:00
|
|
|
for label, valueExpr := range mapping.Labels {
|
2013-07-05 17:31:53 +00:00
|
|
|
value := mapping.regex.ExpandString([]byte{}, valueExpr, statsdMetric, matches)
|
|
|
|
labels[label] = string(value)
|
|
|
|
}
|
2018-01-16 13:16:15 +00:00
|
|
|
|
2021-02-07 00:22:29 +00:00
|
|
|
r := MetricMapperCacheResult{
|
|
|
|
Mapping: &mapping,
|
|
|
|
Matched: true,
|
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
// Add Match to cache
|
2021-02-19 16:17:27 +00:00
|
|
|
if m.cache != nil {
|
|
|
|
m.cache.Add(formatKey(statsdMetric, statsdMetricType), r)
|
|
|
|
}
|
2019-04-03 15:36:34 +00:00
|
|
|
|
2017-03-13 01:11:05 +00:00
|
|
|
return &mapping, labels, true
|
2013-07-05 17:31:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 00:22:29 +00:00
|
|
|
// Add Miss to cache
|
2021-02-19 16:17:27 +00:00
|
|
|
if m.cache != nil {
|
|
|
|
m.cache.Add(formatKey(statsdMetric, statsdMetricType), MetricMapperCacheResult{})
|
|
|
|
}
|
2017-03-13 01:11:05 +00:00
|
|
|
return nil, nil, false
|
2013-07-05 17:31:53 +00:00
|
|
|
}
|
2020-01-11 12:03:02 +00:00
|
|
|
|
|
|
|
// make a shallow copy so that we do not overwrite name
|
|
|
|
// as multiple names can be matched by same mapping
|
|
|
|
func copyMetricMapping(in *MetricMapping) *MetricMapping {
|
|
|
|
var out MetricMapping
|
|
|
|
out = *in
|
|
|
|
return &out
|
|
|
|
}
|