Skip to content

Commit

Permalink
[receiver/awscloudwatch] Fix validation in some scenarios (#14953)
Browse files Browse the repository at this point in the history
* Fix validation of some awscloudwatch configs that used named groups
  • Loading branch information
schmikei committed Oct 18, 2022
1 parent fa933d6 commit e2f8e76
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
16 changes: 16 additions & 0 deletions .chloggen/fix-validate-of-awscloudwatchreceiver.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: bug_fix

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix config validation for some named configurations.

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

# (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:
3 changes: 1 addition & 2 deletions receiver/awscloudwatchreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Receives Cloudwatch events from [AWS Cloudwatch](https://aws.amazon.com/cloudwat

## Getting Started

This receiver uses the [AWS SDK Profiles](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) as mode of authentication.
This receiver uses the [AWS SDK](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html) as mode of authentication, which includes [Profile](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) and [IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) authentication for EC2 instances.

## Configuration

Expand Down Expand Up @@ -77,7 +77,6 @@ awscloudwatch:
names: [kube-apiserver-ea9c831555adca1815ae04b87661klasdj]
```


## Sample Configs

This receiver has a number of sample configs for reference.
Expand Down
18 changes: 18 additions & 0 deletions receiver/awscloudwatchreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/confmap"
"go.uber.org/multierr"
)

Expand Down Expand Up @@ -93,6 +94,23 @@ func (c *Config) Validate() error {
return errs
}

// Unmarshal is a custom unmarshaller that ensures that autodiscover is nil if
// autodiscover is not specified
func (c *Config) Unmarshal(componentParser *confmap.Conf) error {
if componentParser == nil {
return errors.New("")
}
err := componentParser.Unmarshal(c, confmap.WithErrorUnused())
if err != nil {
return err
}

if componentParser.IsSet("logs::groups::named") && !componentParser.IsSet("logs::groups::autodiscover") {
c.Logs.Groups.AutodiscoverConfig = nil
}
return nil
}

func (c *Config) validateLogsConfig() error {
if c.Logs == nil {
return errNoLogsConfigured
Expand Down
30 changes: 22 additions & 8 deletions receiver/awscloudwatchreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ func TestValidate(t *testing.T) {
},
expectedErr: errors.New("unable to parse URI for imds_endpoint"),
},
{
name: "Both Logs Autodiscover and Named Set",
config: Config{
Region: "us-east-1",
Logs: &LogsConfig{
MaxEventsPerRequest: defaultEventLimit,
PollInterval: defaultPollInterval,
Groups: GroupConfig{
AutodiscoverConfig: &AutodiscoverConfig{
Limit: defaultEventLimit,
},
NamedConfigs: map[string]StreamConfig{
"some-log-group": {
Names: []*string{aws.String("some-lg-name")},
},
},
},
},
},
expectedErr: errAutodiscoverAndNamedConfigured,
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -218,10 +239,6 @@ func TestLoadConfig(t *testing.T) {
PollInterval: 5 * time.Minute,
MaxEventsPerRequest: defaultEventLimit,
Groups: GroupConfig{
// this is ignored since named configs are present
AutodiscoverConfig: &AutodiscoverConfig{
Limit: defaultLogGroupLimit,
},
NamedConfigs: map[string]StreamConfig{
"/aws/eks/dev-0/cluster": {},
},
Expand All @@ -239,10 +256,6 @@ func TestLoadConfig(t *testing.T) {
PollInterval: 5 * time.Minute,
MaxEventsPerRequest: defaultEventLimit,
Groups: GroupConfig{
// this is ignored since named configs are present
AutodiscoverConfig: &AutodiscoverConfig{
Limit: defaultLogGroupLimit,
},
NamedConfigs: map[string]StreamConfig{
"/aws/eks/dev-0/cluster": {
Names: []*string{aws.String("kube-apiserver-ea9c831555adca1815ae04b87661klasdj")},
Expand All @@ -263,6 +276,7 @@ func TestLoadConfig(t *testing.T) {
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(loaded, cfg))
require.Equal(t, cfg, tc.expectedConfig)
require.NoError(t, cfg.Validate())
})
}
}

0 comments on commit e2f8e76

Please sign in to comment.