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 duplicate tags in TCP/UDP logs #29780

Merged
merged 11 commits into from
Oct 4, 2024
7 changes: 2 additions & 5 deletions pkg/logs/tailers/socket/tailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ func (t *Tailer) readForever() {
log.Warnf("Couldn't read message from connection: %v", err)
return
}
copiedTags := make([]string, len(t.source.Config.Tags))
copy(copiedTags, t.source.Config.Tags)
msg := decoder.NewInput(data)
if ipAddress != "" && pkgconfigsetup.Datadog().GetBool("logs_config.use_sourcehost_tag") {
lastColonIndex := strings.LastIndex(ipAddress, ":")
var ipAddressWithoutPort string
Expand All @@ -109,10 +108,8 @@ func (t *Tailer) readForever() {
ipAddressWithoutPort = ipAddress
}
sourceHostTag := fmt.Sprintf("source_host:%s", ipAddressWithoutPort)
copiedTags = append(copiedTags, sourceHostTag)
msg.ParsingExtra.Tags = append(msg.ParsingExtra.Tags, sourceHostTag)
}
msg := decoder.NewInput(data)
msg.ParsingExtra.Tags = append(msg.ParsingExtra.Tags, copiedTags...)
t.decoder.InputChan <- msg
}
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/logs/tailers/socket/tailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ func TestReadShouldFailWithError(t *testing.T) {
tailer.Stop()
}

func TestDuplicateTags(t *testing.T) {
msgChan := make(chan *message.Message)
r, w := net.Pipe()
logsConfig := &config.LogsConfig{
Tags: []string{"test:tag"},
}
logSource := sources.NewLogSource("test-source", logsConfig)

tailer := NewTailer(logSource, r, msgChan, read)
tailer.Start()

var msg *message.Message

w.Write([]byte("foo\n"))
msg = <-msgChan
assert.Equal(t, []string{"test:tag"}, msg.Tags())

tailer.Stop()
}

func read(tailer *Tailer) ([]byte, string, error) {
inBuf := make([]byte, 4096)
n, err := tailer.Conn.Read(inBuf)
Expand Down
12 changes: 12 additions & 0 deletions releasenotes/notes/fix-duplicate-tags-e97e8eeb6492235f.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Each section from every release note are combined when the
# CHANGELOG.rst is rendered. So the text needs to be worded so that
# it does not depend on any information only available in another
# section. This may mean repeating some details, but each section
# must be readable independently of the other.
#
# Each section note must be formatted as reStructuredText.
---
fixes:
- |
Fix duplicate tags in UDP/TCP logs.

Loading