Skip to content

Commit

Permalink
[exporter/loki] fix issue with log message quotation (#12116)
Browse files Browse the repository at this point in the history
* wrap double quotes to log entry line

* use strconv.Quote for wrapping quotation marks

* Add test case for quotation marks in loki exporter

* add changelog

* fix additional double quote in the beginning of the attribute
  • Loading branch information
harpratap authored Jul 7, 2022
1 parent 2f4d18e commit b1cd915
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion exporter/lokiexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ func (l *lokiExporter) convertLogBodyToEntry(lr plog.LogRecord, res pcommon.Reso
if _, found := l.config.Labels.Attributes[k]; !found {
b.WriteString(k)
b.WriteString("=")
b.WriteString(v.AsString())
// encapsulate with double quotes. See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/11827
b.WriteString(strconv.Quote(v.AsString()))
b.WriteRune(' ')
}
return true
Expand Down
3 changes: 2 additions & 1 deletion exporter/lokiexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,14 @@ func TestExporter_logDataToLoki(t *testing.T) {
lr.Body().SetStringVal("log message")
lr.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer")
lr.Attributes().InsertString("severity", "info")
lr.Attributes().InsertString("random.attribute", "random")
lr.Attributes().InsertString("random.attribute", "random attribute")
lr.SetTimestamp(ts)

pr, numDroppedLogs := exp.logDataToLoki(logs)
require.Equal(t, 0, numDroppedLogs)
require.NotNil(t, pr)
require.Len(t, pr.Streams, 1)
require.Contains(t, pr.Streams[0].Entries[0].Line, fmt.Sprintf("%q", "random attribute"))
})

t.Run("with multiple logs and same attributes", func(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions unreleased/fix-loki-quote-issue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: lokiexporter

# A brief description of the change
note: Wrap quotes around log message

# One or more tracking issues related to the change
issues: [11827]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# subtext:

0 comments on commit b1cd915

Please sign in to comment.