Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure graylog spec fields not prefixed with '_' #10209

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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