-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
logs.go
279 lines (240 loc) · 8.02 KB
/
logs.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package mongodbatlasreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver"
import (
"context"
"errors"
"io"
"net"
"strings"
"sync"
"time"
"go.mongodb.org/atlas/mongodbatlas"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
rcvr "go.opentelemetry.io/collector/receiver"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver/internal/model"
)
const mongoDBMajorVersion4_2 = "4.2"
type logsReceiver struct {
log *zap.Logger
cfg *Config
client *internal.MongoDBAtlasClient
consumer consumer.Logs
stopperChan chan struct{}
wg sync.WaitGroup
start time.Time
end time.Time
}
type ProjectContext struct {
Project mongodbatlas.Project
orgName string
}
// MongoDB Atlas Documentation reccommends a polling interval of 5 minutes: https://www.mongodb.com/docs/atlas/reference/api/logs/#logs
const collectionInterval = time.Minute * 5
func newMongoDBAtlasLogsReceiver(settings rcvr.CreateSettings, cfg *Config, consumer consumer.Logs) *logsReceiver {
client := internal.NewMongoDBAtlasClient(cfg.PublicKey, string(cfg.PrivateKey), cfg.RetrySettings, settings.Logger)
for _, p := range cfg.Logs.Projects {
p.populateIncludesAndExcludes()
}
return &logsReceiver{
log: settings.Logger,
cfg: cfg,
client: client,
stopperChan: make(chan struct{}),
consumer: consumer,
}
}
// Log receiver logic
func (s *logsReceiver) Start(ctx context.Context, _ component.Host) error {
s.wg.Add(1)
go func() {
defer s.wg.Done()
s.start = time.Now().Add(-collectionInterval)
s.end = time.Now()
for {
s.collect(ctx)
// collection interval loop,
select {
case <-ctx.Done():
return
case <-s.stopperChan:
return
case <-time.After(collectionInterval):
s.start = s.end
s.end = time.Now()
}
}
}()
return nil
}
func (s *logsReceiver) Shutdown(_ context.Context) error {
close(s.stopperChan)
s.wg.Wait()
return s.client.Shutdown()
}
// parseHostNames parses out the hostname from the specified cluster host
func parseHostNames(s string, logger *zap.Logger) []string {
var hostnames []string
if s == "" {
return []string{}
}
for _, t := range strings.Split(s, ",") {
// separate hostname from scheme and port
host, _, err := net.SplitHostPort(strings.TrimPrefix(t, "mongodb://"))
if err != nil {
logger.Error("Could not parse out hostname: " + host)
continue
}
hostnames = append(hostnames, host)
}
return hostnames
}
// collect spins off functionality of the receiver from the Start function
func (s *logsReceiver) collect(ctx context.Context) {
for _, projectCfg := range s.cfg.Logs.Projects {
project, err := s.client.GetProject(ctx, projectCfg.Name)
if err != nil {
s.log.Error("Error retrieving project "+projectCfg.Name+":", zap.Error(err))
continue
}
pc := ProjectContext{Project: *project}
org, err := s.client.GetOrganization(ctx, project.OrgID)
if err != nil {
s.log.Error("Error retrieving organization", zap.Error(err))
pc.orgName = "unknown"
} else {
pc.orgName = org.Name
}
// get clusters for each of the projects
clusters, err := s.processClusters(ctx, *projectCfg, project.ID)
if err != nil {
s.log.Error("Failure to process Clusters", zap.Error(err))
}
s.collectClusterLogs(clusters, *projectCfg, pc)
}
}
func (s *logsReceiver) processClusters(ctx context.Context, projectCfg LogsProjectConfig, projectID string) ([]mongodbatlas.Cluster, error) {
clusters, err := s.client.GetClusters(ctx, projectID)
if err != nil {
s.log.Error("Failure to collect clusters from project: %w", zap.Error(err))
return nil, err
}
return filterClusters(clusters, projectCfg.ProjectConfig)
}
func (s *logsReceiver) collectClusterLogs(clusters []mongodbatlas.Cluster, projectCfg LogsProjectConfig, pc ProjectContext) {
for _, cluster := range clusters {
hostnames := parseHostNames(cluster.ConnectionStrings.Standard, s.log)
for _, hostname := range hostnames {
// Defaults to true if not specified
if projectCfg.EnableHostLogs == nil || *projectCfg.EnableHostLogs {
s.collectLogs(pc, hostname, "mongodb.gz", cluster.Name, cluster.MongoDBMajorVersion)
s.collectLogs(pc, hostname, "mongos.gz", cluster.Name, cluster.MongoDBMajorVersion)
}
// Defaults to false if not specified
if projectCfg.EnableAuditLogs {
s.collectAuditLogs(pc, hostname, "mongodb-audit-log.gz", cluster.Name, cluster.MongoDBMajorVersion)
s.collectAuditLogs(pc, hostname, "mongos-audit-log.gz", cluster.Name, cluster.MongoDBMajorVersion)
}
}
}
}
func filterClusters(clusters []mongodbatlas.Cluster, projectCfg ProjectConfig) ([]mongodbatlas.Cluster, error) {
include, exclude := projectCfg.IncludeClusters, projectCfg.ExcludeClusters
var allowed bool
var clusterNameSet map[string]struct{}
// check to include or exclude clusters
switch {
// keep all clusters if include and exclude are not specified
case len(include) == 0 && len(exclude) == 0:
return clusters, nil
// include is initialized
case len(include) > 0 && len(exclude) == 0:
allowed = true
clusterNameSet = projectCfg.includesByClusterName
// exclude is initialized
case len(exclude) > 0 && len(include) == 0:
allowed = false
clusterNameSet = projectCfg.excludesByClusterName
// both are initialized
default:
return nil, errors.New("both Include and Exclude clusters configured")
}
var filtered []mongodbatlas.Cluster
for _, cluster := range clusters {
if _, ok := clusterNameSet[cluster.Name]; (!ok && !allowed) || (ok && allowed) {
filtered = append(filtered, cluster)
}
}
return filtered, nil
}
func (s *logsReceiver) getHostLogs(groupID, hostname, logName string, clusterMajorVersion string) ([]model.LogEntry, error) {
// Get gzip bytes buffer from API
buf, err := s.client.GetLogs(context.Background(), groupID, hostname, logName, s.start, s.end)
if err != nil {
return nil, err
}
return decodeLogs(s.log, clusterMajorVersion, buf)
}
func (s *logsReceiver) getHostAuditLogs(groupID, hostname, logName string) ([]model.AuditLog, error) {
// Get gzip bytes buffer from API
buf, err := s.client.GetLogs(context.Background(), groupID, hostname, logName, s.start, s.end)
if err != nil {
return nil, err
}
return decodeAuditJSON(s.log, buf)
}
func (s *logsReceiver) collectLogs(pc ProjectContext, hostname, logName, clusterName, clusterMajorVersion string) {
logs, err := s.getHostLogs(pc.Project.ID, hostname, logName, clusterMajorVersion)
if err != nil && !errors.Is(err, io.EOF) {
s.log.Warn("Failed to retrieve host logs", zap.Error(err), zap.String("log", logName))
return
}
if len(logs) == 0 {
s.log.Warn("Attempted to retrieve host logs but received 0 logs", zap.Error(err), zap.String("log", logName))
return
}
plog := mongodbEventToLogData(s.log,
logs,
pc,
hostname,
logName,
clusterName,
clusterMajorVersion)
err = s.consumer.ConsumeLogs(context.Background(), plog)
if err != nil {
s.log.Error("Failed to consume logs", zap.Error(err))
}
}
func (s *logsReceiver) collectAuditLogs(pc ProjectContext, hostname, logName, clusterName, clusterMajorVersion string) {
logs, err := s.getHostAuditLogs(
pc.Project.ID,
hostname,
logName,
)
if err != nil && !errors.Is(err, io.EOF) {
s.log.Warn("Failed to retrieve audit logs", zap.Error(err), zap.String("log", logName))
return
}
if len(logs) == 0 {
s.log.Warn("Attempted to retrieve audit logs but received 0 logs", zap.Error(err), zap.String("log", logName))
return
}
plog, err := mongodbAuditEventToLogData(s.log,
logs,
pc,
hostname,
logName,
clusterName,
clusterMajorVersion)
if err != nil {
s.log.Warn("Failed to translate audit logs: "+logName, zap.Error(err))
return
}
err = s.consumer.ConsumeLogs(context.Background(), plog)
if err != nil {
s.log.Error("Failed to consume logs", zap.Error(err))
}
}