-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Fix a few issues with functionbeat #8713
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ import ( | |
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
|
@@ -22,6 +24,11 @@ import ( | |
"github.com/elastic/beats/x-pack/functionbeat/provider/aws/transformer" | ||
) | ||
|
||
var ( | ||
logGroupNamePattern = "^[\\.\\-_/#A-Za-z0-9]+$" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use backticks for raw strings. Then you don't have to escape the escapes. |
||
logGroupNameRE = regexp.MustCompile(logGroupNamePattern) | ||
) | ||
|
||
// CloudwatchLogsConfig is the configuration for the cloudwatchlogs event type. | ||
type CloudwatchLogsConfig struct { | ||
Triggers []*CloudwatchLogsTriggerConfig `config:"triggers"` | ||
|
@@ -32,8 +39,8 @@ type CloudwatchLogsConfig struct { | |
|
||
// CloudwatchLogsTriggerConfig is the configuration for the specific triggers for cloudwatch. | ||
type CloudwatchLogsTriggerConfig struct { | ||
LogGroupName string `config:"log_group_name" validate:"nonzero,required"` | ||
FilterPattern string `config:"filter_pattern"` | ||
LogGroupName logGroupName `config:"log_group_name" validate:"nonzero,required"` | ||
FilterPattern string `config:"filter_pattern"` | ||
} | ||
|
||
// Validate validates the configuration. | ||
|
@@ -44,6 +51,33 @@ func (cfg *CloudwatchLogsConfig) Validate() error { | |
return nil | ||
} | ||
|
||
// DOC: see validations rules at https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_CreateLogGroup.html | ||
type logGroupName string | ||
|
||
// Unpack takes a string and validate the log group format. | ||
func (l *logGroupName) Unpack(s string) error { | ||
const max = 512 | ||
const min = 1 | ||
|
||
if len(s) > max { | ||
return fmt.Errorf("log group name '%s' is too long, maximum length is %d", s, max) | ||
} | ||
|
||
if len(s) < min { | ||
return fmt.Errorf("log group name too short, minimum length is %d", min) | ||
} | ||
|
||
if !logGroupNameRE.MatchString(s) { | ||
return fmt.Errorf( | ||
"invalid characters in log group name '%s', name must match regular expression: '%s'", | ||
s, | ||
logGroupNamePattern, | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not everyone will understand the regex being printed (it even contains escapes). How about: |
||
} | ||
*l = logGroupName(s) | ||
return nil | ||
} | ||
|
||
// CloudwatchLogs receives CloudwatchLogs events from a lambda function and forward the logs to | ||
// an Elasticsearch cluster. | ||
type CloudwatchLogs struct { | ||
|
@@ -157,7 +191,7 @@ func (c *CloudwatchLogs) Template() *cloudformation.Template { | |
":", | ||
cloudformation.Ref("AWS::AccountId"), | ||
":log-group:", | ||
trigger.LogGroupName, | ||
string(trigger.LogGroupName), | ||
":*", | ||
}, | ||
), | ||
|
@@ -168,10 +202,10 @@ func (c *CloudwatchLogs) Template() *cloudformation.Template { | |
} | ||
|
||
// doc: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-subscriptionfilter.html | ||
template.Resources[prefix("SubscriptionFilter"+normalize(trigger.LogGroupName))] = &AWSLogsSubscriptionFilter{ | ||
template.Resources[prefix("SubscriptionFilter"+normalize(string(trigger.LogGroupName)))] = &AWSLogsSubscriptionFilter{ | ||
DestinationArn: cloudformation.GetAtt(prefix(""), "Arn"), | ||
FilterPattern: trigger.FilterPattern, | ||
LogGroupName: trigger.LogGroupName, | ||
LogGroupName: string(trigger.LogGroupName), | ||
} | ||
} | ||
return template | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have mixed feeling about how to display to display provider options vs functions.
What would be the best way to make it clean in the YML?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say the format chosen very much fits our conventions. If there is a chance we might introduce even more settings you can write:
I'm in favor of having
functionbeat.provider.aws.functions:
top-level. This reduces the amount of indentation and copy'n paste errors.Alternatively:
I wonder if we even need to have an explicit provider on functions. Normally the provider is implicit by function type. By having providers 'implicit', we can't limit the max number of providers in a config file to 1.