Skip to content

Commit

Permalink
fix(outputs.graphite): Add custom regex to outputs (influxdata#12908)
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj authored Mar 22, 2023
1 parent 02eea01 commit 3c8277d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
11 changes: 10 additions & 1 deletion plugins/outputs/graphite/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}
Expand Down
39 changes: 39 additions & 0 deletions plugins/outputs/graphite/graphite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3c8277d

Please sign in to comment.