-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
mongodb_atlas_client.go
558 lines (524 loc) · 14 KB
/
mongodb_atlas_client.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// nolint:errcheck
package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver/internal"
import (
"context"
"fmt"
"net/http"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/mongodb-forks/digest"
"github.com/pkg/errors"
"go.mongodb.org/atlas/mongodbatlas"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver/internal/metadata"
)
type clientRoundTripper struct {
originalTransport http.RoundTripper
log *zap.Logger
retrySettings exporterhelper.RetrySettings
isStopped bool
shutdownChan chan struct{}
}
func newClientRoundTripper(
originalTransport http.RoundTripper,
log *zap.Logger,
retrySettings exporterhelper.RetrySettings) *clientRoundTripper {
return &clientRoundTripper{
originalTransport: originalTransport,
log: log,
retrySettings: retrySettings,
shutdownChan: make(chan struct{}, 1),
}
}
func (rt *clientRoundTripper) Shutdown() error {
rt.isStopped = true
rt.shutdownChan <- struct{}{}
close(rt.shutdownChan)
return nil
}
func (rt *clientRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
if rt.isStopped {
return nil, fmt.Errorf("request cancelled due to shutdown")
}
resp, err := rt.originalTransport.RoundTrip(r)
if err != nil {
return nil, err // Can't do anything
}
if resp.StatusCode == 429 {
expBackoff := &backoff.ExponentialBackOff{
InitialInterval: rt.retrySettings.InitialInterval,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: backoff.DefaultMultiplier,
MaxInterval: rt.retrySettings.MaxInterval,
MaxElapsedTime: rt.retrySettings.MaxElapsedTime,
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}
expBackoff.Reset()
attempts := 0
for {
attempts++
delay := expBackoff.NextBackOff()
if delay == backoff.Stop {
return resp, err
}
rt.log.Warn("server busy, retrying request",
zap.Int("attempts", attempts),
zap.Duration("delay", delay))
select {
case <-r.Context().Done():
return resp, fmt.Errorf("request was cancelled or timed out")
case <-rt.shutdownChan:
return resp, fmt.Errorf("request is cancelled due to server shutdown")
case <-time.After(delay):
}
resp, err = rt.originalTransport.RoundTrip(r)
if err != nil {
return nil, err
}
if resp.StatusCode != 429 {
break
}
}
}
return resp, err
}
// MongoDBAtlasClient wraps the official MongoDB Atlas client to manage pagination
// and mapping to OpenTelmetry metric and log structures.
type MongoDBAtlasClient struct {
log *zap.Logger
client *mongodbatlas.Client
roundTripper *clientRoundTripper
}
// NewMongoDBAtlasClient creates a new MongoDB Atlas client wrapper
func NewMongoDBAtlasClient(
publicKey string,
privateKey string,
retrySettings exporterhelper.RetrySettings,
log *zap.Logger,
) (*MongoDBAtlasClient, error) {
t := digest.NewTransport(publicKey, privateKey)
roundTripper := newClientRoundTripper(t, log, retrySettings)
tc := &http.Client{Transport: roundTripper}
client := mongodbatlas.NewClient(tc)
return &MongoDBAtlasClient{
log,
client,
roundTripper,
}, nil
}
func (s *MongoDBAtlasClient) Shutdown() error {
s.roundTripper.Shutdown()
return nil
}
// Check both the returned error and the status of the HTTP response
func checkMongoDBClientErr(err error, response *mongodbatlas.Response) error {
if err != nil {
return err
}
if response != nil {
return mongodbatlas.CheckResponse(response.Response)
}
return nil
}
func hasNext(links []*mongodbatlas.Link) bool {
for _, link := range links {
if link.Rel == "next" {
return true
}
}
return false
}
// Organizations returns a list of all organizations available with the supplied credentials
func (s *MongoDBAtlasClient) Organizations(ctx context.Context) ([]*mongodbatlas.Organization, error) {
allOrgs := make([]*mongodbatlas.Organization, 0)
page := 1
for {
orgs, hasNext, err := s.getOrganizationsPage(ctx, page)
page++
if err != nil {
// TODO: Add error to a metric
// Stop, returning what we have (probably empty slice)
return allOrgs, errors.Wrap(err, "error retrieving organizations from MongoDB Atlas API")
}
allOrgs = append(allOrgs, orgs...)
if !hasNext {
break
}
}
return allOrgs, nil
}
func (s *MongoDBAtlasClient) getOrganizationsPage(
ctx context.Context,
pageNum int,
) ([]*mongodbatlas.Organization, bool, error) {
orgs, response, err := s.client.Organizations.List(ctx, &mongodbatlas.OrganizationsListOptions{
ListOptions: mongodbatlas.ListOptions{
PageNum: pageNum,
},
})
err = checkMongoDBClientErr(err, response)
if err != nil {
return nil, false, fmt.Errorf("error in retrieving organizations: %w", err)
}
return orgs.Results, hasNext(orgs.Links), nil
}
// Projects returns a list of projects accessible within the provided organization
func (s *MongoDBAtlasClient) Projects(
ctx context.Context,
orgID string,
) ([]*mongodbatlas.Project, error) {
allProjects := make([]*mongodbatlas.Project, 0)
page := 1
for {
projects, hasNext, err := s.getProjectsPage(ctx, orgID, page)
page++
if err != nil {
return allProjects, errors.Wrap(err, "error retrieving list of projects from MongoDB Atlas API")
}
allProjects = append(allProjects, projects...)
if !hasNext {
break
}
}
return allProjects, nil
}
func (s *MongoDBAtlasClient) getProjectsPage(
ctx context.Context,
orgID string,
pageNum int,
) ([]*mongodbatlas.Project, bool, error) {
projects, response, err := s.client.Organizations.Projects(
ctx,
orgID,
&mongodbatlas.ListOptions{PageNum: pageNum},
)
err = checkMongoDBClientErr(err, response)
if err != nil {
return nil, false, errors.Wrap(err, "error retrieving project page")
}
return projects.Results, hasNext(projects.Links), nil
}
// Processes returns the list of processes running for a given project.
func (s *MongoDBAtlasClient) Processes(
ctx context.Context,
projectID string,
) ([]*mongodbatlas.Process, error) {
// A paginated API, but the MongoDB client just returns the values from the first page
// Note: MongoDB Atlas also has the idea of a Cluster- we can retrieve a list of clusters from
// the Project, but a Cluster does not have a link to its Process list and a Process does not
// have a link to its Cluster (save through the hostname, which is not a documented relationship).
processes, response, err := s.client.Processes.List(
ctx,
projectID,
&mongodbatlas.ProcessesListOptions{
ListOptions: mongodbatlas.ListOptions{
PageNum: 0,
ItemsPerPage: 0,
IncludeCount: true,
},
},
)
err = checkMongoDBClientErr(err, response)
if err != nil {
return make([]*mongodbatlas.Process, 0), errors.Wrap(err, "error retrieving processes from MongoDB Atlas API")
}
return processes, nil
}
func (s *MongoDBAtlasClient) getProcessDatabasesPage(
ctx context.Context,
projectID string,
host string,
port int,
pageNum int,
) ([]*mongodbatlas.ProcessDatabase, bool, error) {
databases, response, err := s.client.ProcessDatabases.List(
ctx,
projectID,
host,
port,
&mongodbatlas.ListOptions{PageNum: pageNum},
)
err = checkMongoDBClientErr(err, response)
if err != nil {
return nil, false, err
}
return databases.Results, hasNext(databases.Links), nil
}
// ProcessDatabases lists databases that are running in a given MongoDB Atlas process
func (s *MongoDBAtlasClient) ProcessDatabases(
ctx context.Context,
projectID string,
host string,
port int,
) ([]*mongodbatlas.ProcessDatabase, error) {
allProcessDatabases := make([]*mongodbatlas.ProcessDatabase, 0)
pageNum := 1
for {
processes, hasMore, err := s.getProcessDatabasesPage(ctx, projectID, host, port, pageNum)
pageNum++
if err != nil {
return allProcessDatabases, err
}
allProcessDatabases = append(allProcessDatabases, processes...)
if !hasMore {
break
}
}
return allProcessDatabases, nil
}
// ProcessMetrics returns a set of metrics associated with the specified running process.
func (s *MongoDBAtlasClient) ProcessMetrics(
ctx context.Context,
mb *metadata.MetricsBuilder,
projectID string,
host string,
port int,
start string,
end string,
resolution string,
) error {
allMeasurements := make([]*mongodbatlas.Measurements, 0)
pageNum := 1
for {
measurements, hasMore, err := s.getProcessMeasurementsPage(
ctx,
projectID,
host,
port,
pageNum,
start,
end,
resolution,
)
if err != nil {
s.log.Debug("Error retrieving process metrics from MongoDB Atlas API", zap.Error(err))
break // Return partial results
}
pageNum++
allMeasurements = append(allMeasurements, measurements...)
if !hasMore {
break
}
}
return processMeasurements(mb, allMeasurements)
}
func (s *MongoDBAtlasClient) getProcessMeasurementsPage(
ctx context.Context,
projectID string,
host string,
port int,
pageNum int,
start string,
end string,
resolution string,
) ([]*mongodbatlas.Measurements, bool, error) {
measurements, result, err := s.client.ProcessMeasurements.List(
ctx,
projectID,
host,
port,
&mongodbatlas.ProcessMeasurementListOptions{
ListOptions: &mongodbatlas.ListOptions{PageNum: pageNum},
Granularity: resolution,
Start: start,
End: end,
},
)
err = checkMongoDBClientErr(err, result)
if err != nil {
return nil, false, err
}
return measurements.Measurements, hasNext(measurements.Links), nil
}
// ProcessDatabaseMetrics returns metrics about a particular database running within a MongoDB Atlas process
func (s *MongoDBAtlasClient) ProcessDatabaseMetrics(
ctx context.Context,
mb *metadata.MetricsBuilder,
projectID string,
host string,
port int,
dbname string,
start string,
end string,
resolution string,
) error {
allMeasurements := make([]*mongodbatlas.Measurements, 0)
pageNum := 1
for {
measurements, hasMore, err := s.getProcessDatabaseMeasurementsPage(
ctx,
projectID,
host,
port,
dbname,
pageNum,
start,
end,
resolution,
)
if err != nil {
return err
}
pageNum++
allMeasurements = append(allMeasurements, measurements...)
if !hasMore {
break
}
}
return processMeasurements(mb, allMeasurements)
}
func (s *MongoDBAtlasClient) getProcessDatabaseMeasurementsPage(
ctx context.Context,
projectID string,
host string,
port int,
dbname string,
pageNum int,
start string,
end string,
resolution string,
) ([]*mongodbatlas.Measurements, bool, error) {
measurements, result, err := s.client.ProcessDatabaseMeasurements.List(
ctx,
projectID,
host,
port,
dbname,
&mongodbatlas.ProcessMeasurementListOptions{
ListOptions: &mongodbatlas.ListOptions{PageNum: pageNum},
Granularity: resolution,
Start: start,
End: end,
},
)
err = checkMongoDBClientErr(err, result)
if err != nil {
return nil, false, err
}
return measurements.Measurements, hasNext(measurements.Links), nil
}
// ProcessDisks enumerates the disks accessible to a specified MongoDB Atlas process
func (s *MongoDBAtlasClient) ProcessDisks(
ctx context.Context,
projectID string,
host string,
port int,
) []*mongodbatlas.ProcessDisk {
allDisks := make([]*mongodbatlas.ProcessDisk, 0)
pageNum := 1
for {
disks, hasMore, err := s.getProcessDisksPage(ctx, projectID, host, port, pageNum)
if err != nil {
s.log.Debug("Error retrieving disk metrics from MongoDB Atlas API", zap.Error(err))
break // Return partial results
}
pageNum++
allDisks = append(allDisks, disks...)
if !hasMore {
break
}
}
return allDisks
}
func (s *MongoDBAtlasClient) getProcessDisksPage(
ctx context.Context,
projectID string,
host string,
port int,
pageNum int,
) ([]*mongodbatlas.ProcessDisk, bool, error) {
disks, result, err := s.client.ProcessDisks.List(
ctx,
projectID,
host,
port,
&mongodbatlas.ListOptions{PageNum: pageNum},
)
err = checkMongoDBClientErr(err, result)
if err != nil {
return nil, false, err
}
return disks.Results, hasNext(disks.Links), nil
}
// ProcessDiskMetrics returns metrics supplied for a particular disk partition used by a MongoDB Atlas process
func (s *MongoDBAtlasClient) ProcessDiskMetrics(
ctx context.Context,
mb *metadata.MetricsBuilder,
projectID string,
host string,
port int,
partitionName string,
start string,
end string,
resolution string,
) error {
allMeasurements := make([]*mongodbatlas.Measurements, 0)
pageNum := 1
for {
measurements, hasMore, err := s.processDiskMeasurementsPage(
ctx,
projectID,
host,
port,
partitionName,
pageNum,
start,
end,
resolution,
)
if err != nil {
return err
}
pageNum++
allMeasurements = append(allMeasurements, measurements...)
if !hasMore {
break
}
}
return processMeasurements(mb, allMeasurements)
}
func (s *MongoDBAtlasClient) processDiskMeasurementsPage(
ctx context.Context,
projectID string,
host string,
port int,
partitionName string,
pageNum int,
start string,
end string,
resolution string,
) ([]*mongodbatlas.Measurements, bool, error) {
measurements, result, err := s.client.ProcessDiskMeasurements.List(
ctx,
projectID,
host,
port,
partitionName,
&mongodbatlas.ProcessMeasurementListOptions{
ListOptions: &mongodbatlas.ListOptions{PageNum: pageNum},
Granularity: resolution,
Start: start,
End: end,
},
)
err = checkMongoDBClientErr(err, result)
if err != nil {
return nil, false, err
}
return measurements.Measurements, hasNext(measurements.Links), nil
}