Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/awsemfexporter] add tags feature for Cloudwatch Log Group #19406

Merged
merged 29 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
52e29ba
Add set tags to config
humivo Mar 3, 2023
639d0c1
Add config to cloudwatchlogsexporter too
humivo Mar 7, 2023
e25c61f
Add unit testing to aws emf
humivo Mar 8, 2023
08215ec
Add unit testing for other code changes and update README
humivo Mar 8, 2023
fb8c6c7
Add chlog entry
humivo Mar 8, 2023
e5c93d7
Merge branch 'main' into SetTagsLogGroup
humivo Mar 8, 2023
9792497
Fix issue number
humivo Mar 8, 2023
d086990
Change naming to accountID
humivo Mar 8, 2023
a56ab0c
Add sts endpoint and fix validate func
humivo Mar 16, 2023
80c0914
Fix import
humivo Mar 16, 2023
1af294c
Merge branch 'main' into SetTagsLogGroup
humivo Mar 16, 2023
6a72fec
Add comments to client code
humivo Mar 16, 2023
0e32886
Move functions to utils.go
humivo Mar 28, 2023
2ab601c
Remove line
humivo Mar 28, 2023
37403f4
Change tags call
humivo Apr 26, 2023
aa69c3d
Merge branch 'main' into SetTagsLogGroup
humivo Apr 26, 2023
57be341
Fix code after merge
humivo Apr 26, 2023
2ac02ce
Fix spelling error
humivo Apr 26, 2023
3576ddb
gci the file
humivo Apr 26, 2023
c3acf58
gci the exporter files
humivo Apr 26, 2023
71eb45c
Add gci option prefix
humivo Apr 26, 2023
27a640a
Add comment to cwlog_client
humivo Apr 27, 2023
ed6562f
Address comments on PR
humivo Apr 27, 2023
3701061
Improve testing
humivo May 9, 2023
18dacb3
Run gci command
humivo May 9, 2023
5a64e2d
Merge branch 'main' into SetTagsLogGroup
humivo May 9, 2023
5e99c16
Fix license header
humivo May 9, 2023
71e81c3
Simplify return statement
humivo May 11, 2023
26df4c3
Run gci command on code
humivo May 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add sts endpoint and fix validate func
  • Loading branch information
humivo committed Mar 16, 2023
commit a56ab0c156ad7d104289974c06e699ab613b9a2d
21 changes: 11 additions & 10 deletions exporter/awscloudwatchlogsexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package awscloudwatchlogsexporter // import "github.com/open-telemetry/opentelem

import (
"errors"
"fmt"
"regexp"

"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -86,9 +87,9 @@ func (config *Config) Validate() error {
if !isValidRetentionValue(config.LogRetention) {
return errors.New("invalid value for retention policy. Please make sure to use the following values: 0 (Never Expire), 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 2192, 2557, 2922, 3288, or 3653")
}
tagInputReturnVal := isValidTagsInput(config.Tags)
if tagInputReturnVal != "Valid" {
return errors.New(tagInputReturnVal)
tagInputErr := validateTagsInput(config.Tags)
if tagInputErr != nil {
return errors.New(tagInputErr.Error())
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}
Expand Down Expand Up @@ -125,28 +126,28 @@ func isValidRetentionValue(input int64) bool {
}

// Check if the tags input is valid
func isValidTagsInput(input map[string]*string) string {
func validateTagsInput(input map[string]*string) error {
if len(input) > 50 {
return "invalid amount of items. Please input at most 50 tags."
return fmt.Errorf("invalid amount of items. Please input at most 50 tags")
}
validKeyPattern := regexp.MustCompile(`^([\p{L}\p{Z}\p{N}_.:/=+\-@]+)$`)
validValuePattern := regexp.MustCompile(`^([\p{L}\p{Z}\p{N}_.:/=+\-@]*)$`)
for key, value := range input {
if len(key) < 1 || len(key) > 128 {
return "key - " + key + " has an invalid length. Please use keys with a length of 1 to 128 characters"
return fmt.Errorf("key - " + key + " has an invalid length. Please use keys with a length of 1 to 128 characters")
}
if len(*value) < 1 || len(*value) > 256 {
return "value - " + *value + " has an invalid length. Please use values with a length of 1 to 256 characters"
return fmt.Errorf("value - " + *value + " has an invalid length. Please use values with a length of 1 to 256 characters")
}
if !validKeyPattern.MatchString(key) {
return "key - " + key + " does not follow the regex pattern" + `^([\p{L}\p{Z}\p{N}_.:/=+\-@]+)$`
return fmt.Errorf("key - " + key + " does not follow the regex pattern" + `^([\p{L}\p{Z}\p{N}_.:/=+\-@]+)$`)
}
if !validValuePattern.MatchString(*value) {
return "value - " + *value + " does not follow the regex pattern" + `^([\p{L}\p{Z}\p{N}_.:/=+\-@]*)$`
return fmt.Errorf("value - " + *value + " does not follow the regex pattern" + `^([\p{L}\p{Z}\p{N}_.:/=+\-@]*)$`)
}
}

return "Valid"
return nil
}

func (config *Config) enforcedQueueSettings() exporterhelper.QueueSettings {
Expand Down
2 changes: 1 addition & 1 deletion exporter/awscloudwatchlogsexporter/config_test.go
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestTagsValidateTooManyTags(t *testing.T) {
QueueSize: exporterhelper.NewDefaultQueueSettings().QueueSize,
},
}
assert.EqualError(t, component.ValidateConfig(wrongcfg), "invalid amount of items. Please input at most 50 tags.")
assert.EqualError(t, component.ValidateConfig(wrongcfg), "invalid amount of items. Please input at most 50 tags")
}

func TestTagsValidateWrongKeyRegex(t *testing.T) {
Expand Down
21 changes: 11 additions & 10 deletions exporter/awsemfexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package awsemfexporter // import "github.com/open-telemetry/opentelemetry-collec
import (
"errors"
"regexp"
"fmt"

"go.uber.org/zap"

Expand Down Expand Up @@ -138,37 +139,37 @@ func (config *Config) Validate() error {
return errors.New("invalid value for retention policy. Please make sure to use the following values: 0 (Never Expire), 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 2192, 2557, 2922, 3288, or 3653")
}

tagInputReturnVal := isValidTagsInput(config.Tags)
if tagInputReturnVal != "Valid" {
return errors.New(tagInputReturnVal)
tagInputErr := validateTagsInput(config.Tags)
if tagInputErr != nil {
return errors.New(tagInputErr.Error())
}

return nil
}

// Check if the tags input is valid
func isValidTagsInput(input map[string]*string) string {
func validateTagsInput(input map[string]*string) error {
if len(input) > 50 {
return "invalid amount of items. Please input at most 50 tags."
return fmt.Errorf("invalid amount of items. Please input at most 50 tags")
}
validKeyPattern := regexp.MustCompile(`^([\p{L}\p{Z}\p{N}_.:/=+\-@]+)$`)
validValuePattern := regexp.MustCompile(`^([\p{L}\p{Z}\p{N}_.:/=+\-@]*)$`)
for key, value := range input {
if len(key) < 1 || len(key) > 128 {
return "key - " + key + " has an invalid length. Please use keys with a length of 1 to 128 characters"
return fmt.Errorf("key - " + key + " has an invalid length. Please use keys with a length of 1 to 128 characters")
}
if len(*value) < 1 || len(*value) > 256 {
return "value - " + *value + " has an invalid length. Please use values with a length of 1 to 256 characters"
return fmt.Errorf("value - " + *value + " has an invalid length. Please use values with a length of 1 to 256 characters")
}
if !validKeyPattern.MatchString(key) {
return "key - " + key + " does not follow the regex pattern" + `^([\p{L}\p{Z}\p{N}_.:/=+\-@]+)$`
return fmt.Errorf("key - " + key + " does not follow the regex pattern" + `^([\p{L}\p{Z}\p{N}_.:/=+\-@]+)$`)
}
if !validValuePattern.MatchString(*value) {
return "value - " + *value + " does not follow the regex pattern" + `^([\p{L}\p{Z}\p{N}_.:/=+\-@]*)$`
return fmt.Errorf("value - " + *value + " does not follow the regex pattern" + `^([\p{L}\p{Z}\p{N}_.:/=+\-@]*)$`)
}
}

return "Valid"
return nil
}

// Added function to check if value is an accepted number of log retention days
Expand Down
2 changes: 1 addition & 1 deletion exporter/awsemfexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestTagsValidateTooManyTags(t *testing.T) {
ResourceToTelemetrySettings: resourcetotelemetry.Settings{Enabled: true},
logger: zap.NewNop(),
}
assert.EqualError(t, component.ValidateConfig(wrongcfg), "invalid amount of items. Please input at most 50 tags.")
assert.EqualError(t, component.ValidateConfig(wrongcfg), "invalid amount of items. Please input at most 50 tags")
}

func TestTagsValidateWrongKeyRegex(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions internal/aws/cwlogs/cwlog_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
Expand Down Expand Up @@ -70,6 +71,7 @@ func NewClient(logger *zap.Logger, awsConfig *aws.Config, buildInfo component.Bu
if !reflect.ValueOf(awsConfig.Region).IsNil() {
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved
region = *awsConfig.Region
}
awsConfig = awsConfig.WithSTSRegionalEndpoint(endpoints.RegionalSTSEndpoint)
stsClient := sts.New(sess, awsConfig)
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved
accountCall, err := stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
Expand Down