-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
factory.go
117 lines (101 loc) · 3.48 KB
/
factory.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
// 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"
"fmt"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver/internal/metadata"
)
const (
defaultGranularity = "PT1M" // 1-minute, as per https://docs.atlas.mongodb.com/reference/api/process-measurements/
defaultAlertsEnabled = false
defaultLogsEnabled = false
)
// NewFactory creates a factory for MongoDB Atlas receiver
func NewFactory() receiver.Factory {
return receiver.NewFactory(
metadata.Type,
createDefaultConfig,
receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability),
receiver.WithLogs(createCombinedLogReceiver, metadata.LogsStability))
}
func createMetricsReceiver(
_ context.Context,
params receiver.Settings,
rConf component.Config,
consumer consumer.Metrics,
) (receiver.Metrics, error) {
cfg := rConf.(*Config)
recv := newMongoDBAtlasReceiver(params, cfg)
ms, err := newMongoDBAtlasScraper(recv)
if err != nil {
return nil, fmt.Errorf("unable to create a MongoDB Atlas Scaper instance: %w", err)
}
return scraperhelper.NewScraperControllerReceiver(&cfg.ControllerConfig, params, consumer, scraperhelper.AddScraper(metadata.Type, ms))
}
func createCombinedLogReceiver(
_ context.Context,
params receiver.Settings,
rConf component.Config,
consumer consumer.Logs,
) (receiver.Logs, error) {
cfg := rConf.(*Config)
if !cfg.Alerts.Enabled && !cfg.Logs.Enabled && cfg.Events == nil {
return nil, errors.New("one of 'alerts', 'events', or 'logs' must be enabled")
}
var err error
recv := &combinedLogsReceiver{
id: params.ID,
storageID: cfg.StorageID,
}
if cfg.Alerts.Enabled {
recv.alerts, err = newAlertsReceiver(params, cfg, consumer)
if err != nil {
return nil, fmt.Errorf("unable to create a MongoDB Atlas Alerts Receiver instance: %w", err)
}
}
if cfg.Logs.Enabled {
recv.logs = newMongoDBAtlasLogsReceiver(params, cfg, consumer)
// Confirm at least one project is enabled for access logs before adding
for _, project := range cfg.Logs.Projects {
if project.AccessLogs != nil && project.AccessLogs.IsEnabled() {
recv.accessLogs = newAccessLogsReceiver(params, cfg, consumer)
break
}
}
}
if cfg.Events != nil {
recv.events = newEventsReceiver(params, cfg, consumer)
}
return recv, nil
}
func createDefaultConfig() component.Config {
c := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Granularity: defaultGranularity,
BackOffConfig: configretry.NewDefaultBackOffConfig(),
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
Alerts: AlertConfig{
Enabled: defaultAlertsEnabled,
Mode: alertModeListen,
PollInterval: defaultAlertsPollInterval,
PageSize: defaultAlertsPageSize,
MaxPages: defaultAlertsMaxPages,
},
Logs: LogConfig{
Enabled: defaultLogsEnabled,
Projects: []*LogsProjectConfig{},
},
}
// reset default of 1 minute to be 3 minutes in order to avoid null values for some metrics that do not publish
// more frequently
c.ControllerConfig.CollectionInterval = 3 * time.Minute
return c
}