Skip to content

Commit c3b0227

Browse files
committed
Test for issue open-telemetry#33300
The current syslog exporter does not handle structured data well. In particular, when there is more than one structured data field, it will put them all in one structured data block rather than in one block per field as the RFC5424 suggests. This test is a test for this error.
1 parent 154d313 commit c3b0227

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

exporter/syslogexporter/rfc5424_formatter_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package syslogexporter
66
import (
77
"fmt"
88
"regexp"
9+
"strings"
910
"testing"
1011
"time"
1112

@@ -85,6 +86,47 @@ func TestRFC5424Formatter(t *testing.T) {
8586
assert.Contains(t, actual, "UserID=\"Tester2\"")
8687
assert.Contains(t, actual, "PEN=\"27389\"")
8788

89+
// Test structured data (more than one field)
90+
expectedRegex = "\\<165\\>1 2003-08-24T12:14:15.000003Z 192\\.0\\.2\\.1 myproc 8710 - " +
91+
"\\[\\S+ \\S+ \\S+\\]" +
92+
"\\[\\S+ \\S+ \\S+\\]" +
93+
" It's time to make the do-nuts\\.\n"
94+
logRecord = plog.NewLogRecord()
95+
logRecord.Attributes().PutStr("appname", "myproc")
96+
logRecord.Attributes().PutStr("hostname", "192.0.2.1")
97+
logRecord.Attributes().PutStr("message", "It's time to make the do-nuts.")
98+
logRecord.Attributes().PutInt("priority", 165)
99+
logRecord.Attributes().PutStr("proc_id", "8710")
100+
logRecord.Attributes().PutEmptyMap("structured_data")
101+
structuredData, found = logRecord.Attributes().Get("structured_data")
102+
require.True(t, found)
103+
structuredData.Map().PutEmptyMap("A@123")
104+
structuredDataSubmap, found = structuredData.Map().Get("A@123")
105+
require.True(t, found)
106+
structuredDataSubmap.Map().PutStr("A", "123")
107+
structuredDataSubmap.Map().PutStr("UserHostAddress", "192.168.2.132")
108+
structuredData.Map().PutEmptyMap("B@321")
109+
structuredDataSubmap, found = structuredData.Map().Get("B@321")
110+
require.True(t, found)
111+
structuredDataSubmap.Map().PutStr("B", "321")
112+
structuredDataSubmap.Map().PutStr("UserHostAddress", "192.168.2.132")
113+
logRecord.Attributes().PutInt("version", 1)
114+
115+
timestamp, err = time.Parse(time.RFC3339Nano, "2003-08-24T05:14:15.000003-07:00")
116+
require.NoError(t, err)
117+
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
118+
119+
actual = newRFC5424Formatter(false).format(logRecord)
120+
assert.NoError(t, err)
121+
matched, err = regexp.MatchString(expectedRegex, actual)
122+
assert.NoError(t, err)
123+
assert.True(t, matched, fmt.Sprintf("unexpected form of formatted message, formatted message: %s, regexp: %s", actual, expectedRegex))
124+
assert.True(t, strings.Contains(actual, "[A@123"))
125+
assert.True(t, strings.Contains(actual, "A=\"123\""))
126+
assert.True(t, strings.Contains(actual, "UserHostAddress=\"192.168.2.132\""))
127+
assert.True(t, strings.Contains(actual, "[B@321"))
128+
assert.True(t, strings.Contains(actual, "B=\"321\""))
129+
88130
// Test defaults
89131
expected = "<165>1 2003-08-24T12:14:15.000003Z - - - - -\n"
90132
logRecord = plog.NewLogRecord()

0 commit comments

Comments
 (0)