From c54e14cdec445c6ea18fba79c7dc2368b7671b68 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 30 Sep 2022 20:48:29 +0300 Subject: [PATCH] lib/promscrape/discovery/ec2: expose __meta_ec2_region label in the same way as Prometheus 2.39 does See https://github.com/prometheus/prometheus/pull/11326 --- docs/CHANGELOG.md | 1 + docs/sd_configs.md | 1 + lib/awsapi/config.go | 9 +++++---- lib/promscrape/discovery/ec2/instance.go | 6 ++++-- lib/promscrape/discovery/ec2/instance_test.go | 3 ++- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d26dc52ed..f586335cd 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -43,6 +43,7 @@ See [these docs](https://docs.victoriametrics.com/Cluster-VictoriaMetrics.html#m * FEATURE: [MetricsQL](https://docs.victoriametrics.com/MetricsQL.html): add [sort_by_label_numeric](https://docs.victoriametrics.com/MetricsQL.html#sort_by_label_numeric) and [sort_by_label_numeric_desc](https://docs.victoriametrics.com/MetricsQL.html#sort_by_label_numeric_desc) functions for [numeric sort](https://www.gnu.org/software/coreutils/manual/html_node/Version-sort-is-not-the-same-as-numeric-sort.html) of input time series by the specified labels. See [this feature request](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2938). * FEATURE: [vmbackup](https://docs.victoriametrics.com/vmbackup.html) and [vmrestore](https://docs.victoriametrics.com/vmrestore.html): retry GCS operations for up to 3 minutes on temporary failures. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3147). * FEATURE: [vmgateway](https://docs.victoriametrics.com/vmgateway.html): add ability to extract JWT authorization token from non-standard HTTP header by passing it via `-auth.httpHeader` command-line flag. See [this feature request](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3054). +* FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): expose `__meta_ec2_region` label for [ec2_sd_config](https://docs.victoriametrics.com/sd_configs.html#ec2_sd_configs) in the same way as [Prometheus 2.39 does](https://github.com/prometheus/prometheus/pull/11326). * BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): properly encode query params for aws signed requests, use `%20` instead of `+` as api requires. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3171). * BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): properly parse relabel config when regex ending with escaped `$`. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3131). diff --git a/docs/sd_configs.md b/docs/sd_configs.md index c834deda0..c9f4a8e32 100644 --- a/docs/sd_configs.md +++ b/docs/sd_configs.md @@ -473,6 +473,7 @@ The following meta labels are available on discovered targets during [relabeling * `__meta_ec2_private_ip`: the private IP address of the instance, if present * `__meta_ec2_public_dns_name`: the public DNS name of the instance, if available * `__meta_ec2_public_ip`: the public IP address of the instance, if available +* `__meta_ec2_region`: EC2 region for the discovered instance * `__meta_ec2_subnet_id`: comma separated list of subnets IDs in which the instance is running, if available * `__meta_ec2_tag_`: each tag value of the instance * `__meta_ec2_vpc_id`: the ID of the VPC in which the instance is running, if available diff --git a/lib/awsapi/config.go b/lib/awsapi/config.go index 706f87526..fb97be0af 100644 --- a/lib/awsapi/config.go +++ b/lib/awsapi/config.go @@ -53,11 +53,9 @@ func NewConfig(ec2Endpoint, stsEndpoint, region, roleARN, accessKey, secretKey, defaultAccessKey: os.Getenv("AWS_ACCESS_KEY_ID"), defaultSecretKey: os.Getenv("AWS_SECRET_ACCESS_KEY"), } - cfg.service = service if cfg.service == "" { cfg.service = "aps" } - cfg.region = region if cfg.region == "" { r, err := getDefaultRegion(cfg.client) if err != nil { @@ -75,8 +73,6 @@ func NewConfig(ec2Endpoint, stsEndpoint, region, roleARN, accessKey, secretKey, return nil, fmt.Errorf("roleARN is missing for AWS_WEB_IDENTITY_TOKEN_FILE=%q; set it via env var AWS_ROLE_ARN", cfg.webTokenPath) } // explicitly set credentials has priority over env variables - cfg.defaultAccessKey = os.Getenv("AWS_ACCESS_KEY_ID") - cfg.defaultSecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY") if len(accessKey) > 0 { cfg.defaultAccessKey = accessKey } @@ -90,6 +86,11 @@ func NewConfig(ec2Endpoint, stsEndpoint, region, roleARN, accessKey, secretKey, return cfg, nil } +// GetRegion returns region for the given cfg. +func (cfg *Config) GetRegion() string { + return cfg.region +} + // GetEC2APIResponse performs EC2 API request with ghe given action. // // filtersQueryString must contain an optional percent-encoded query string for aws filters. diff --git a/lib/promscrape/discovery/ec2/instance.go b/lib/promscrape/discovery/ec2/instance.go index 9cacb2506..e00f1fb6e 100644 --- a/lib/promscrape/discovery/ec2/instance.go +++ b/lib/promscrape/discovery/ec2/instance.go @@ -16,10 +16,11 @@ func getInstancesLabels(cfg *apiConfig) ([]map[string]string, error) { return nil, err } azMap := getAZMap(cfg) + region := cfg.awsConfig.GetRegion() var ms []map[string]string for _, r := range rs { for _, inst := range r.InstanceSet.Items { - ms = inst.appendTargetLabels(ms, r.OwnerID, cfg.port, azMap) + ms = inst.appendTargetLabels(ms, r.OwnerID, region, cfg.port, azMap) } } return ms, nil @@ -134,7 +135,7 @@ func parseInstancesResponse(data []byte) (*InstancesResponse, error) { return &v, nil } -func (inst *Instance) appendTargetLabels(ms []map[string]string, ownerID string, port int, azMap map[string]string) []map[string]string { +func (inst *Instance) appendTargetLabels(ms []map[string]string, ownerID, region string, port int, azMap map[string]string) []map[string]string { if len(inst.PrivateIPAddress) == 0 { // Cannot scrape instance without private IP address return ms @@ -157,6 +158,7 @@ func (inst *Instance) appendTargetLabels(ms []map[string]string, ownerID string, "__meta_ec2_private_ip": inst.PrivateIPAddress, "__meta_ec2_public_dns_name": inst.PublicDNSName, "__meta_ec2_public_ip": inst.PublicIPAddress, + "__meta_ec2_region": region, "__meta_ec2_vpc_id": inst.VPCID, } if len(inst.VPCID) > 0 { diff --git a/lib/promscrape/discovery/ec2/instance_test.go b/lib/promscrape/discovery/ec2/instance_test.go index a8441a96c..b6667db89 100644 --- a/lib/promscrape/discovery/ec2/instance_test.go +++ b/lib/promscrape/discovery/ec2/instance_test.go @@ -238,7 +238,7 @@ func TestParseInstancesResponse(t *testing.T) { ownerID := rs.OwnerID port := 423 inst := rs.InstanceSet.Items[0] - labelss := inst.appendTargetLabels(nil, ownerID, port, map[string]string{ + labelss := inst.appendTargetLabels(nil, ownerID, "region-a", port, map[string]string{ "eu-west-2c": "foobar-zone", }) var sortedLabelss [][]prompbmarshal.Label @@ -263,6 +263,7 @@ func TestParseInstancesResponse(t *testing.T) { "__meta_ec2_private_ip": "172.31.11.152", "__meta_ec2_public_dns_name": "ec2-3-8-232-141.eu-west-2.compute.amazonaws.com", "__meta_ec2_public_ip": "3.8.232.141", + "__meta_ec2_region": "region-a", "__meta_ec2_subnet_id": ",subnet-57044c3e,", "__meta_ec2_tag_foo": "bar", "__meta_ec2_vpc_id": "vpc-f1eaad99",