From 66fcd954645a0eb00c602ba62b67128fcec6a2bb Mon Sep 17 00:00:00 2001 From: Vincent Mercier Date: Mon, 23 Oct 2023 22:35:55 +0200 Subject: [PATCH] feat(exporter): Add --collect-instance-metrics parameter to enable/disable AWS instance metrics --- README.md | 1 + cmd/root.go | 40 ++++++++++++++++++------------- internal/app/exporter/exporter.go | 17 +++++++------ 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 778af48..818c4cd 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,7 @@ Configuration could be defined in `.prometheus-rds-exporter.yaml` or environment | --- | --- | --- | | aws-assume-role-arn | AWS IAM ARN role to assume to fetch metrics | | | aws-assume-role-session | AWS assume role session name | prometheus-rds-exporter | +| collect-instances-metrics | Collect AWS instances metrics (AWS Cloudwatch API) | true | | collect-instance-types | Collect AWS instance types information (AWS EC2 API) | true | | collect-logs-size | Collect AWS instances logs size (AWS RDS API) | true | | collect-maintenances | Collect AWS instances maintenances (AWS RDS API) | true | diff --git a/cmd/root.go b/cmd/root.go index b7e2285..2b69252 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,17 +29,18 @@ const ( var cfgFile string type exporterConfig struct { - Debug bool `mapstructure:"debug"` - LogFormat string `mapstructure:"log-format"` - MetricPath string `mapstructure:"metrics-path"` - ListenAddress string `mapstructure:"listen-address"` - AWSAssumeRoleSession string `mapstructure:"aws-assume-role-session"` - AWSAssumeRoleArn string `mapstructure:"aws-assume-role-arn"` - CollectInstanceTypes bool `mapstructure:"collect-instances-types"` - CollectLogsSize bool `mapstructure:"collect-logs-size"` - CollectMaintenances bool `mapstructure:"collect-maintenances"` - CollectQuotas bool `mapstructure:"collect-quotas"` - CollectUsages bool `mapstructure:"collect-usages"` + Debug bool `mapstructure:"debug"` + LogFormat string `mapstructure:"log-format"` + MetricPath string `mapstructure:"metrics-path"` + ListenAddress string `mapstructure:"listen-address"` + AWSAssumeRoleSession string `mapstructure:"aws-assume-role-session"` + AWSAssumeRoleArn string `mapstructure:"aws-assume-role-arn"` + CollectInstanceMetrics bool `mapstructure:"collect-instance-metrics"` + CollectInstanceTypes bool `mapstructure:"collect-instance-types"` + CollectLogsSize bool `mapstructure:"collect-logs-size"` + CollectMaintenances bool `mapstructure:"collect-maintenances"` + CollectQuotas bool `mapstructure:"collect-quotas"` + CollectUsages bool `mapstructure:"collect-usages"` } func run(configuration exporterConfig) { @@ -67,11 +68,12 @@ func run(configuration exporterConfig) { servicequotasClient := servicequotas.NewFromConfig(cfg) collectorConfiguration := exporter.Configuration{ - CollectLogsSize: configuration.CollectLogsSize, - CollectMaintenances: configuration.CollectMaintenances, - CollectQuotas: configuration.CollectQuotas, - CollectUsages: configuration.CollectUsages, - CollectInstanceTypes: configuration.CollectInstanceTypes, + CollectInstanceMetrics: configuration.CollectInstanceMetrics, + CollectInstanceTypes: configuration.CollectInstanceTypes, + CollectLogsSize: configuration.CollectLogsSize, + CollectMaintenances: configuration.CollectMaintenances, + CollectQuotas: configuration.CollectQuotas, + CollectUsages: configuration.CollectUsages, } collector := exporter.NewCollector(*logger, collectorConfiguration, awsAccountID, awsRegion, rdsClient, ec2Client, cloudWatchClient, servicequotasClient) @@ -116,6 +118,7 @@ func NewRootCommand() (*cobra.Command, error) { cmd.Flags().StringP("aws-assume-role-arn", "", "", "AWS IAM ARN role to assume to fetch metrics") cmd.Flags().StringP("aws-assume-role-session", "", "prometheus-rds-exporter", "AWS assume role session name") cmd.Flags().BoolP("collect-instance-types", "", true, "Collect AWS instance types") + cmd.Flags().BoolP("collect-instance-metrics", "", true, "Collect AWS instance metrics") cmd.Flags().BoolP("collect-logs-size", "", true, "Collect AWS instances logs size") cmd.Flags().BoolP("collect-maintenances", "", true, "Collect AWS instances maintenances") cmd.Flags().BoolP("collect-quotas", "", true, "Collect AWS RDS quotas") @@ -151,6 +154,11 @@ func NewRootCommand() (*cobra.Command, error) { return cmd, fmt.Errorf("failed to bind 'aws-assume-role-session' parameter: %w", err) } + err = viper.BindPFlag("collect-instance-metrics", cmd.Flags().Lookup("collect-instance-metrics")) + if err != nil { + return cmd, fmt.Errorf("failed to bind 'collect-instance-metrics' parameter: %w", err) + } + err = viper.BindPFlag("collect-instance-types", cmd.Flags().Lookup("collect-instance-types")) if err != nil { return cmd, fmt.Errorf("failed to bind 'collect-instance-types' parameter: %w", err) diff --git a/internal/app/exporter/exporter.go b/internal/app/exporter/exporter.go index fa297fd..7cbc9c0 100644 --- a/internal/app/exporter/exporter.go +++ b/internal/app/exporter/exporter.go @@ -21,11 +21,12 @@ const ( ) type Configuration struct { - CollectQuotas bool - CollectUsages bool - CollectInstanceTypes bool - CollectLogsSize bool - CollectMaintenances bool + CollectInstanceMetrics bool + CollectInstanceTypes bool + CollectLogsSize bool + CollectMaintenances bool + CollectQuotas bool + CollectUsages bool } type counters struct { @@ -309,8 +310,10 @@ func (c *rdsCollector) fetchMetrics() error { } // Fetch Cloudwatch metrics for instances - go c.getCloudwatchMetrics(c.cloudWatchClient, instanceIdentifiers) - c.wg.Add(1) + if c.configuration.CollectInstanceMetrics { + go c.getCloudwatchMetrics(c.cloudWatchClient, instanceIdentifiers) + c.wg.Add(1) + } // Wait for all go routines to finish c.wg.Wait()