Change prometheus replacer to reverse regex replacer

closes #1474
This commit is contained in:
Cameron Sparr 2016-07-18 11:45:25 +01:00
parent 6afe9ceef1
commit b4a6d9c647
1 changed files with 4 additions and 5 deletions

View File

@ -5,7 +5,6 @@ import (
"log"
"net/http"
"regexp"
"strings"
"sync"
"github.com/influxdata/telegraf"
@ -14,7 +13,7 @@ import (
)
var (
sanitizedChars = strings.NewReplacer("/", "_", "@", "_", " ", "_", "-", "_", ".", "_")
invalidNameCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
// Prometheus metric names must match this regex
// see https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
@ -111,12 +110,12 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
for _, point := range metrics {
key := point.Name()
key = sanitizedChars.Replace(key)
key = invalidNameCharRE.ReplaceAllString(key, "_")
var labels []string
l := prometheus.Labels{}
for k, v := range point.Tags() {
k = sanitizedChars.Replace(k)
k = invalidNameCharRE.ReplaceAllString(k, "_")
if len(k) == 0 {
continue
}
@ -137,7 +136,7 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
}
// sanitize the measurement name
n = sanitizedChars.Replace(n)
n = invalidNameCharRE.ReplaceAllString(n, "_")
var mname string
if n == "value" {
mname = key