Skip to content

Commit

Permalink
feat(exporter): Add --collect-instance-metrics parameter to enable/di…
Browse files Browse the repository at this point in the history
…sable AWS instance metrics
  • Loading branch information
vmercierfr committed Oct 23, 2023
1 parent a0b89bd commit a5a8da9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
40 changes: 24 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 10 additions & 7 deletions internal/app/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a5a8da9

Please sign in to comment.