/
deliverer.go
357 lines (315 loc) · 9.16 KB
/
deliverer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package queue
import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"
"time"
"github.com/philips-software/logproxy/shared"
"github.com/influxdata/go-syslog/v2"
"github.com/influxdata/go-syslog/v2/rfc5424"
"github.com/m4rw3r/uuid"
"github.com/philips-software/go-hsdp-api/logging"
)
var (
batchSize = 25
rtrTimeFormat = "2006-01-02T15:04:05.000Z0700"
vcapPattern = regexp.MustCompile(`vcap_request_id:"(?P<requestID>[^"]+)"`)
rtrPattern = regexp.MustCompile(`\[RTR/(?P<index>\d+)]`)
rtrFormat = regexp.MustCompile(`(?P<hostname>[^?/\s]+) - \[(?P<time>[^/\s]+)]`)
errNoMessage = errors.New("no message in syslogMessage")
defaultInvalidCharacters = "$&+,:;=?@#|<>()[]"
applicationNameInvalidCharacters = "$&+,;=?@#|<>()[]"
applicationInstanceInvalidCharacters = "[&+;?@|<>()[]"
eventIDInvalidCharacters = "&+,:;=?@#|<>()[]"
otherNameInvalidCharacters = "&+,;=?@#|<>()[]"
originatingUsersInvalidCharacters = "$&+;=?@#|<>()[]"
versionInvalidCharacters = "&+;=?@|<>()[]"
parser = rfc5424.NewParser()
)
// DHPLogMessage describes a structured log message from applications
type DHPLogMessage struct {
Category string `json:"cat"`
EventID string `json:"evt"`
ApplicationVersion string `json:"ver"`
Component string `json:"cmp"`
ApplicationName string `json:"app"`
ApplicationInstance string `json:"inst"`
ServerName string `json:"srv"`
TransactionID string `json:"trns"`
ServiceName string `json:"service"`
LogTime string `json:"time"`
OriginatingUser string `json:"usr"`
Severity string `json:"sev"`
LogData DHPLogData `json:"val"`
Custom json.RawMessage `json:"custom,omitempty"`
}
// DHPLogData represents the payload of DHPLogMessage
type DHPLogData struct {
Message string `json:"message"`
}
// Logger implements logging interface
type Logger interface {
Debugf(format string, args ...interface{})
}
// Deliverer implements all processing logic for parsing and forwarding logs
type Deliverer struct {
Debug bool
storer logging.Storer
log Logger
buildVersion string
manager *shared.PluginManager
}
// NewDeliverer returns a new configured Deliverer instance
func NewDeliverer(storer logging.Storer, log Logger, manager *shared.PluginManager, buildVersion string) (*Deliverer, error) {
var logger Deliverer
logger.storer = storer
logger.log = log // Meta
logger.buildVersion = buildVersion
logger.manager = manager
return &logger, nil
}
func BodyToResource(body []byte) (*logging.Resource, error) {
syslogMessage, err := parser.Parse(body)
if err != nil {
return nil, err
}
if syslogMessage == nil || syslogMessage.Message() == nil {
return nil, errNoMessage
}
resource, err := ProcessMessage(syslogMessage)
if err != nil {
return nil, err
}
return resource, nil
}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func (pl *Deliverer) flushBatch(resources []logging.Resource, count int, queue Queue) (int, error) {
fmt.Printf("batch flushing %d messages\n", count)
maxLoop := count
l := 0
for {
l++
resp, err := pl.storer.StoreResources(resources, count)
if err == nil { // Happy flow
break
}
if resp == nil {
fmt.Printf("unexpected error for StoreResource(): %v\n", err)
continue
}
nrErrors := len(resp.Failed)
keys := make([]int, 0, nrErrors)
for k := range resp.Failed {
keys = append(keys, k)
}
// Remove offending messages and resend
pos := 0
for i := 0; i < count; i++ {
if contains(keys, i) {
_ = queue.DeadLetter(resources[i])
continue
}
resources[pos] = resources[i]
pos++
}
count = pos
fmt.Printf("Found %d errors. Resending %d\n", nrErrors, count)
if l > maxLoop || count <= 0 {
fmt.Printf("Maximum retries reached or nothingt to send. Bailing..\n")
break
}
}
return count, nil
}
// ResourceWorker implements the worker process for parsing the queues
func (pl *Deliverer) ResourceWorker(queue Queue, done <-chan bool) {
var count int
var totalStored int64
buf := make([]logging.Resource, batchSize)
resourceChannel := queue.Output()
fmt.Printf("Starting ResourceWorker...\n")
for {
select {
case resource := <-resourceChannel:
if resource.ApplicationVersion == "" {
resource.ApplicationVersion = pl.buildVersion
}
if drop := pl.processFilters(&resource); drop {
continue
}
buf[count] = resource
count++
if count == batchSize {
stored, _ := pl.flushBatch(buf, count, queue)
totalStored += int64(stored)
count = 0
}
case <-time.After(500 * time.Millisecond):
if count > 0 {
stored, _ := pl.flushBatch(buf, count, queue)
totalStored += int64(stored)
count = 0
}
case <-done:
if count > 0 {
stored, _ := pl.flushBatch(buf, count, queue)
totalStored += int64(stored)
}
fmt.Printf("Worker received done message...%d stored\n", totalStored)
return
}
}
}
func (pl *Deliverer) processFilters(resource *logging.Resource) bool {
if pl.manager == nil {
return false
}
for _, p := range pl.manager.Plugins() {
r, drop, modified, _ := p.App.Filter(*resource)
if drop {
return true
}
if modified {
*resource = r
}
}
return false
}
func ProcessMessage(rfcLogMessage syslog.Message) (*logging.Resource, error) {
var dhp DHPLogMessage
var msg logging.Resource
var logMessage *string
if rfcLogMessage == nil {
return nil, fmt.Errorf("no message found in syslog entry")
}
logMessage = rfcLogMessage.Message()
msg = wrapResource("logproxy-wrapped", rfcLogMessage)
err := json.Unmarshal([]byte(*logMessage), &dhp)
if err == nil {
if dhp.TransactionID != "" {
msg.TransactionID = dhp.TransactionID
}
if dhp.LogData.Message != "" {
msg.LogData.Message = dhp.LogData.Message
}
if dhp.ApplicationInstance != "" {
msg.ApplicationInstance = dhp.ApplicationInstance
}
var encoderTasks = []struct {
src string
dest *string
invalidChars string
}{
{dhp.OriginatingUser, &msg.OriginatingUser, originatingUsersInvalidCharacters},
{dhp.EventID, &msg.EventID, eventIDInvalidCharacters},
{dhp.ApplicationVersion, &msg.ApplicationVersion, versionInvalidCharacters},
{dhp.ApplicationName, &msg.ApplicationName, applicationNameInvalidCharacters},
{dhp.ServiceName, &msg.ServiceName, otherNameInvalidCharacters},
{dhp.ServerName, &msg.ServerName, otherNameInvalidCharacters},
{dhp.Category, &msg.Category, defaultInvalidCharacters},
{dhp.Component, &msg.Component, defaultInvalidCharacters},
{dhp.Severity, &msg.Severity, defaultInvalidCharacters},
}
for _, task := range encoderTasks {
if task.src != "" {
*task.dest = EncodeString(task.src, task.invalidChars)
}
}
msg.Custom = dhp.Custom
}
return &msg, nil
}
// EncodeString encodes all characters from the characterstoEncode set
func EncodeString(s string, charactersToEncode string) string {
var res = strings.Builder{}
for _, char := range s {
if strings.ContainsRune(charactersToEncode, char) {
encodedCharacter := fmt.Sprintf("%%%X", int(char))
res.WriteString(encodedCharacter)
} else {
res.WriteRune(char)
}
}
return res.String()
}
func wrapResource(originatingUser string, msg syslog.Message) logging.Resource {
var lm logging.Resource
// ID
id, _ := uuid.V4()
lm.ID = id.String()
// EventID
lm.EventID = "1"
// Category
lm.Category = "ApplicationLog"
// Component
lm.Component = "logproxy"
// TransactionID
if vcap := vcapPattern.FindStringSubmatch(lm.LogData.Message); len(vcap) > 0 {
lm.TransactionID = vcap[1]
} else {
uuidV4, _ := uuid.V4()
lm.TransactionID = uuidV4.String()
}
// ServiceName
lm.ServiceName = "logproxy"
if a := msg.Appname(); a != nil {
lm.ServiceName = *a
}
// ApplicationName
lm.ApplicationName = "logproxy"
if a := msg.Appname(); a != nil {
lm.ApplicationName = *a
}
// ApplicationInstance
lm.ApplicationInstance = "logproxy"
if h := msg.Hostname(); h != nil {
lm.ApplicationInstance = *h
}
// OriginatingUser
lm.OriginatingUser = originatingUser
// ServerName
lm.ServerName = "logproxy"
if s := msg.Hostname(); s != nil {
lm.ServerName = *s
}
// Severity
lm.Severity = "INFO"
if sev := msg.SeverityLevel(); sev != nil {
lm.Severity = *sev
}
// LogTime
lm.LogTime = time.Now().Format(logging.TimeFormat)
if m := msg.Timestamp(); m != nil {
lm.LogTime = m.Format(logging.TimeFormat)
}
parseProcID(msg, &lm)
// LogData
lm.LogData.Message = "no message identified"
if m := msg.Message(); m != nil {
lm.LogData.Message = *m
}
return lm
}
func parseProcID(msg syslog.Message, lm *logging.Resource) {
if procID := msg.ProcID(); procID != nil {
if rtrPattern.FindStringSubmatch(*procID) != nil && msg.Message() != nil {
m := msg.Message()
if rtr := rtrFormat.FindStringSubmatch(*m); rtr != nil {
if rtrTime, err := time.Parse(rtrTimeFormat, rtr[2]); err == nil {
lm.LogTime = rtrTime.Format(logging.TimeFormat)
}
}
} else {
lm.ApplicationInstance = EncodeString(*procID, applicationInstanceInvalidCharacters)
}
}
}