diff --git a/plugins/outputs/graphite/graphite.go b/plugins/outputs/graphite/graphite.go index db61240d2f92b..ded4dc58aa6b8 100644 --- a/plugins/outputs/graphite/graphite.go +++ b/plugins/outputs/graphite/graphite.go @@ -25,6 +25,7 @@ type Graphite struct { GraphiteTagSupport bool `toml:"graphite_tag_support"` GraphiteTagSanitizeMode string `toml:"graphite_tag_sanitize_mode"` GraphiteSeparator string `toml:"graphite_separator"` + GraphiteStrictRegex string `toml:"graphite_strict_sanitize_regex"` // URL is only for backwards compatibility Servers []string `toml:"servers"` Prefix string `toml:"prefix"` @@ -165,7 +166,15 @@ func (g *Graphite) checkEOF(conn net.Conn) error { func (g *Graphite) Write(metrics []telegraf.Metric) error { // Prepare data var batch []byte - s, err := serializers.NewGraphiteSerializer(g.Prefix, g.Template, "", g.GraphiteTagSupport, g.GraphiteTagSanitizeMode, g.GraphiteSeparator, g.Templates) + s, err := serializers.NewGraphiteSerializer( + g.Prefix, + g.Template, + g.GraphiteStrictRegex, + g.GraphiteTagSupport, + g.GraphiteTagSanitizeMode, + g.GraphiteSeparator, + g.Templates, + ) if err != nil { return err } diff --git a/plugins/outputs/graphite/graphite_test.go b/plugins/outputs/graphite/graphite_test.go index d686b4d52dfe0..22c6148468efa 100644 --- a/plugins/outputs/graphite/graphite_test.go +++ b/plugins/outputs/graphite/graphite_test.go @@ -102,6 +102,45 @@ func TestGraphiteOK(t *testing.T) { require.NoError(t, err) } +func TestGraphiteStrictRegex(t *testing.T) { + var wg sync.WaitGroup + wg.Add(1) + t.Log("Starting server") + tcpServer, err := net.Listen("tcp", "127.0.0.1:12042") + require.NoError(t, err) + go func() { + defer wg.Done() + conn, _ := (tcpServer).Accept() + reader := bufio.NewReader(conn) + tp := textproto.NewReader(reader) + data1, _ := tp.ReadLine() + require.Equal(t, "192_168_0_1.|us-west-2|.mymeasurement.myfield 0.123 1289430000", data1) + require.NoError(t, conn.Close()) + require.NoError(t, tcpServer.Close()) + }() + + m := metric.New( + "mymeasurement", + map[string]string{ + "host": "192.168.0.1", + "datacenter": "|us-west-2|", + }, + map[string]interface{}{"myfield": float64(0.123)}, + time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC), + ) + + g := Graphite{ + Servers: []string{"localhost:12042"}, + Log: testutil.Logger{}, + GraphiteStrictRegex: `[^a-zA-Z0-9-:._=|\p{L}]`, + } + require.NoError(t, g.Connect()) + require.NoError(t, g.Write([]telegraf.Metric{m})) + + wg.Wait() + require.NoError(t, g.Close()) +} + func TestGraphiteOkWithSeparatorDot(t *testing.T) { var wg sync.WaitGroup // Start TCP server