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 (
|
2019-12-31 21:34:47 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2019-04-03 15:36:34 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cacheLength = prometheus.NewGauge(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "statsd_exporter_cache_length",
|
|
|
|
Help: "The count of unique metrics currently cached.",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
type MetricMapperCacheResult struct {
|
|
|
|
Mapping *MetricMapping
|
|
|
|
Matched bool
|
|
|
|
Labels prometheus.Labels
|
|
|
|
}
|
|
|
|
|
|
|
|
type MetricMapperCache interface {
|
2019-06-06 08:31:11 +00:00
|
|
|
Get(metricString string, metricType MetricType) (*MetricMapperCacheResult, bool)
|
|
|
|
AddMatch(metricString string, metricType MetricType, mapping *MetricMapping, labels prometheus.Labels)
|
|
|
|
AddMiss(metricString string, metricType MetricType)
|
2019-04-03 15:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MetricMapperLRUCache struct {
|
|
|
|
MetricMapperCache
|
|
|
|
cache *lru.Cache
|
|
|
|
}
|
|
|
|
|
|
|
|
type MetricMapperNoopCache struct {
|
|
|
|
MetricMapperCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMetricMapperCache(size int) (*MetricMapperLRUCache, error) {
|
|
|
|
cacheLength.Set(0)
|
|
|
|
cache, err := lru.New(size)
|
|
|
|
if err != nil {
|
|
|
|
return &MetricMapperLRUCache{}, err
|
|
|
|
}
|
|
|
|
return &MetricMapperLRUCache{cache: cache}, nil
|
|
|
|
}
|
|
|
|
|
2019-06-06 08:31:11 +00:00
|
|
|
func (m *MetricMapperLRUCache) Get(metricString string, metricType MetricType) (*MetricMapperCacheResult, bool) {
|
|
|
|
if result, ok := m.cache.Get(formatKey(metricString, metricType)); ok {
|
2019-04-03 15:36:34 +00:00
|
|
|
return result.(*MetricMapperCacheResult), true
|
|
|
|
} else {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 08:31:11 +00:00
|
|
|
func (m *MetricMapperLRUCache) AddMatch(metricString string, metricType MetricType, mapping *MetricMapping, labels prometheus.Labels) {
|
2019-04-03 15:36:34 +00:00
|
|
|
go m.trackCacheLength()
|
2019-06-06 08:31:11 +00:00
|
|
|
m.cache.Add(formatKey(metricString, metricType), &MetricMapperCacheResult{Mapping: mapping, Matched: true, Labels: labels})
|
2019-04-03 15:36:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 08:31:11 +00:00
|
|
|
func (m *MetricMapperLRUCache) AddMiss(metricString string, metricType MetricType) {
|
2019-04-03 15:36:34 +00:00
|
|
|
go m.trackCacheLength()
|
2019-06-06 08:31:11 +00:00
|
|
|
m.cache.Add(formatKey(metricString, metricType), &MetricMapperCacheResult{Matched: false})
|
2019-04-03 15:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MetricMapperLRUCache) trackCacheLength() {
|
|
|
|
cacheLength.Set(float64(m.cache.Len()))
|
|
|
|
}
|
|
|
|
|
2019-06-06 08:31:11 +00:00
|
|
|
func formatKey(metricString string, metricType MetricType) string {
|
2019-12-31 21:34:47 +00:00
|
|
|
return string(metricType) + "." + metricString
|
2019-06-06 08:31:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 15:36:34 +00:00
|
|
|
func NewMetricMapperNoopCache() *MetricMapperNoopCache {
|
|
|
|
cacheLength.Set(0)
|
|
|
|
return &MetricMapperNoopCache{}
|
|
|
|
}
|
|
|
|
|
2019-06-06 08:31:11 +00:00
|
|
|
func (m *MetricMapperNoopCache) Get(metricString string, metricType MetricType) (*MetricMapperCacheResult, bool) {
|
2019-04-03 15:36:34 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-06-06 08:31:11 +00:00
|
|
|
func (m *MetricMapperNoopCache) AddMatch(metricString string, metricType MetricType, mapping *MetricMapping, labels prometheus.Labels) {
|
2019-04-03 15:36:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-06 08:31:11 +00:00
|
|
|
func (m *MetricMapperNoopCache) AddMiss(metricString string, metricType MetricType) {
|
2019-04-03 15:36:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(cacheLength)
|
|
|
|
}
|