2019-04-03 15:36:34 +00:00
|
|
|
// Copyright 2019 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 (
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2020-11-20 19:14:14 +00:00
|
|
|
type CacheMetrics struct {
|
|
|
|
CacheLength prometheus.Gauge
|
|
|
|
CacheGetsTotal prometheus.Counter
|
|
|
|
CacheHitsTotal prometheus.Counter
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCacheMetrics(reg prometheus.Registerer) *CacheMetrics {
|
|
|
|
var m CacheMetrics
|
|
|
|
|
|
|
|
m.CacheLength = prometheus.NewGauge(
|
2019-04-03 15:36:34 +00:00
|
|
|
prometheus.GaugeOpts{
|
2020-01-10 14:32:16 +00:00
|
|
|
Name: "statsd_metric_mapper_cache_length",
|
2019-04-03 15:36:34 +00:00
|
|
|
Help: "The count of unique metrics currently cached.",
|
|
|
|
},
|
|
|
|
)
|
2020-11-20 19:14:14 +00:00
|
|
|
m.CacheGetsTotal = prometheus.NewCounter(
|
2020-01-02 15:06:04 +00:00
|
|
|
prometheus.CounterOpts{
|
2020-01-10 14:32:16 +00:00
|
|
|
Name: "statsd_metric_mapper_cache_gets_total",
|
|
|
|
Help: "The count of total metric cache gets.",
|
2020-01-02 15:06:04 +00:00
|
|
|
},
|
|
|
|
)
|
2020-11-20 19:14:14 +00:00
|
|
|
m.CacheHitsTotal = prometheus.NewCounter(
|
2020-01-02 15:06:04 +00:00
|
|
|
prometheus.CounterOpts{
|
2020-01-10 14:32:16 +00:00
|
|
|
Name: "statsd_metric_mapper_cache_hits_total",
|
|
|
|
Help: "The count of total metric cache hits.",
|
2020-01-02 15:06:04 +00:00
|
|
|
},
|
|
|
|
)
|
2020-11-20 19:14:14 +00:00
|
|
|
|
|
|
|
if reg != nil {
|
|
|
|
reg.MustRegister(m.CacheLength)
|
|
|
|
reg.MustRegister(m.CacheGetsTotal)
|
|
|
|
reg.MustRegister(m.CacheHitsTotal)
|
|
|
|
}
|
|
|
|
return &m
|
|
|
|
}
|
2019-04-03 15:36:34 +00:00
|
|
|
|
|
|
|
type MetricMapperCacheResult struct {
|
|
|
|
Mapping *MetricMapping
|
|
|
|
Matched bool
|
|
|
|
Labels prometheus.Labels
|
|
|
|
}
|
|
|
|
|
2021-02-19 19:13:03 +00:00
|
|
|
// MetricMapperCache MUST be thread-safe and should be instrumented with CacheMetrics
|
2019-04-03 15:36:34 +00:00
|
|
|
type MetricMapperCache interface {
|
2021-02-07 00:22:29 +00:00
|
|
|
// Get a cached result
|
|
|
|
Get(metricKey string) (interface{}, bool)
|
|
|
|
// Add a statsd MetricMapperResult to the cache
|
|
|
|
Add(metricKey string, result interface{}) // Add an item to the cache
|
|
|
|
// Reset clears the cache for config reloads
|
|
|
|
Reset()
|
2019-04-03 15:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 00:22:29 +00:00
|
|
|
func formatKey(metricString string, metricType MetricType) string {
|
|
|
|
return string(metricType) + "." + metricString
|
2019-04-03 15:36:34 +00:00
|
|
|
}
|