-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
receiver.go
354 lines (314 loc) · 11.1 KB
/
receiver.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package mongodbatlasreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver"
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"go.mongodb.org/atlas/mongodbatlas"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/scraper"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver/internal/metadata"
)
type mongodbatlasreceiver struct {
log *zap.Logger
cfg *Config
client *internal.MongoDBAtlasClient
lastRun time.Time
mb *metadata.MetricsBuilder
stopperChan chan struct{}
}
type timeconstraints struct {
start string
end string
resolution string
}
func newMongoDBAtlasReceiver(settings receiver.Settings, cfg *Config) *mongodbatlasreceiver {
client := internal.NewMongoDBAtlasClient(cfg.PublicKey, string(cfg.PrivateKey), cfg.BackOffConfig, settings.Logger)
for _, p := range cfg.Projects {
p.populateIncludesAndExcludes()
}
return &mongodbatlasreceiver{
log: settings.Logger,
cfg: cfg,
client: client,
mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings),
stopperChan: make(chan struct{}),
}
}
func newMongoDBAtlasScraper(recv *mongodbatlasreceiver) (scraper.Metrics, error) {
return scraper.NewMetrics(recv.scrape, scraper.WithShutdown(recv.shutdown))
}
func (s *mongodbatlasreceiver) scrape(ctx context.Context) (pmetric.Metrics, error) {
now := time.Now()
if err := s.poll(ctx, s.timeConstraints(now)); err != nil {
return pmetric.Metrics{}, err
}
s.lastRun = now
return s.mb.Emit(), nil
}
func (s *mongodbatlasreceiver) timeConstraints(now time.Time) timeconstraints {
var start time.Time
if s.lastRun.IsZero() {
start = now.Add(s.cfg.CollectionInterval * -1)
} else {
start = s.lastRun
}
return timeconstraints{
start.UTC().Format(time.RFC3339),
now.UTC().Format(time.RFC3339),
s.cfg.Granularity,
}
}
func (s *mongodbatlasreceiver) shutdown(context.Context) error {
return s.client.Shutdown()
}
// poll decides whether to poll all projects or a specific project based on the configuration.
func (s *mongodbatlasreceiver) poll(ctx context.Context, time timeconstraints) error {
if len(s.cfg.Projects) == 0 {
return s.pollAllProjects(ctx, time)
}
return s.pollProjects(ctx, time)
}
// pollAllProjects handles polling across all projects within the organizations.
func (s *mongodbatlasreceiver) pollAllProjects(ctx context.Context, time timeconstraints) error {
orgs, err := s.client.Organizations(ctx)
if err != nil {
return fmt.Errorf("error retrieving organizations: %w", err)
}
for _, org := range orgs {
proj, err := s.client.Projects(ctx, org.ID)
if err != nil {
s.log.Error("error retrieving projects", zap.String("orgID", org.ID), zap.Error(err))
continue
}
for _, project := range proj {
// Since there is no specific ProjectConfig for these projects, pass nil.
if err := s.processProject(ctx, time, org.Name, project, nil); err != nil {
s.log.Error("error processing project", zap.String("projectID", project.ID), zap.Error(err))
}
}
}
return nil
}
// pollProject handles polling for specific projects as configured.
func (s *mongodbatlasreceiver) pollProjects(ctx context.Context, time timeconstraints) error {
for _, projectCfg := range s.cfg.Projects {
project, err := s.client.GetProject(ctx, projectCfg.Name)
if err != nil {
s.log.Error("error retrieving project", zap.String("projectName", projectCfg.Name), zap.Error(err))
continue
}
org, err := s.client.GetOrganization(ctx, project.OrgID)
if err != nil {
s.log.Error("error retrieving organization from project", zap.String("projectName", projectCfg.Name), zap.Error(err))
continue
}
if err := s.processProject(ctx, time, org.Name, project, projectCfg); err != nil {
s.log.Error("error processing project", zap.String("projectID", project.ID), zap.Error(err))
}
}
return nil
}
func (s *mongodbatlasreceiver) processProject(ctx context.Context, time timeconstraints, orgName string, project *mongodbatlas.Project, projectCfg *ProjectConfig) error {
nodeClusterMap, providerMap, err := s.getNodeClusterNameMap(ctx, project.ID)
if err != nil {
return fmt.Errorf("error collecting clusters from project %s: %w", project.ID, err)
}
processes, err := s.client.Processes(ctx, project.ID)
if err != nil {
return fmt.Errorf("error retrieving MongoDB Atlas processes for project %s: %w", project.ID, err)
}
for _, process := range processes {
clusterName := nodeClusterMap[process.UserAlias]
providerValues := providerMap[clusterName]
if !shouldProcessCluster(projectCfg, clusterName) {
// Skip processing for this cluster
continue
}
if err := s.extractProcessMetrics(ctx, time, orgName, project, process, clusterName, providerValues); err != nil {
return fmt.Errorf("error when polling process metrics from MongoDB Atlas for process %s: %w", process.ID, err)
}
if err := s.extractProcessDatabaseMetrics(ctx, time, orgName, project, process, clusterName, providerValues); err != nil {
return fmt.Errorf("error when polling process database metrics from MongoDB Atlas for process %s: %w", process.ID, err)
}
if err := s.extractProcessDiskMetrics(ctx, time, orgName, project, process, clusterName, providerValues); err != nil {
return fmt.Errorf("error when polling process disk metrics from MongoDB Atlas for process %s: %w", process.ID, err)
}
}
return nil
}
// shouldProcessCluster checks whether a given cluster should be processed based on the project configuration.
func shouldProcessCluster(projectCfg *ProjectConfig, clusterName string) bool {
if projectCfg == nil {
// If there is no project config, process all clusters.
return true
}
_, isIncluded := projectCfg.includesByClusterName[clusterName]
_, isExcluded := projectCfg.excludesByClusterName[clusterName]
// Return false immediately if the cluster is excluded.
if isExcluded {
return false
}
// If IncludeClusters is empty, or the cluster is explicitly included, return true.
return len(projectCfg.IncludeClusters) == 0 || isIncluded
}
type providerValues struct {
RegionName string
ProviderName string
}
func (s *mongodbatlasreceiver) getNodeClusterNameMap(
ctx context.Context,
projectID string,
) (map[string]string, map[string]providerValues, error) {
providerMap := make(map[string]providerValues)
clusterMap := make(map[string]string)
clusters, err := s.client.GetClusters(ctx, projectID)
if err != nil {
return nil, nil, err
}
for _, cluster := range clusters {
// URI in the form mongodb://host1.mongodb.net:27017,host2.mongodb.net:27017,host3.mongodb.net:27017
nodes := strings.Split(strings.TrimPrefix(cluster.MongoURI, "mongodb://"), ",")
for _, node := range nodes {
// Remove the port from the node
n, _, _ := strings.Cut(node, ":")
clusterMap[n] = cluster.Name
}
providerMap[cluster.Name] = providerValues{
RegionName: cluster.ProviderSettings.RegionName,
ProviderName: cluster.ProviderSettings.ProviderName,
}
}
return clusterMap, providerMap, nil
}
func (s *mongodbatlasreceiver) extractProcessMetrics(
ctx context.Context,
time timeconstraints,
orgName string,
project *mongodbatlas.Project,
process *mongodbatlas.Process,
clusterName string,
providerValues providerValues,
) error {
if err := s.client.ProcessMetrics(
ctx,
s.mb,
project.ID,
process.Hostname,
process.Port,
time.start,
time.end,
time.resolution,
); err != nil {
return fmt.Errorf("error when polling process metrics from MongoDB Atlas: %w", err)
}
rb := s.mb.NewResourceBuilder()
rb.SetMongodbAtlasOrgName(orgName)
rb.SetMongodbAtlasProjectName(project.Name)
rb.SetMongodbAtlasProjectID(project.ID)
rb.SetMongodbAtlasHostName(process.Hostname)
rb.SetMongodbAtlasUserAlias(process.UserAlias)
rb.SetMongodbAtlasClusterName(clusterName)
rb.SetMongodbAtlasProcessPort(strconv.Itoa(process.Port))
rb.SetMongodbAtlasProcessTypeName(process.TypeName)
rb.SetMongodbAtlasProcessID(process.ID)
rb.SetMongodbAtlasRegionName(providerValues.RegionName)
rb.SetMongodbAtlasProviderName(providerValues.ProviderName)
s.mb.EmitForResource(metadata.WithResource(rb.Emit()))
return nil
}
func (s *mongodbatlasreceiver) extractProcessDatabaseMetrics(
ctx context.Context,
time timeconstraints,
orgName string,
project *mongodbatlas.Project,
process *mongodbatlas.Process,
clusterName string,
providerValues providerValues,
) error {
processDatabases, err := s.client.ProcessDatabases(
ctx,
project.ID,
process.Hostname,
process.Port,
)
if err != nil {
return fmt.Errorf("error retrieving process databases: %w", err)
}
for _, db := range processDatabases {
if err := s.client.ProcessDatabaseMetrics(
ctx,
s.mb,
project.ID,
process.Hostname,
process.Port,
db.DatabaseName,
time.start,
time.end,
time.resolution,
); err != nil {
return fmt.Errorf("error when polling database metrics from MongoDB Atlas: %w", err)
}
rb := s.mb.NewResourceBuilder()
rb.SetMongodbAtlasOrgName(orgName)
rb.SetMongodbAtlasProjectName(project.Name)
rb.SetMongodbAtlasProjectID(project.ID)
rb.SetMongodbAtlasHostName(process.Hostname)
rb.SetMongodbAtlasUserAlias(process.UserAlias)
rb.SetMongodbAtlasClusterName(clusterName)
rb.SetMongodbAtlasProcessPort(strconv.Itoa(process.Port))
rb.SetMongodbAtlasProcessTypeName(process.TypeName)
rb.SetMongodbAtlasProcessID(process.ID)
rb.SetMongodbAtlasDbName(db.DatabaseName)
rb.SetMongodbAtlasRegionName(providerValues.RegionName)
rb.SetMongodbAtlasProviderName(providerValues.ProviderName)
s.mb.EmitForResource(metadata.WithResource(rb.Emit()))
}
return nil
}
func (s *mongodbatlasreceiver) extractProcessDiskMetrics(
ctx context.Context,
time timeconstraints,
orgName string,
project *mongodbatlas.Project,
process *mongodbatlas.Process,
clusterName string,
providerValues providerValues,
) error {
for _, disk := range s.client.ProcessDisks(ctx, project.ID, process.Hostname, process.Port) {
if err := s.client.ProcessDiskMetrics(
ctx,
s.mb,
project.ID,
process.Hostname,
process.Port,
disk.PartitionName,
time.start,
time.end,
time.resolution,
); err != nil {
return fmt.Errorf("error when polling disk metrics from MongoDB Atlas: %w", err)
}
rb := s.mb.NewResourceBuilder()
rb.SetMongodbAtlasOrgName(orgName)
rb.SetMongodbAtlasProjectName(project.Name)
rb.SetMongodbAtlasProjectID(project.ID)
rb.SetMongodbAtlasHostName(process.Hostname)
rb.SetMongodbAtlasUserAlias(process.UserAlias)
rb.SetMongodbAtlasClusterName(clusterName)
rb.SetMongodbAtlasProcessPort(strconv.Itoa(process.Port))
rb.SetMongodbAtlasProcessTypeName(process.TypeName)
rb.SetMongodbAtlasProcessID(process.ID)
rb.SetMongodbAtlasDiskPartition(disk.PartitionName)
rb.SetMongodbAtlasRegionName(providerValues.RegionName)
rb.SetMongodbAtlasProviderName(providerValues.ProviderName)
s.mb.EmitForResource(metadata.WithResource(rb.Emit()))
}
return nil
}