@@ -6,6 +6,7 @@ package syslogexporter
6
6
import (
7
7
"fmt"
8
8
"regexp"
9
+ "strings"
9
10
"testing"
10
11
"time"
11
12
@@ -85,6 +86,47 @@ func TestRFC5424Formatter(t *testing.T) {
85
86
assert .Contains (t , actual , "UserID=\" Tester2\" " )
86
87
assert .Contains (t , actual , "PEN=\" 27389\" " )
87
88
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
+
88
130
// Test defaults
89
131
expected = "<165>1 2003-08-24T12:14:15.000003Z - - - - -\n "
90
132
logRecord = plog .NewLogRecord ()
0 commit comments