diff --git a/pkg/mapper/mapper.go b/pkg/mapper/mapper.go index 35101cc..808f87d 100644 --- a/pkg/mapper/mapper.go +++ b/pkg/mapper/mapper.go @@ -237,7 +237,7 @@ func (m *MetricMapper) InitCache(cacheSize int) { func (m *MetricMapper) GetMapping(statsdMetric string, statsdMetricType MetricType) (*MetricMapping, prometheus.Labels, bool) { m.mutex.RLock() defer m.mutex.RUnlock() - result, cached := m.cache.Get(statsdMetric) + result, cached := m.cache.Get(statsdMetric, statsdMetricType) if cached { return result.Mapping, result.Labels, result.Matched } @@ -253,12 +253,12 @@ func (m *MetricMapper) GetMapping(statsdMetric string, statsdMetricType MetricTy labels[result.labelKeys[index]] = formatter.Format(captures) } - m.cache.AddMatch(statsdMetric, result, labels) + m.cache.AddMatch(statsdMetric, statsdMetricType, result, labels) return result, labels, true } else if !m.doRegex { // if there's no regex match type, return immediately - m.cache.AddMiss(statsdMetric) + m.cache.AddMiss(statsdMetric, statsdMetricType) return nil, nil, false } } @@ -291,11 +291,11 @@ func (m *MetricMapper) GetMapping(statsdMetric string, statsdMetricType MetricTy labels[label] = string(value) } - m.cache.AddMatch(statsdMetric, &mapping, labels) + m.cache.AddMatch(statsdMetric, statsdMetricType, &mapping, labels) return &mapping, labels, true } - m.cache.AddMiss(statsdMetric) + m.cache.AddMiss(statsdMetric, statsdMetricType) return nil, nil, false } diff --git a/pkg/mapper/mapper_cache.go b/pkg/mapper/mapper_cache.go index bbf85c2..d498df6 100644 --- a/pkg/mapper/mapper_cache.go +++ b/pkg/mapper/mapper_cache.go @@ -14,6 +14,7 @@ package mapper import ( + "fmt" "github.com/hashicorp/golang-lru" "github.com/prometheus/client_golang/prometheus" ) @@ -34,9 +35,9 @@ type MetricMapperCacheResult struct { } type MetricMapperCache interface { - Get(metricString string) (*MetricMapperCacheResult, bool) - AddMatch(metricString string, mapping *MetricMapping, labels prometheus.Labels) - AddMiss(metricString string) + Get(metricString string, metricType MetricType) (*MetricMapperCacheResult, bool) + AddMatch(metricString string, metricType MetricType, mapping *MetricMapping, labels prometheus.Labels) + AddMiss(metricString string, metricType MetricType) } type MetricMapperLRUCache struct { @@ -57,42 +58,46 @@ func NewMetricMapperCache(size int) (*MetricMapperLRUCache, error) { return &MetricMapperLRUCache{cache: cache}, nil } -func (m *MetricMapperLRUCache) Get(metricString string) (*MetricMapperCacheResult, bool) { - if result, ok := m.cache.Get(metricString); ok { +func (m *MetricMapperLRUCache) Get(metricString string, metricType MetricType) (*MetricMapperCacheResult, bool) { + if result, ok := m.cache.Get(formatKey(metricString, metricType)); ok { return result.(*MetricMapperCacheResult), true } else { return nil, false } } -func (m *MetricMapperLRUCache) AddMatch(metricString string, mapping *MetricMapping, labels prometheus.Labels) { +func (m *MetricMapperLRUCache) AddMatch(metricString string, metricType MetricType, mapping *MetricMapping, labels prometheus.Labels) { go m.trackCacheLength() - m.cache.Add(metricString, &MetricMapperCacheResult{Mapping: mapping, Matched: true, Labels: labels}) + m.cache.Add(formatKey(metricString, metricType), &MetricMapperCacheResult{Mapping: mapping, Matched: true, Labels: labels}) } -func (m *MetricMapperLRUCache) AddMiss(metricString string) { +func (m *MetricMapperLRUCache) AddMiss(metricString string, metricType MetricType) { go m.trackCacheLength() - m.cache.Add(metricString, &MetricMapperCacheResult{Matched: false}) + m.cache.Add(formatKey(metricString, metricType), &MetricMapperCacheResult{Matched: false}) } func (m *MetricMapperLRUCache) trackCacheLength() { cacheLength.Set(float64(m.cache.Len())) } +func formatKey(metricString string, metricType MetricType) string { + return fmt.Sprintf("%s.%s", string(metricType), metricString) +} + func NewMetricMapperNoopCache() *MetricMapperNoopCache { cacheLength.Set(0) return &MetricMapperNoopCache{} } -func (m *MetricMapperNoopCache) Get(metricString string) (*MetricMapperCacheResult, bool) { +func (m *MetricMapperNoopCache) Get(metricString string, metricType MetricType) (*MetricMapperCacheResult, bool) { return nil, false } -func (m *MetricMapperNoopCache) AddMatch(metricString string, mapping *MetricMapping, labels prometheus.Labels) { +func (m *MetricMapperNoopCache) AddMatch(metricString string, metricType MetricType, mapping *MetricMapping, labels prometheus.Labels) { return } -func (m *MetricMapperNoopCache) AddMiss(metricString string) { +func (m *MetricMapperNoopCache) AddMiss(metricString string, metricType MetricType) { return }