Skip to content

Commit

Permalink
[receiver/mongodbatlas] Add Events API Support (#18237)
Browse files Browse the repository at this point in the history
* Add events support for mongdobatlas receiver
  • Loading branch information
schmikei committed Feb 3, 2023
1 parent f5e42bf commit 61f5b38
Show file tree
Hide file tree
Showing 14 changed files with 1,039 additions and 10 deletions.
16 changes: 16 additions & 0 deletions .chloggen/mongodbatlas-events-support.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: mongodbatlasreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Adds Events API Logs Support

# One or more tracking issues related to the change
issues: [18061]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
29 changes: 28 additions & 1 deletion receiver/mongodbatlasreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ MongoDB Atlas [Documentation](https://www.mongodb.com/docs/atlas/reference/api/l
- `public_key` (required for metrics, logs, or alerts in `poll` mode)
- `private_key` (required for metrics, logs, or alerts in `poll` mode)
- `granularity` (default `PT1M` - See [MongoDB Atlas Documentation](https://docs.atlas.mongodb.com/reference/api/process-measurements/))
- `storage` configure the component ID of a storage extension. If specified, alerts `poll` mode will utilize the extension to ensure alerts are not duplicated after a collector restart.
- `storage` (optional) The component ID of a storage extension which can be used when polling for `alerts` or `events` . The storage extension prevents duplication of data after a collector restart by remembering which data were previously collected.
- `retry_on_failure`
- `enabled` (default true)
- `initial_interval` (default 5s)
Expand Down Expand Up @@ -55,6 +55,17 @@ MongoDB Atlas [Documentation](https://www.mongodb.com/docs/atlas/reference/api/l
- `collect_audit_logs` (default false)
- `include_clusters` (default empty)
- `exclude_clusters` (default empty)
- `events`
- `projects`
- `name` Name of the Project to discover events from
- `poll_interval` (default `1m`)
- How often the receiver will poll the Events API for new events.
- `page_size` (default `100`)
- This is the number of events that will be processed per request to the MongoDB Atlas API.
- `max_pages` (default `25`)
- This will limit how many pages of events the receiver will request from the MongoDB Atlas API for each project.
- `types` (defaults to all types of events)
- This is a list of [event types](https://www.mongodb.com/docs/atlas/reference/api/events-orgs-get-all/#event-type-values) that the receiver will request from the API. If specified, the receiver will collect only the indicated types of events.

Examples:

Expand Down Expand Up @@ -97,6 +108,7 @@ receivers:
```
Receive logs:
```yaml
receivers:
mongodbatlas:
Expand All @@ -107,5 +119,20 @@ receivers:
collect_audit_logs: true
```
Receive events:
```yaml
receivers:
mongodbatlas:
events:
projects:
- name: "project 1"
poll_interval: 1m
page_size: 100
max_pages: 25
# use of a storage extension is recommended to reduce chance of duplicated events
storage: file_storage
```
[beta]:https://github.com/open-telemetry/opentelemetry-collector#beta
[contrib]:https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib
13 changes: 13 additions & 0 deletions receiver/mongodbatlasreceiver/combined_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
type combinedLogsReceiver struct {
alerts *alertsReceiver
logs *logsReceiver
events *eventsReceiver
}

// Starts up the combined MongoDB Atlas Logs and Alert Receiver
Expand All @@ -43,6 +44,12 @@ func (c *combinedLogsReceiver) Start(ctx context.Context, host component.Host) e
}
}

if c.events != nil {
if err := c.events.Start(ctx, host); err != nil {
errs = multierr.Append(errs, err)
}
}

return errs
}

Expand All @@ -62,5 +69,11 @@ func (c *combinedLogsReceiver) Shutdown(ctx context.Context) error {
}
}

if c.events != nil {
if err := c.events.Shutdown(ctx); err != nil {
errs = multierr.Append(errs, err)
}
}

return errs
}
20 changes: 20 additions & 0 deletions receiver/mongodbatlasreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Config struct {
Granularity string `mapstructure:"granularity"`
Metrics metadata.MetricsSettings `mapstructure:"metrics"`
Alerts AlertConfig `mapstructure:"alerts"`
Events *EventsConfig `mapstructure:"events"`
Logs LogConfig `mapstructure:"logs"`
RetrySettings exporterhelper.RetrySettings `mapstructure:"retry_on_failure"`
StorageID *component.ID `mapstructure:"storage"`
Expand All @@ -63,6 +64,15 @@ type LogConfig struct {
Projects []*ProjectConfig `mapstructure:"projects"`
}

// EventsConfig is the configuration options for events collection
type EventsConfig struct {
Projects []*ProjectConfig `mapstructure:"projects"`
PollInterval time.Duration `mapstructure:"poll_interval"`
Types []string `mapstructure:"types"`
PageSize int64 `mapstructure:"page_size"`
MaxPages int64 `mapstructure:"max_pages"`
}

type ProjectConfig struct {
Name string `mapstructure:"name"`
ExcludeClusters []string `mapstructure:"exclude_clusters"`
Expand Down Expand Up @@ -109,6 +119,9 @@ func (c *Config) Validate() error {

errs = multierr.Append(errs, c.Alerts.validate())
errs = multierr.Append(errs, c.Logs.validate())
if c.Events != nil {
errs = multierr.Append(errs, c.Events.validate())
}

return errs
}
Expand Down Expand Up @@ -194,3 +207,10 @@ func (a AlertConfig) validateListenConfig() error {
}
return errs
}

func (e EventsConfig) validate() error {
if len(e.Projects) == 0 {
return errNoProjects
}
return nil
}
65 changes: 65 additions & 0 deletions receiver/mongodbatlasreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
package mongodbatlasreceiver

import (
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/confmap/confmaptest"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver/internal/metadata"
)

func TestValidate(t *testing.T) {
Expand Down Expand Up @@ -223,6 +229,15 @@ func TestValidate(t *testing.T) {
},
expectedErr: errPageSizeIncorrect.Error(),
},
{
name: "Invalid events config - no projects",
input: Config{
Events: &EventsConfig{
Projects: []*ProjectConfig{},
},
},
expectedErr: errNoProjects.Error(),
},
}

for _, tc := range testCases {
Expand All @@ -237,3 +252,53 @@ func TestValidate(t *testing.T) {
})
}
}

func TestLoadConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

factory := NewFactory()
cfg := factory.CreateDefaultConfig()

sub, err := cm.Sub(component.NewIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Metrics = metadata.DefaultMetricsSettings()
expected.PrivateKey = "my-private-key"
expected.PublicKey = "my-public-key"
expected.Logs = LogConfig{
Enabled: true,
Projects: []*ProjectConfig{
{
Name: "Project 0",
},
},
}
expected.Alerts = AlertConfig{
Enabled: true,
Mode: alertModePoll,
Projects: []*ProjectConfig{
{
Name: "Project 0",
IncludeClusters: []string{"Cluster0"},
},
},
PageSize: defaultAlertsPageSize,
MaxPages: defaultAlertsMaxPages,
PollInterval: time.Minute,
}

expected.Events = &EventsConfig{
Projects: []*ProjectConfig{
{
Name: "Project 0",
},
},
PollInterval: time.Minute,
MaxPages: defaultEventsMaxPages,
PageSize: defaultEventsPageSize,
}
require.Equal(t, expected, cfg)
}
Loading

0 comments on commit 61f5b38

Please sign in to comment.