2020-04-27 16:25:45 +00:00
|
|
|
package ec2
|
|
|
|
|
|
|
|
import (
|
2021-06-25 09:10:20 +00:00
|
|
|
"flag"
|
2020-04-27 16:25:45 +00:00
|
|
|
"fmt"
|
2021-06-25 09:10:20 +00:00
|
|
|
"time"
|
2021-11-05 12:41:14 +00:00
|
|
|
|
2022-05-05 07:59:28 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/awsapi"
|
2021-11-05 12:41:14 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2022-11-30 05:22:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-04-27 16:25:45 +00:00
|
|
|
)
|
|
|
|
|
2021-06-25 09:10:20 +00:00
|
|
|
// SDCheckInterval defines interval for targets refresh.
|
|
|
|
var SDCheckInterval = flag.Duration("promscrape.ec2SDCheckInterval", time.Minute, "Interval for checking for changes in ec2. "+
|
|
|
|
"This works only if ec2_sd_configs is configured in '-promscrape.config' file. "+
|
2024-04-18 00:27:47 +00:00
|
|
|
"See https://docs.victoriametrics.com/sd_configs/#ec2_sd_configs for details")
|
2021-06-25 09:10:20 +00:00
|
|
|
|
2020-04-27 16:25:45 +00:00
|
|
|
// SDConfig represents service discovery config for ec2.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#ec2_sd_config
|
|
|
|
type SDConfig struct {
|
2022-08-05 15:50:00 +00:00
|
|
|
Region string `yaml:"region,omitempty"`
|
|
|
|
Endpoint string `yaml:"endpoint,omitempty"`
|
|
|
|
STSEndpoint string `yaml:"sts_endpoint,omitempty"`
|
|
|
|
AccessKey string `yaml:"access_key,omitempty"`
|
|
|
|
SecretKey *promauth.Secret `yaml:"secret_key,omitempty"`
|
2020-09-21 13:04:15 +00:00
|
|
|
// TODO add support for Profile, not working atm
|
2022-08-05 15:50:00 +00:00
|
|
|
// Profile string `yaml:"profile,omitempty"`
|
2020-11-13 14:17:03 +00:00
|
|
|
RoleARN string `yaml:"role_arn,omitempty"`
|
2020-04-27 16:25:45 +00:00
|
|
|
// RefreshInterval time.Duration `yaml:"refresh_interval"`
|
|
|
|
// refresh_interval is obtained from `-promscrape.ec2SDCheckInterval` command-line option.
|
2022-05-06 09:43:29 +00:00
|
|
|
Port *int `yaml:"port,omitempty"`
|
|
|
|
InstanceFilters []awsapi.Filter `yaml:"filters,omitempty"`
|
|
|
|
AZFilters []awsapi.Filter `yaml:"az_filters,omitempty"`
|
2020-04-27 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabels returns ec2 labels according to sdc.
|
2023-09-01 07:34:16 +00:00
|
|
|
func (sdc *SDConfig) GetLabels(_ string) ([]*promutils.Labels, error) {
|
2020-04-27 16:25:45 +00:00
|
|
|
cfg, err := getAPIConfig(sdc)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot get API config: %w", err)
|
2020-04-27 16:25:45 +00:00
|
|
|
}
|
|
|
|
ms, err := getInstancesLabels(cfg)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("error when fetching instances data from EC2: %w", err)
|
2020-04-27 16:25:45 +00:00
|
|
|
}
|
|
|
|
return ms, nil
|
|
|
|
}
|
2021-03-01 12:13:56 +00:00
|
|
|
|
|
|
|
// MustStop stops further usage for sdc.
|
|
|
|
func (sdc *SDConfig) MustStop() {
|
|
|
|
configMap.Delete(sdc)
|
|
|
|
}
|