Add truncate_tags setting to wavefront output (#7503)
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -21,6 +22,7 @@ func defaultWavefront() *Wavefront {
|
||||
ConvertPaths: true,
|
||||
ConvertBool: true,
|
||||
UseRegex: false,
|
||||
Log: testutil.Logger{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +66,7 @@ func TestBuildMetrics(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, mt := range metricTests {
|
||||
ml := buildMetrics(mt.metric, w)
|
||||
ml := w.buildMetrics(mt.metric)
|
||||
for i, line := range ml {
|
||||
if mt.metricPoints[i].Metric != line.Metric || mt.metricPoints[i].Value != line.Value {
|
||||
t.Errorf("\nexpected\t%+v %+v\nreceived\t%+v %+v\n", mt.metricPoints[i].Metric, mt.metricPoints[i].Value, line.Metric, line.Value)
|
||||
@@ -104,7 +106,7 @@ func TestBuildMetricsStrict(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, mt := range metricTests {
|
||||
ml := buildMetrics(mt.metric, w)
|
||||
ml := w.buildMetrics(mt.metric)
|
||||
for i, line := range ml {
|
||||
if mt.metricPoints[i].Metric != line.Metric || mt.metricPoints[i].Value != line.Value {
|
||||
t.Errorf("\nexpected\t%+v %+v\nreceived\t%+v %+v\n", mt.metricPoints[i].Metric, mt.metricPoints[i].Value, line.Metric, line.Value)
|
||||
@@ -143,7 +145,7 @@ func TestBuildMetricsWithSimpleFields(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, mt := range metricTests {
|
||||
ml := buildMetrics(mt.metric, w)
|
||||
ml := w.buildMetrics(mt.metric)
|
||||
for i, line := range ml {
|
||||
if mt.metricLines[i].Metric != line.Metric || mt.metricLines[i].Value != line.Value {
|
||||
t.Errorf("\nexpected\t%+v %+v\nreceived\t%+v %+v\n", mt.metricLines[i].Metric, mt.metricLines[i].Value, line.Metric, line.Value)
|
||||
@@ -195,7 +197,7 @@ func TestBuildTags(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tagtests {
|
||||
source, tags := buildTags(tt.ptIn, w)
|
||||
source, tags := w.buildTags(tt.ptIn)
|
||||
if source != tt.outSource {
|
||||
t.Errorf("\nexpected\t%+v\nreceived\t%+v\n", tt.outSource, source)
|
||||
}
|
||||
@@ -247,7 +249,7 @@ func TestBuildTagsWithSource(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tagtests {
|
||||
source, tags := buildTags(tt.ptIn, w)
|
||||
source, tags := w.buildTags(tt.ptIn)
|
||||
if source != tt.outSource {
|
||||
t.Errorf("\nexpected\t%+v\nreceived\t%+v\n", tt.outSource, source)
|
||||
}
|
||||
@@ -316,6 +318,42 @@ func TestBuildValueString(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestTagLimits(t *testing.T) {
|
||||
w := defaultWavefront()
|
||||
w.TruncateTags = true
|
||||
|
||||
// Should fail (all tags skipped)
|
||||
template := make(map[string]string)
|
||||
template[strings.Repeat("x", 255)] = "whatever"
|
||||
_, tags := w.buildTags(template)
|
||||
require.Empty(t, tags, "All tags should have been skipped")
|
||||
|
||||
// Should truncate value
|
||||
template = make(map[string]string)
|
||||
longKey := strings.Repeat("x", 253)
|
||||
template[longKey] = "whatever"
|
||||
_, tags = w.buildTags(template)
|
||||
require.Contains(t, tags, longKey, "Should contain truncated long key")
|
||||
require.Equal(t, "w", tags[longKey])
|
||||
|
||||
// Should not truncate
|
||||
template = make(map[string]string)
|
||||
longKey = strings.Repeat("x", 251)
|
||||
template[longKey] = "Hi!"
|
||||
_, tags = w.buildTags(template)
|
||||
require.Contains(t, tags, longKey, "Should contain non truncated long key")
|
||||
require.Equal(t, "Hi!", tags[longKey])
|
||||
|
||||
// Turn off truncating and make sure it leaves the tags intact
|
||||
w.TruncateTags = false
|
||||
template = make(map[string]string)
|
||||
longKey = strings.Repeat("x", 255)
|
||||
template[longKey] = longKey
|
||||
_, tags = w.buildTags(template)
|
||||
require.Contains(t, tags, longKey, "Should contain non truncated long key")
|
||||
require.Equal(t, longKey, tags[longKey])
|
||||
}
|
||||
|
||||
// Benchmarks to test performance of string replacement via Regex and Replacer
|
||||
var testString = "this_is*my!test/string\\for=replacement"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user