From c179763e6894e367be612ad074e0abd140364ffe Mon Sep 17 00:00:00 2001 From: Alexandre Jacquemot Date: Thu, 8 Mar 2018 15:13:53 +0100 Subject: [PATCH] [Logs]: Fixed issue with missing tags when collecting logs from docker container (#1420) --- pkg/logs/message/origin.go | 12 ++++++-- pkg/logs/message/origin_test.go | 28 ++++++++++++++++++- ...fix-msg-tags-missing-b4215986dbc67aab.yaml | 4 +++ 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/logs-fix-msg-tags-missing-b4215986dbc67aab.yaml diff --git a/pkg/logs/message/origin.go b/pkg/logs/message/origin.go index ad96921e29a52..7461fc22ab215 100644 --- a/pkg/logs/message/origin.go +++ b/pkg/logs/message/origin.go @@ -49,11 +49,17 @@ func (o *Origin) TagsPayload() []byte { if o.LogSource.Config.SourceCategory != "" { tagsPayload = append(tagsPayload, []byte("[dd ddsourcecategory=\""+o.LogSource.Config.SourceCategory+"\"]")...) } + var tags string if o.LogSource.Config.Tags != "" { - tags := o.LogSource.Config.Tags - if len(o.tags) > 0 { - tags = tags + "," + strings.Join(o.tags, ",") + tags = o.LogSource.Config.Tags + } + if len(o.tags) > 0 { + if tags != "" { + tags = tags + "," } + tags = tags + strings.Join(o.tags, ",") + } + if tags != "" { tagsPayload = append(tagsPayload, []byte("[dd ddtags=\""+tags+"\"]")...) } if len(tagsPayload) == 0 { diff --git a/pkg/logs/message/origin_test.go b/pkg/logs/message/origin_test.go index b4b34aa57ce7c..04fd7e365a9f1 100644 --- a/pkg/logs/message/origin_test.go +++ b/pkg/logs/message/origin_test.go @@ -22,7 +22,33 @@ func TestSetTagsEmpty(t *testing.T) { assert.Equal(t, []byte{}, origin.TagsPayload()) } -func TestSetTags(t *testing.T) { +func TestTagsWithConfigTagsOnly(t *testing.T) { + cfg := &config.LogsConfig{ + Source: "a", + SourceCategory: "b", + Tags: "c:d,e", + } + source := config.NewLogSource("", cfg) + origin := NewOrigin() + origin.LogSource = source + assert.Equal(t, []string{"source:a", "sourcecategory:b", "c:d", "e"}, origin.Tags()) + assert.Equal(t, "[dd ddsource=\"a\"][dd ddsourcecategory=\"b\"][dd ddtags=\"c:d,e\"]", string(origin.TagsPayload())) +} + +func TestSetTagsWithNoConfigTags(t *testing.T) { + cfg := &config.LogsConfig{ + Source: "a", + SourceCategory: "b", + } + source := config.NewLogSource("", cfg) + origin := NewOrigin() + origin.LogSource = source + origin.SetTags([]string{"foo:bar", "baz"}) + assert.Equal(t, []string{"foo:bar", "baz", "source:a", "sourcecategory:b"}, origin.Tags()) + assert.Equal(t, "[dd ddsource=\"a\"][dd ddsourcecategory=\"b\"][dd ddtags=\"foo:bar,baz\"]", string(origin.TagsPayload())) +} + +func TestSetTagsWithConfigTags(t *testing.T) { cfg := &config.LogsConfig{ Source: "a", SourceCategory: "b", diff --git a/releasenotes/notes/logs-fix-msg-tags-missing-b4215986dbc67aab.yaml b/releasenotes/notes/logs-fix-msg-tags-missing-b4215986dbc67aab.yaml new file mode 100644 index 0000000000000..b3260ee2f3021 --- /dev/null +++ b/releasenotes/notes/logs-fix-msg-tags-missing-b4215986dbc67aab.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fixed the issue preventing from having docker tags when collecting logs from containers.