Skip to content

Commit

Permalink
Adding mapping cache
Browse files Browse the repository at this point in the history
Signed-off-by: SpencerMalone <[email protected]>
  • Loading branch information
SpencerMalone committed Apr 4, 2019
1 parent 71df5a3 commit 74f4bbf
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 39 deletions.
4 changes: 2 additions & 2 deletions exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mappings:
name: "${1}"
`
testMapper := &mapper.MetricMapper{}
err := testMapper.InitFromYAMLString(config)
err := testMapper.InitFromYAMLString(config, false)
if err != nil {
t.Fatalf("Config load error: %s %s", config, err)
}
Expand Down Expand Up @@ -245,7 +245,7 @@ mappings:
`
// Create mapper from config and start an Exporter with a synchronous channel
testMapper := &mapper.MetricMapper{}
err := testMapper.InitFromYAMLString(config)
err := testMapper.InitFromYAMLString(config, false)
if err != nil {
t.Fatalf("Config load error: %s %s", config, err)
}
Expand Down
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func tcpAddrFromString(addr string) *net.TCPAddr {
}
}

func watchConfig(fileName string, mapper *mapper.MetricMapper) {
func watchConfig(fileName string, mapper *mapper.MetricMapper, useMetricCache bool) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
Expand All @@ -103,7 +103,7 @@ func watchConfig(fileName string, mapper *mapper.MetricMapper) {
select {
case ev := <-watcher.Event:
log.Infof("Config file changed (%s), attempting reload", ev)
err = mapper.InitFromFile(fileName)
err = mapper.InitFromFile(fileName, useMetricCache)
if err != nil {
log.Errorln("Error reloading config:", err)
configLoads.WithLabelValues("failure").Inc()
Expand Down Expand Up @@ -144,6 +144,8 @@ func main() {
mappingConfig = kingpin.Flag("statsd.mapping-config", "Metric mapping configuration file name.").String()
readBuffer = kingpin.Flag("statsd.read-buffer", "Size (in bytes) of the operating system's transmit read buffer associated with the UDP connection. Please make sure the kernel parameters net.core.rmem_max is set to a value greater than the value specified.").Int()
dumpFSMPath = kingpin.Flag("debug.dump-fsm", "The path to dump internal FSM generated for glob matching as Dot file.").Default("").String()

useMetricCache = kingpin.Flag("statsd.enable-mapping-cache", "Should you use the metric mapping cache. Increases throughput at the cost of memory").Default("true").Bool()
)

log.AddFlags(kingpin.CommandLine)
Expand Down Expand Up @@ -197,7 +199,7 @@ func main() {

mapper := &mapper.MetricMapper{MappingsCount: mappingsCount}
if *mappingConfig != "" {
err := mapper.InitFromFile(*mappingConfig)
err := mapper.InitFromFile(*mappingConfig, *useMetricCache)
if err != nil {
log.Fatal("Error loading config:", err)
}
Expand All @@ -207,7 +209,7 @@ func main() {
log.Fatal("Error dumping FSM:", err)
}
}
go watchConfig(*mappingConfig, mapper)
go watchConfig(*mappingConfig, mapper, *useMetricCache)
}
exporter := NewExporter(mapper)
exporter.Listen(events)
Expand Down
42 changes: 33 additions & 9 deletions pkg/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ type MetricMapper struct {
FSM *fsm.FSM
doFSM bool
doRegex bool
mutex sync.Mutex
useCache bool
cache *MetricMapperCache
mutex sync.RWMutex
sha string

MappingsCount prometheus.Gauge
}
Expand Down Expand Up @@ -83,7 +86,7 @@ var defaultQuantiles = []metricObjective{
{Quantile: 0.99, Error: 0.001},
}

func (m *MetricMapper) InitFromYAMLString(fileContents string) error {
func (m *MetricMapper) InitFromYAMLString(fileContents string, useCache bool) error {
var n MetricMapper

if err := yaml.Unmarshal([]byte(fileContents), &n); err != nil {
Expand Down Expand Up @@ -189,6 +192,8 @@ func (m *MetricMapper) InitFromYAMLString(fileContents string) error {

m.Defaults = n.Defaults
m.Mappings = n.Mappings
m.cache = NewMetricMapperCache()
m.useCache = useCache
if n.doFSM {
var mappings []string
for _, mapping := range n.Mappings {
Expand All @@ -206,19 +211,27 @@ func (m *MetricMapper) InitFromYAMLString(fileContents string) error {
if m.MappingsCount != nil {
m.MappingsCount.Set(float64(len(n.Mappings)))
}

return nil
}

func (m *MetricMapper) InitFromFile(fileName string) error {
func (m *MetricMapper) InitFromFile(fileName string, useCache bool) error {
mappingStr, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
return m.InitFromYAMLString(string(mappingStr))

return m.InitFromYAMLString(string(mappingStr), useCache)
}

func (m *MetricMapper) GetMapping(statsdMetric string, statsdMetricType MetricType) (*MetricMapping, prometheus.Labels, bool) {
m.mutex.RLock()
defer m.mutex.RUnlock()
if m.useCache {
result, cached := m.cache.Get(statsdMetric)
if cached {
return result.Mapping, result.Labels, result.Matched
}
}
// glob matching
if m.doFSM {
finalState, captures := m.FSM.GetMapping(statsdMetric, string(statsdMetricType))
Expand All @@ -230,17 +243,22 @@ func (m *MetricMapper) GetMapping(statsdMetric string, statsdMetricType MetricTy
for index, formatter := range result.labelFormatters {
labels[result.labelKeys[index]] = formatter.Format(captures)
}

if m.useCache {
m.cache.AddMatch(statsdMetric, result, labels)
}

return result, labels, true
} else if !m.doRegex {
// if there's no regex match type, return immediately
if m.useCache {
m.cache.AddMiss(statsdMetric)
}
return nil, nil, false
}
}

// regex matching
m.mutex.Lock()
defer m.mutex.Unlock()

for _, mapping := range m.Mappings {
// if a rule don't have regex matching type, the regex field is unset
if mapping.regex == nil {
Expand Down Expand Up @@ -268,8 +286,14 @@ func (m *MetricMapper) GetMapping(statsdMetric string, statsdMetricType MetricTy
labels[label] = string(value)
}

if m.useCache {
m.cache.AddMatch(statsdMetric, &mapping, labels)
}

return &mapping, labels, true
}

if m.useCache {
m.cache.AddMiss(statsdMetric)
}
return nil, nil, false
}
Loading

0 comments on commit 74f4bbf

Please sign in to comment.