-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
log_decoder.go
120 lines (99 loc) · 3.31 KB
/
log_decoder.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package mongodbatlasreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver"
import (
"bufio"
"compress/gzip"
"encoding/json"
"io"
"regexp"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver/internal/model"
)
func decodeLogs(logger *zap.Logger, clusterMajorVersion string, r io.Reader) ([]model.LogEntry, error) {
switch clusterMajorVersion {
case mongoDBMajorVersion4_2:
// 4.2 clusters use a console log format
return decode4_2(logger.Named("console_decoder"), r)
default:
// All other versions use JSON logging
return decodeJSON(logger.Named("json_decoder"), r)
}
}
func decodeJSON(logger *zap.Logger, r io.Reader) ([]model.LogEntry, error) {
// Pass this into a gzip reader for decoding
gzipReader, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(gzipReader)
var entries []model.LogEntry
for {
if !scanner.Scan() {
// Scan failed; This might just be EOF, in which case Err will be nil, or it could be some other IO error.
return entries, scanner.Err()
}
var entry model.LogEntry
if err := json.Unmarshal(scanner.Bytes(), &entry); err != nil {
logger.Error("Failed to parse log entry as JSON", zap.String("entry", scanner.Text()))
continue
}
entry.Raw = scanner.Text()
entries = append(entries, entry)
}
}
var mongo4_2LogRegex = regexp.MustCompile(`^(?P<timestamp>\S+)\s+(?P<severity>\w+)\s+(?P<component>[\w-]+)\s+\[(?P<context>\S+)\]\s+(?P<message>.*)$`)
func decode4_2(logger *zap.Logger, r io.Reader) ([]model.LogEntry, error) {
// Pass this into a gzip reader for decoding
gzipReader, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(gzipReader)
var entries []model.LogEntry
for {
if !scanner.Scan() {
// Scan failed; This might just be EOF, in which case Err will be nil, or it could be some other IO error.
return entries, scanner.Err()
}
submatches := mongo4_2LogRegex.FindStringSubmatch(scanner.Text())
if len(submatches) != 6 {
// Match failed for line; We will skip this line and continue processing others.
logger.Error("Entry did not match regex", zap.String("entry", scanner.Text()))
continue
}
entry := model.LogEntry{
Timestamp: model.LogTimestamp{
Date: submatches[1],
},
Severity: submatches[2],
Component: submatches[3],
Context: submatches[4],
Message: submatches[5],
Raw: submatches[0],
}
entries = append(entries, entry)
}
}
func decodeAuditJSON(logger *zap.Logger, r io.Reader) ([]model.AuditLog, error) {
// Pass this into a gzip reader for decoding
gzipReader, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(gzipReader)
var entries []model.AuditLog
for {
if !scanner.Scan() {
// Scan failed; This might just be EOF, in which case Err will be nil, or it could be some other IO error.
return entries, scanner.Err()
}
var entry model.AuditLog
if err := json.Unmarshal(scanner.Bytes(), &entry); err != nil {
logger.Error("Failed to parse audit log entry as JSON", zap.String("entry", scanner.Text()))
continue
}
entry.Raw = scanner.Text()
entries = append(entries, entry)
}
}