2013-07-05 17:31:53 +00:00
|
|
|
// Copyright (c) 2013, Prometheus Team
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2013-07-08 18:37:12 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2013-07-05 17:31:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
identifierRE = `[a-zA-Z_][a-zA-Z0-9_]+`
|
|
|
|
metricLineRE = regexp.MustCompile(`^(\*\.|` + identifierRE + `\.)+(\*|` + identifierRE + `)$`)
|
|
|
|
labelLineRE = regexp.MustCompile(`^(` + identifierRE + `)\s*=\s*"(.*)"$`)
|
2013-07-12 12:27:51 +00:00
|
|
|
metricNameRE = regexp.MustCompile(`^` + identifierRE + `$`)
|
2013-07-05 17:31:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type metricMapping struct {
|
|
|
|
regex *regexp.Regexp
|
2014-06-26 13:56:21 +00:00
|
|
|
labels prometheus.Labels
|
2013-07-05 17:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type metricMapper struct {
|
|
|
|
mappings []metricMapping
|
2013-07-08 18:37:12 +00:00
|
|
|
mutex sync.Mutex
|
2013-07-05 17:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type configLoadStates int
|
|
|
|
|
|
|
|
const (
|
|
|
|
SEARCHING configLoadStates = iota
|
|
|
|
METRIC_DEFINITION
|
|
|
|
)
|
|
|
|
|
2013-07-08 18:37:12 +00:00
|
|
|
func (m *metricMapper) initFromString(fileContents string) error {
|
2013-07-05 17:31:53 +00:00
|
|
|
lines := strings.Split(fileContents, "\n")
|
|
|
|
state := SEARCHING
|
|
|
|
|
2013-07-08 18:37:12 +00:00
|
|
|
parsedMappings := []metricMapping{}
|
2014-06-26 13:56:21 +00:00
|
|
|
currentMapping := metricMapping{labels: prometheus.Labels{}}
|
2013-07-05 17:31:53 +00:00
|
|
|
for i, line := range lines {
|
|
|
|
line := strings.TrimSpace(line)
|
|
|
|
|
|
|
|
switch state {
|
|
|
|
case SEARCHING:
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !metricLineRE.MatchString(line) {
|
|
|
|
return fmt.Errorf("Line %d: expected metric match line, got: %s", i, line)
|
|
|
|
}
|
2013-07-08 18:37:12 +00:00
|
|
|
|
|
|
|
// Translate the glob-style metric match line into a proper regex that we
|
|
|
|
// can use to match metrics later on.
|
2013-07-05 17:31:53 +00:00
|
|
|
metricRe := strings.Replace(line, ".", "\\.", -1)
|
|
|
|
metricRe = strings.Replace(metricRe, "*", "([^.]+)", -1)
|
2013-07-08 18:37:12 +00:00
|
|
|
currentMapping.regex = regexp.MustCompile("^" + metricRe + "$")
|
|
|
|
|
2013-07-05 17:31:53 +00:00
|
|
|
state = METRIC_DEFINITION
|
|
|
|
|
|
|
|
case METRIC_DEFINITION:
|
|
|
|
if line == "" {
|
2013-07-08 18:37:12 +00:00
|
|
|
if len(currentMapping.labels) == 0 {
|
2013-07-05 17:31:53 +00:00
|
|
|
return fmt.Errorf("Line %d: metric mapping didn't set any labels", i)
|
|
|
|
}
|
2013-07-08 18:37:12 +00:00
|
|
|
if _, ok := currentMapping.labels["name"]; !ok {
|
2013-07-05 17:31:53 +00:00
|
|
|
return fmt.Errorf("Line %d: metric mapping didn't set a metric name", i)
|
|
|
|
}
|
|
|
|
|
2013-07-08 18:37:12 +00:00
|
|
|
parsedMappings = append(parsedMappings, currentMapping)
|
2013-07-05 17:31:53 +00:00
|
|
|
|
|
|
|
state = SEARCHING
|
2014-06-26 13:56:21 +00:00
|
|
|
currentMapping = metricMapping{labels: prometheus.Labels{}}
|
2013-07-05 17:31:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := labelLineRE.FindStringSubmatch(line)
|
|
|
|
if len(matches) != 3 {
|
|
|
|
return fmt.Errorf("Line %d: expected label mapping line, got: %s", i, line)
|
|
|
|
}
|
2013-07-12 12:27:51 +00:00
|
|
|
label, value := matches[1], matches[2]
|
|
|
|
if label == "name" && !metricNameRE.MatchString(value) {
|
|
|
|
return fmt.Errorf("Line %d: metric name '%s' doesn't match regex '%s'", i, value, metricNameRE)
|
|
|
|
}
|
|
|
|
currentMapping.labels[label] = value
|
2013-07-05 17:31:53 +00:00
|
|
|
default:
|
|
|
|
panic("illegal state")
|
|
|
|
}
|
|
|
|
}
|
2013-07-08 18:37:12 +00:00
|
|
|
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
m.mappings = parsedMappings
|
|
|
|
|
2014-06-26 13:56:21 +00:00
|
|
|
mappingsCount.Set(float64(len(parsedMappings)))
|
2013-07-08 18:37:12 +00:00
|
|
|
|
2013-07-05 17:31:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-08 18:37:12 +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
|
|
|
|
}
|
2013-07-08 18:37:12 +00:00
|
|
|
return m.initFromString(string(mappingStr))
|
2013-07-05 17:31:53 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 13:56:21 +00:00
|
|
|
func (m *metricMapper) getMapping(statsdMetric string) (labels prometheus.Labels, present bool) {
|
2013-07-08 18:37:12 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
for _, mapping := range m.mappings {
|
2013-07-05 17:31:53 +00:00
|
|
|
matches := mapping.regex.FindStringSubmatchIndex(statsdMetric)
|
|
|
|
if len(matches) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-06-26 13:56:21 +00:00
|
|
|
labels := prometheus.Labels{}
|
2013-07-05 17:31:53 +00:00
|
|
|
for label, valueExpr := range mapping.labels {
|
|
|
|
value := mapping.regex.ExpandString([]byte{}, valueExpr, statsdMetric, matches)
|
|
|
|
labels[label] = string(value)
|
|
|
|
}
|
|
|
|
return labels, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|