Skip to content

Commit 5ca4f6d

Browse files
committed
fix(daemon): Properly escape UTF-8 in log labels
The previous code did not account for that labels can contain UTF-8 characters which must be escaped. This commit changes the code to use the Go marshalling for json which will properly handle these characters. A guard was added to make sure invalid log labels are not converted to JSON (with an empty key or value). The ingest code should prevent this from happening but a secondary check here on JSON generation is a reasonable safeguard.
1 parent 0e666b9 commit 5ca4f6d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

daemon/internal/newrelic/log_events.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,25 @@ func (events *LogEvents) CollectorJSON(id AgentRunID) ([]byte, error) {
7676
estimate := len(es) * 128
7777
buf.Grow(estimate)
7878
buf.WriteString(`[{` +
79-
`"common": {"attributes": {`)
79+
`"common": {"attributes": `)
8080
nwrit := 0
81-
for _, value := range events.LogForwardingLabels {
82-
if nwrit > 0 {
83-
buf.WriteByte(',')
81+
labelMap := make(map[string]string)
82+
for _, label := range events.LogForwardingLabels {
83+
// safegaurd against invalid labels
84+
if len(label.LabelType) != 0 && len(label.LabelValue) != 0 {
85+
labelMap["tags."+label.LabelType] = label.LabelValue
8486
}
85-
nwrit++
86-
buf.WriteString(`"tags.`)
87-
buf.WriteString(value.LabelType)
88-
buf.WriteString(`":"`)
89-
buf.WriteString(value.LabelValue)
90-
buf.WriteString(`"`)
9187
}
9288

93-
buf.WriteString(`}},` +
89+
j, e := json.Marshal(labelMap)
90+
if e != nil {
91+
log.Errorf("failed to marshal log label: %s", e)
92+
buf.WriteString("{}")
93+
} else {
94+
buf.Write(j)
95+
}
96+
97+
buf.WriteString(`},` +
9498
`"logs": [`)
9599

96100
nwrit = 0

0 commit comments

Comments
 (0)