-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[exporter/debug] format log records as one-liners in
normal
verbosi…
…ty (#10225) <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Changes the behavior of `normal` verbosity of the Debug exporter for logs to display each log record in one line of text. Note that if the body of the log record contains newlines, the output will be displayed in multiple lines. This pull request is part of #7806; it implements the change for logs. The changes for metrics and [traces](#10280) will be proposed in separate pull requests. The implementation in this pull request does not display any details on the resource or the scope of the logs. I would like to propose displaying the resource and the scope as separate lines in a separate pull request. This change applies to the Debug exporter only. The behavior of the Logging exporter remains unchanged. To use this behavior, switch from the deprecated Logging exporter to Debug exporter. #### Link to tracking issue - #7806 #### Testing Added unit tests for the formatter. #### Documentation Described the formatting in the Debug exporter's README. --------- Co-authored-by: Roger Coll <roger.coll@elastic.co> Co-authored-by: Pablo Baeyens <pbaeyens31+github@gmail.com>
- Loading branch information
1 parent
ed09aa1
commit 426e660
Showing
5 changed files
with
221 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
component: exporter/debug | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: In `normal` verbosity, display one line of text for each log record | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [7806] | ||
|
||
# (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. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package normal // import "go.opentelemetry.io/collector/exporter/debugexporter/internal/normal" | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
type normalLogsMarshaler struct{} | ||
|
||
// Ensure normalLogsMarshaller implements interface plog.Marshaler | ||
var _ plog.Marshaler = normalLogsMarshaler{} | ||
|
||
// NewNormalLogsMarshaler returns a plog.Marshaler for normal verbosity. It writes one line of text per log record | ||
func NewNormalLogsMarshaler() plog.Marshaler { | ||
return normalLogsMarshaler{} | ||
} | ||
|
||
func (normalLogsMarshaler) MarshalLogs(ld plog.Logs) ([]byte, error) { | ||
var buffer bytes.Buffer | ||
for i := 0; i < ld.ResourceLogs().Len(); i++ { | ||
resourceLog := ld.ResourceLogs().At(i) | ||
for j := 0; j < resourceLog.ScopeLogs().Len(); j++ { | ||
scopeLog := resourceLog.ScopeLogs().At(j) | ||
for k := 0; k < scopeLog.LogRecords().Len(); k++ { | ||
logRecord := scopeLog.LogRecords().At(k) | ||
logAttributes := writeAttributes(logRecord.Attributes()) | ||
|
||
logString := fmt.Sprintf("%s %s", logRecord.Body().AsString(), strings.Join(logAttributes, " ")) | ||
buffer.WriteString(logString) | ||
buffer.WriteString("\n") | ||
} | ||
} | ||
} | ||
return buffer.Bytes(), nil | ||
} | ||
|
||
// writeAttributes returns a slice of strings in the form "attrKey=attrValue" | ||
func writeAttributes(attributes pcommon.Map) (attributeStrings []string) { | ||
attributes.Range(func(k string, v pcommon.Value) bool { | ||
attribute := fmt.Sprintf("%s=%s", k, v.AsString()) | ||
attributeStrings = append(attributeStrings, attribute) | ||
return true | ||
}) | ||
return attributeStrings | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package normal | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
func TestMarshalLogs(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input plog.Logs | ||
expected string | ||
}{ | ||
{ | ||
name: "empty logs", | ||
input: plog.NewLogs(), | ||
expected: "", | ||
}, | ||
{ | ||
name: "one log record", | ||
input: func() plog.Logs { | ||
logs := plog.NewLogs() | ||
logRecord := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty() | ||
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(time.Date(2024, 1, 23, 17, 54, 41, 153, time.UTC))) | ||
logRecord.SetSeverityNumber(plog.SeverityNumberInfo) | ||
logRecord.SetSeverityText("INFO") | ||
logRecord.Body().SetStr("Single line log message") | ||
logRecord.Attributes().PutStr("key1", "value1") | ||
logRecord.Attributes().PutStr("key2", "value2") | ||
return logs | ||
}(), | ||
expected: `Single line log message key1=value1 key2=value2 | ||
`, | ||
}, | ||
{ | ||
name: "multiline log", | ||
input: func() plog.Logs { | ||
logs := plog.NewLogs() | ||
logRecord := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty() | ||
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(time.Date(2024, 1, 23, 17, 54, 41, 153, time.UTC))) | ||
logRecord.SetSeverityNumber(plog.SeverityNumberInfo) | ||
logRecord.SetSeverityText("INFO") | ||
logRecord.Body().SetStr("First line of the log message\n second line of the log message") | ||
logRecord.Attributes().PutStr("key1", "value1") | ||
logRecord.Attributes().PutStr("key2", "value2") | ||
return logs | ||
}(), | ||
expected: `First line of the log message | ||
second line of the log message key1=value1 key2=value2 | ||
`, | ||
}, | ||
{ | ||
name: "two log records", | ||
input: func() plog.Logs { | ||
logs := plog.NewLogs() | ||
logRecords := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords() | ||
|
||
logRecord := logRecords.AppendEmpty() | ||
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(time.Date(2024, 1, 23, 17, 54, 41, 153, time.UTC))) | ||
logRecord.SetSeverityNumber(plog.SeverityNumberInfo) | ||
logRecord.SetSeverityText("INFO") | ||
logRecord.Body().SetStr("Single line log message") | ||
logRecord.Attributes().PutStr("key1", "value1") | ||
logRecord.Attributes().PutStr("key2", "value2") | ||
|
||
logRecord = logRecords.AppendEmpty() | ||
logRecord.Body().SetStr("Multi-line\nlog message") | ||
logRecord.Attributes().PutStr("mykey2", "myvalue2") | ||
logRecord.Attributes().PutStr("mykey1", "myvalue1") | ||
return logs | ||
}(), | ||
expected: `Single line log message key1=value1 key2=value2 | ||
Multi-line | ||
log message mykey2=myvalue2 mykey1=myvalue1 | ||
`, | ||
}, | ||
{ | ||
name: "log with maps in body and attributes", | ||
input: func() plog.Logs { | ||
logs := plog.NewLogs() | ||
logRecord := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty() | ||
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(time.Date(2020, 2, 11, 20, 26, 13, 789, time.UTC))) | ||
logRecord.SetSeverityNumber(plog.SeverityNumberInfo) | ||
logRecord.SetSeverityText("INFO") | ||
body := logRecord.Body().SetEmptyMap() | ||
body.PutStr("app", "CurrencyConverter") | ||
bodyEvent := body.PutEmptyMap("event") | ||
bodyEvent.PutStr("operation", "convert") | ||
bodyEvent.PutStr("result", "success") | ||
conversionAttr := logRecord.Attributes().PutEmptyMap("conversion") | ||
conversionSourceAttr := conversionAttr.PutEmptyMap("source") | ||
conversionSourceAttr.PutStr("currency", "USD") | ||
conversionSourceAttr.PutDouble("amount", 34.22) | ||
conversionDestinationAttr := conversionAttr.PutEmptyMap("destination") | ||
conversionDestinationAttr.PutStr("currency", "EUR") | ||
logRecord.Attributes().PutStr("service", "payments") | ||
return logs | ||
}(), | ||
expected: `{"app":"CurrencyConverter","event":{"operation":"convert","result":"success"}} conversion={"destination":{"currency":"EUR"},"source":{"amount":34.22,"currency":"USD"}} service=payments | ||
`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
output, err := NewNormalLogsMarshaler().MarshalLogs(tt.input) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expected, string(output)) | ||
}) | ||
} | ||
} |