Skip to content
Open
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
64 changes: 64 additions & 0 deletions pkg/logs/processor/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,70 @@ func TestJsonEncoder(t *testing.T) {
})
}

func TestJsonEncoderHostnameOmitted(t *testing.T) {
logsConfig := &config.LogsConfig{Source: "Source"}
source := sources.NewLogSource("", logsConfig)

t.Run("empty hostname is omitted from JSON", func(t *testing.T) {
msg := newMessage([]byte("log message"), source, message.StatusInfo)
msg.State = message.StateRendered

err := JSONEncoder.Encode(msg, "")
assert.Nil(t, err)

var parsed map[string]any
assert.Nil(t, json.Unmarshal(msg.GetContent(), &parsed))
_, hasHostname := parsed["hostname"]
assert.False(t, hasHostname, "hostname key should be absent when hostname is empty")
})

t.Run("non-empty hostname is present in JSON", func(t *testing.T) {
msg := newMessage([]byte("log message"), source, message.StatusInfo)
msg.State = message.StateRendered

err := JSONEncoder.Encode(msg, "my-host")
assert.Nil(t, err)

var parsed map[string]any
assert.Nil(t, json.Unmarshal(msg.GetContent(), &parsed))
hostname, hasHostname := parsed["hostname"]
assert.True(t, hasHostname, "hostname key should be present when hostname is non-empty")
assert.Equal(t, "my-host", hostname)
})
}

func TestJsonServerlessInitEncoderHostnameOmitted(t *testing.T) {
logsConfig := &config.LogsConfig{Source: "Source"}
source := sources.NewLogSource("", logsConfig)

t.Run("empty hostname is omitted from JSON", func(t *testing.T) {
msg := newMessage([]byte("log message"), source, message.StatusInfo)
msg.State = message.StateRendered

err := JSONServerlessInitEncoder.Encode(msg, "")
assert.Nil(t, err)

var parsed map[string]any
assert.Nil(t, json.Unmarshal(msg.GetContent(), &parsed))
_, hasHostname := parsed["hostname"]
assert.False(t, hasHostname, "hostname key should be absent when hostname is empty")
})

t.Run("non-empty hostname is present in JSON", func(t *testing.T) {
msg := newMessage([]byte("log message"), source, message.StatusInfo)
msg.State = message.StateRendered

err := JSONServerlessInitEncoder.Encode(msg, "my-host")
assert.Nil(t, err)

var parsed map[string]any
assert.Nil(t, json.Unmarshal(msg.GetContent(), &parsed))
hostname, hasHostname := parsed["hostname"]
assert.True(t, hasHostname, "hostname key should be present when hostname is non-empty")
assert.Equal(t, "my-host", hostname)
})
}

func TestEncoderToValidUTF8(t *testing.T) {
// valid utf-8
assert.Equal(t, "", toValidUtf8(nil))
Expand Down
2 changes: 1 addition & 1 deletion pkg/logs/processor/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type jsonPayload struct {
Message ValidUtf8Bytes `json:"message"`
Status string `json:"status"`
Timestamp int64 `json:"timestamp"`
Hostname string `json:"hostname"`
Hostname string `json:"hostname,omitempty"`
Service string `json:"service"`
Source string `json:"ddsource"`
Tags string `json:"ddtags"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/logs/processor/json_serverless_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type jsonServerlessInitPayload struct {
Message ValidUtf8Bytes `json:"message"`
Status string `json:"status"`
Timestamp int64 `json:"timestamp"`
Hostname string `json:"hostname"`
Hostname string `json:"hostname,omitempty"`
Service string `json:"service,omitempty"`
Source string `json:"ddsource"`
Tags string `json:"ddtags"`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
Fix OTel logs sent via the Datadog exporter on ECS Fargate showing an empty
string for the ``host`` reserved attribute instead of no host. The hostname
field is now omitted from the log payload when no hostname applies (e.g.
serverless/Fargate environments), so logs appear without a host rather than
with a blank one.
Loading