@@ -10,76 +10,38 @@ import (
10
10
"github.com/influxdata/telegraf/config"
11
11
)
12
12
13
+ const (
14
+ // previous => Append current line to previous line
15
+ previous multilineMatchWhichLine = iota
16
+ // next => next line will be appended to current line
17
+ next
18
+ )
19
+
13
20
// Indicates relation to the multiline event: previous or next
14
- type MultilineMatchWhichLine int
21
+ type multilineMatchWhichLine int
15
22
16
- type Multiline struct {
17
- config * MultilineConfig
23
+ type multiline struct {
24
+ config * multilineConfig
18
25
enabled bool
19
26
patternRegexp * regexp.Regexp
20
27
quote byte
21
28
inQuote bool
22
29
}
23
30
24
- type MultilineConfig struct {
31
+ type multilineConfig struct {
25
32
Pattern string `toml:"pattern"`
26
- MatchWhichLine MultilineMatchWhichLine `toml:"match_which_line"`
33
+ MatchWhichLine multilineMatchWhichLine `toml:"match_which_line"`
27
34
InvertMatch bool `toml:"invert_match"`
28
35
PreserveNewline bool `toml:"preserve_newline"`
29
36
Quotation string `toml:"quotation"`
30
37
Timeout * config.Duration `toml:"timeout"`
31
38
}
32
39
33
- const (
34
- // Previous => Append current line to previous line
35
- Previous MultilineMatchWhichLine = iota
36
- // Next => Next line will be appended to current line
37
- Next
38
- )
39
-
40
- func (m * MultilineConfig ) NewMultiline () (* Multiline , error ) {
41
- var r * regexp.Regexp
42
-
43
- if m .Pattern != "" {
44
- var err error
45
- if r , err = regexp .Compile (m .Pattern ); err != nil {
46
- return nil , err
47
- }
48
- }
49
-
50
- var quote byte
51
- switch m .Quotation {
52
- case "" , "ignore" :
53
- m .Quotation = "ignore"
54
- case "single-quotes" :
55
- quote = '\''
56
- case "double-quotes" :
57
- quote = '"'
58
- case "backticks" :
59
- quote = '`'
60
- default :
61
- return nil , errors .New ("invalid 'quotation' setting" )
62
- }
63
-
64
- enabled := m .Pattern != "" || quote != 0
65
- if m .Timeout == nil || time .Duration (* m .Timeout ).Nanoseconds () == int64 (0 ) {
66
- d := config .Duration (5 * time .Second )
67
- m .Timeout = & d
68
- }
69
-
70
- return & Multiline {
71
- config : m ,
72
- enabled : enabled ,
73
- patternRegexp : r ,
74
- quote : quote ,
75
- }, nil
76
- }
77
-
78
- func (m * Multiline ) IsEnabled () bool {
40
+ func (m * multiline ) isEnabled () bool {
79
41
return m .enabled
80
42
}
81
43
82
- func (m * Multiline ) ProcessLine (text string , buffer * bytes.Buffer ) string {
44
+ func (m * multiline ) processLine (text string , buffer * bytes.Buffer ) string {
83
45
if m .matchQuotation (text ) || m .matchString (text ) {
84
46
// Restore the newline removed by tail's scanner
85
47
if buffer .Len () > 0 && m .config .PreserveNewline {
@@ -89,13 +51,13 @@ func (m *Multiline) ProcessLine(text string, buffer *bytes.Buffer) string {
89
51
return ""
90
52
}
91
53
92
- if m .config .MatchWhichLine == Previous {
54
+ if m .config .MatchWhichLine == previous {
93
55
previousText := buffer .String ()
94
56
buffer .Reset ()
95
57
buffer .WriteString (text )
96
58
text = previousText
97
59
} else {
98
- // Next
60
+ // next
99
61
if buffer .Len () > 0 {
100
62
if m .config .PreserveNewline {
101
63
buffer .WriteString ("\n " )
@@ -109,16 +71,7 @@ func (m *Multiline) ProcessLine(text string, buffer *bytes.Buffer) string {
109
71
return text
110
72
}
111
73
112
- func Flush (buffer * bytes.Buffer ) string {
113
- if buffer .Len () == 0 {
114
- return ""
115
- }
116
- text := buffer .String ()
117
- buffer .Reset ()
118
- return text
119
- }
120
-
121
- func (m * Multiline ) matchQuotation (text string ) bool {
74
+ func (m * multiline ) matchQuotation (text string ) bool {
122
75
if m .config .Quotation == "ignore" {
123
76
return false
124
77
}
@@ -146,46 +99,93 @@ func (m *Multiline) matchQuotation(text string) bool {
146
99
return m .inQuote
147
100
}
148
101
149
- func (m * Multiline ) matchString (text string ) bool {
102
+ func (m * multiline ) matchString (text string ) bool {
150
103
if m .patternRegexp != nil {
151
104
return m .patternRegexp .MatchString (text ) != m .config .InvertMatch
152
105
}
153
106
return false
154
107
}
155
108
156
- func (w MultilineMatchWhichLine ) String () string {
109
+ func (m * multilineConfig ) newMultiline () (* multiline , error ) {
110
+ var r * regexp.Regexp
111
+
112
+ if m .Pattern != "" {
113
+ var err error
114
+ if r , err = regexp .Compile (m .Pattern ); err != nil {
115
+ return nil , err
116
+ }
117
+ }
118
+
119
+ var quote byte
120
+ switch m .Quotation {
121
+ case "" , "ignore" :
122
+ m .Quotation = "ignore"
123
+ case "single-quotes" :
124
+ quote = '\''
125
+ case "double-quotes" :
126
+ quote = '"'
127
+ case "backticks" :
128
+ quote = '`'
129
+ default :
130
+ return nil , errors .New ("invalid 'quotation' setting" )
131
+ }
132
+
133
+ enabled := m .Pattern != "" || quote != 0
134
+ if m .Timeout == nil || time .Duration (* m .Timeout ).Nanoseconds () == int64 (0 ) {
135
+ d := config .Duration (5 * time .Second )
136
+ m .Timeout = & d
137
+ }
138
+
139
+ return & multiline {
140
+ config : m ,
141
+ enabled : enabled ,
142
+ patternRegexp : r ,
143
+ quote : quote ,
144
+ }, nil
145
+ }
146
+
147
+ func flush (buffer * bytes.Buffer ) string {
148
+ if buffer .Len () == 0 {
149
+ return ""
150
+ }
151
+ text := buffer .String ()
152
+ buffer .Reset ()
153
+ return text
154
+ }
155
+
156
+ func (w multilineMatchWhichLine ) String () string {
157
157
switch w {
158
- case Previous :
158
+ case previous :
159
159
return "previous"
160
- case Next :
160
+ case next :
161
161
return "next"
162
162
}
163
163
return ""
164
164
}
165
165
166
- // UnmarshalTOML implements ability to unmarshal MultilineMatchWhichLine from TOML files.
167
- func (w * MultilineMatchWhichLine ) UnmarshalTOML (data []byte ) (err error ) {
166
+ // UnmarshalTOML implements ability to unmarshal multilineMatchWhichLine from TOML files.
167
+ func (w * multilineMatchWhichLine ) UnmarshalTOML (data []byte ) (err error ) {
168
168
return w .UnmarshalText (data )
169
169
}
170
170
171
171
// UnmarshalText implements encoding.TextUnmarshaler
172
- func (w * MultilineMatchWhichLine ) UnmarshalText (data []byte ) (err error ) {
172
+ func (w * multilineMatchWhichLine ) UnmarshalText (data []byte ) (err error ) {
173
173
s := string (data )
174
174
switch strings .ToUpper (s ) {
175
175
case `PREVIOUS` , `"PREVIOUS"` , `'PREVIOUS'` :
176
- * w = Previous
176
+ * w = previous
177
177
return nil
178
178
179
179
case `NEXT` , `"NEXT"` , `'NEXT'` :
180
- * w = Next
180
+ * w = next
181
181
return nil
182
182
}
183
183
* w = - 1
184
184
return errors .New ("unknown multiline MatchWhichLine" )
185
185
}
186
186
187
187
// MarshalText implements encoding.TextMarshaler
188
- func (w MultilineMatchWhichLine ) MarshalText () ([]byte , error ) {
188
+ func (w multilineMatchWhichLine ) MarshalText () ([]byte , error ) {
189
189
s := w .String ()
190
190
if s != "" {
191
191
return []byte (s ), nil
0 commit comments