chore(outputs.wavefront): Reduce code duplication (#12332)
This commit is contained in:
parent
dae0d82b9a
commit
19cc5a7abf
|
|
@ -42,24 +42,7 @@ type Wavefront struct {
|
||||||
Log telegraf.Logger `toml:"-"`
|
Log telegraf.Logger `toml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// catch many of the invalid chars that could appear in a metric or tag name
|
// instead of Sanitize which may miss some special characters we can use a regex pattern, but this is significantly slower than Sanitize
|
||||||
var sanitizedChars = strings.NewReplacer(
|
|
||||||
"!", "-", "@", "-", "#", "-", "$", "-", "%", "-", "^", "-", "&", "-",
|
|
||||||
"*", "-", "(", "-", ")", "-", "+", "-", "`", "-", "'", "-", "\"", "-",
|
|
||||||
"[", "-", "]", "-", "{", "-", "}", "-", ":", "-", ";", "-", "<", "-",
|
|
||||||
">", "-", ",", "-", "?", "-", "/", "-", "\\", "-", "|", "-", " ", "-",
|
|
||||||
"=", "-",
|
|
||||||
)
|
|
||||||
|
|
||||||
// catch many of the invalid chars that could appear in a metric or tag name
|
|
||||||
var strictSanitizedChars = strings.NewReplacer(
|
|
||||||
"!", "-", "@", "-", "#", "-", "$", "-", "%", "-", "^", "-", "&", "-",
|
|
||||||
"*", "-", "(", "-", ")", "-", "+", "-", "`", "-", "'", "-", "\"", "-",
|
|
||||||
"[", "-", "]", "-", "{", "-", "}", "-", ":", "-", ";", "-", "<", "-",
|
|
||||||
">", "-", "?", "-", "\\", "-", "|", "-", " ", "-", "=", "-",
|
|
||||||
)
|
|
||||||
|
|
||||||
// instead of Replacer which may miss some special characters we can use a regex pattern, but this is significantly slower than Replacer
|
|
||||||
var sanitizedRegex = regexp.MustCompile(`[^a-zA-Z\d_.-]`)
|
var sanitizedRegex = regexp.MustCompile(`[^a-zA-Z\d_.-]`)
|
||||||
|
|
||||||
var tagValueReplacer = strings.NewReplacer("*", "-")
|
var tagValueReplacer = strings.NewReplacer("*", "-")
|
||||||
|
|
@ -166,10 +149,8 @@ func (w *Wavefront) buildMetrics(m telegraf.Metric) []*serializer.MetricPoint {
|
||||||
|
|
||||||
if w.UseRegex {
|
if w.UseRegex {
|
||||||
name = sanitizedRegex.ReplaceAllLiteralString(name, "-")
|
name = sanitizedRegex.ReplaceAllLiteralString(name, "-")
|
||||||
} else if w.UseStrict {
|
|
||||||
name = strictSanitizedChars.Replace(name)
|
|
||||||
} else {
|
} else {
|
||||||
name = sanitizedChars.Replace(name)
|
name = serializer.Sanitize(w.UseStrict, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.ConvertPaths {
|
if w.ConvertPaths {
|
||||||
|
|
@ -245,10 +226,8 @@ func (w *Wavefront) buildTags(mTags map[string]string) (string, map[string]strin
|
||||||
var key string
|
var key string
|
||||||
if w.UseRegex {
|
if w.UseRegex {
|
||||||
key = sanitizedRegex.ReplaceAllLiteralString(k, "-")
|
key = sanitizedRegex.ReplaceAllLiteralString(k, "-")
|
||||||
} else if w.UseStrict {
|
|
||||||
key = strictSanitizedChars.Replace(k)
|
|
||||||
} else {
|
} else {
|
||||||
key = sanitizedChars.Replace(k)
|
key = serializer.Sanitize(w.UseStrict, k)
|
||||||
}
|
}
|
||||||
val := tagValueReplacer.Replace(v)
|
val := tagValueReplacer.Replace(v)
|
||||||
if w.TruncateTags {
|
if w.TruncateTags {
|
||||||
|
|
|
||||||
|
|
@ -380,7 +380,7 @@ func TestDefaults(t *testing.T) {
|
||||||
require.Equal(t, 10000, defaultWavefront.HTTPMaximumBatchSize)
|
require.Equal(t, 10000, defaultWavefront.HTTPMaximumBatchSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Benchmarks to test performance of string replacement via Regex and Replacer
|
// Benchmarks to test performance of string replacement via Regex and Sanitize
|
||||||
var testString = "this_is*my!test/string\\for=replacement"
|
var testString = "this_is*my!test/string\\for=replacement"
|
||||||
|
|
||||||
func BenchmarkReplaceAllString(b *testing.B) {
|
func BenchmarkReplaceAllString(b *testing.B) {
|
||||||
|
|
@ -397,6 +397,6 @@ func BenchmarkReplaceAllLiteralString(b *testing.B) {
|
||||||
|
|
||||||
func BenchmarkReplacer(b *testing.B) {
|
func BenchmarkReplacer(b *testing.B) {
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
sanitizedChars.Replace(testString)
|
serializer.Sanitize(false, testString)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
package wavefront
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
// catch many of the invalid chars that could appear in a metric or tag name
|
||||||
|
var sanitizedChars = strings.NewReplacer(
|
||||||
|
"!", "-", "@", "-", "#", "-", "$", "-", "%", "-", "^", "-", "&", "-",
|
||||||
|
"*", "-", "(", "-", ")", "-", "+", "-", "`", "-", "'", "-", "\"", "-",
|
||||||
|
"[", "-", "]", "-", "{", "-", "}", "-", ":", "-", ";", "-", "<", "-",
|
||||||
|
">", "-", ",", "-", "?", "-", "/", "-", "\\", "-", "|", "-", " ", "-",
|
||||||
|
"=", "-",
|
||||||
|
)
|
||||||
|
|
||||||
|
// catch many of the invalid chars that could appear in a metric or tag name
|
||||||
|
var strictSanitizedChars = strings.NewReplacer(
|
||||||
|
"!", "-", "@", "-", "#", "-", "$", "-", "%", "-", "^", "-", "&", "-",
|
||||||
|
"*", "-", "(", "-", ")", "-", "+", "-", "`", "-", "'", "-", "\"", "-",
|
||||||
|
"[", "-", "]", "-", "{", "-", "}", "-", ":", "-", ";", "-", "<", "-",
|
||||||
|
">", "-", "?", "-", "\\", "-", "|", "-", " ", "-", "=", "-",
|
||||||
|
)
|
||||||
|
|
||||||
|
var tagValueReplacer = strings.NewReplacer("\"", "\\\"", "*", "-")
|
||||||
|
|
||||||
|
var pathReplacer = strings.NewReplacer("_", ".")
|
||||||
|
|
||||||
|
func Sanitize(strict bool, val string) string {
|
||||||
|
if strict {
|
||||||
|
return strictSanitizedChars.Replace(val)
|
||||||
|
}
|
||||||
|
return sanitizedChars.Replace(val)
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,6 @@ package wavefront
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
|
@ -19,27 +18,6 @@ type WavefrontSerializer struct {
|
||||||
mu sync.Mutex // buffer mutex
|
mu sync.Mutex // buffer mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// catch many of the invalid chars that could appear in a metric or tag name
|
|
||||||
var sanitizedChars = strings.NewReplacer(
|
|
||||||
"!", "-", "@", "-", "#", "-", "$", "-", "%", "-", "^", "-", "&", "-",
|
|
||||||
"*", "-", "(", "-", ")", "-", "+", "-", "`", "-", "'", "-", "\"", "-",
|
|
||||||
"[", "-", "]", "-", "{", "-", "}", "-", ":", "-", ";", "-", "<", "-",
|
|
||||||
">", "-", ",", "-", "?", "-", "/", "-", "\\", "-", "|", "-", " ", "-",
|
|
||||||
"=", "-",
|
|
||||||
)
|
|
||||||
|
|
||||||
// catch many of the invalid chars that could appear in a metric or tag name
|
|
||||||
var strictSanitizedChars = strings.NewReplacer(
|
|
||||||
"!", "-", "@", "-", "#", "-", "$", "-", "%", "-", "^", "-", "&", "-",
|
|
||||||
"*", "-", "(", "-", ")", "-", "+", "-", "`", "-", "'", "-", "\"", "-",
|
|
||||||
"[", "-", "]", "-", "{", "-", "}", "-", ":", "-", ";", "-", "<", "-",
|
|
||||||
">", "-", "?", "-", "\\", "-", "|", "-", " ", "-", "=", "-",
|
|
||||||
)
|
|
||||||
|
|
||||||
var tagValueReplacer = strings.NewReplacer("\"", "\\\"", "*", "-")
|
|
||||||
|
|
||||||
var pathReplacer = strings.NewReplacer("_", ".")
|
|
||||||
|
|
||||||
type MetricPoint struct {
|
type MetricPoint struct {
|
||||||
Metric string
|
Metric string
|
||||||
Value float64
|
Value float64
|
||||||
|
|
@ -70,11 +48,7 @@ func (s *WavefrontSerializer) serializeMetric(m telegraf.Metric) {
|
||||||
name = s.Prefix + m.Name() + metricSeparator + fieldName
|
name = s.Prefix + m.Name() + metricSeparator + fieldName
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.UseStrict {
|
name = Sanitize(s.UseStrict, name)
|
||||||
name = strictSanitizedChars.Replace(name)
|
|
||||||
} else {
|
|
||||||
name = sanitizedChars.Replace(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !s.DisablePrefixConversions {
|
if !s.DisablePrefixConversions {
|
||||||
name = pathReplacer.Replace(name)
|
name = pathReplacer.Replace(name)
|
||||||
|
|
@ -182,11 +156,7 @@ func formatMetricPoint(b *buffer, metricPoint *MetricPoint, s *WavefrontSerializ
|
||||||
|
|
||||||
for k, v := range metricPoint.Tags {
|
for k, v := range metricPoint.Tags {
|
||||||
b.WriteString(` "`)
|
b.WriteString(` "`)
|
||||||
if s.UseStrict {
|
b.WriteString(Sanitize(s.UseStrict, k))
|
||||||
b.WriteString(strictSanitizedChars.Replace(k))
|
|
||||||
} else {
|
|
||||||
b.WriteString(sanitizedChars.Replace(k))
|
|
||||||
}
|
|
||||||
b.WriteString(`"="`)
|
b.WriteString(`"="`)
|
||||||
b.WriteString(tagValueReplacer.Replace(v))
|
b.WriteString(tagValueReplacer.Replace(v))
|
||||||
b.WriteChar('"')
|
b.WriteChar('"')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue