Skip to content

Commit

Permalink
fix: ensure graylog spec fields not prefixed with '_' (#10209)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7049967)
  • Loading branch information
powersj authored and MyaLongmire committed Dec 8, 2021
1 parent 8a8a838 commit 108bc47
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
22 changes: 21 additions & 1 deletion plugins/outputs/graylog/graylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (
defaultTimeout = 5 * time.Second
)

var defaultSpecFields = []string{"version", "host", "short_message", "full_message", "timestamp", "level", "facility", "line", "file"}

type gelfConfig struct {
Endpoint string
Connection string
Expand Down Expand Up @@ -423,14 +425,22 @@ func (g *Graylog) serialize(metric telegraf.Metric) ([]string, error) {
}

for _, tag := range metric.TagList() {
if tag.Key != "host" {
if tag.Key == "host" {
continue
}

if fieldInSpec(tag.Key) {
m[tag.Key] = tag.Value
} else {
m["_"+tag.Key] = tag.Value
}
}

for _, field := range metric.FieldList() {
if field.Key == g.ShortMessageField {
m["short_message"] = field.Value
} else if fieldInSpec(field.Key) {
m[field.Key] = field.Value
} else {
m["_"+field.Key] = field.Value
}
Expand All @@ -445,6 +455,16 @@ func (g *Graylog) serialize(metric telegraf.Metric) ([]string, error) {
return out, nil
}

func fieldInSpec(field string) bool {
for _, specField := range defaultSpecFields {
if specField == field {
return true
}
}

return false
}

func init() {
outputs.Add("graylog", func() telegraf.Output {
return &Graylog{
Expand Down
41 changes: 41 additions & 0 deletions plugins/outputs/graylog/graylog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,51 @@ import (
reuse "github.com/libp2p/go-reuseport"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/metric"
tlsint "github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/testutil"
)

func TestSerializer(t *testing.T) {
m1 := metric.New("testing",
map[string]string{
"verb": "GET",
"host": "hostname",
},
map[string]interface{}{
"full_message": "full",
"short_message": "short",
"level": "1",
"facility": "demo",
"line": "42",
"file": "graylog.go",
},
time.Now(),
)

graylog := Graylog{}
result, err := graylog.serialize(m1)

require.NoError(t, err)

for _, r := range result {
var obj GelfObject
err = json.Unmarshal([]byte(r), &obj)
require.NoError(t, err)

require.Equal(t, obj["version"], "1.1")
require.Equal(t, obj["_name"], "testing")
require.Equal(t, obj["_verb"], "GET")
require.Equal(t, obj["host"], "hostname")
require.Equal(t, obj["full_message"], "full")
require.Equal(t, obj["short_message"], "short")
require.Equal(t, obj["level"], "1")
require.Equal(t, obj["facility"], "demo")
require.Equal(t, obj["line"], "42")
require.Equal(t, obj["file"], "graylog.go")
}
}

func TestWriteUDP(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 108bc47

Please sign in to comment.