Skip to content

Commit 1eb3032

Browse files
author
kaiyan-sheng
authored
[Metricbeat] allow partial region and zone in googlecloud module config (#17913)
* allow partial region and zone in googlecloud module config * add wildcard support in region and zone
1 parent cc6c4e3 commit 1eb3032

8 files changed

Lines changed: 206 additions & 55 deletions

File tree

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
348348
- Reference kubernetes manifests mount data directory from the host when running metricbeat as daemonset, so data persist between executions in the same node. {pull}17429[17429]
349349
- Add more detailed error messages, system tests and small refactoring to the service metricset in windows. {pull}17725[17725]
350350
- Stack Monitoring modules now auto-configure required metricsets when `xpack.enabled: true` is set. {issue}16471[[16471] {pull}17609[17609]
351+
- Allow partial region and zone name in googlecloud module config. {pull}17913[17913]
351352
- Add aggregation aligner as a config parameter for googlecloud stackdriver metricset. {issue}17141[[17141] {pull}17719[17719]
352353
- Move the perfmon metricset to GA. {issue}16608[16608] {pull}17879[17879]
353354

metricbeat/docs/modules/googlecloud.asciidoc

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,20 @@ Note: extra GCP charges on Stackdriver Monitoring API requests will be generated
1616
== Module config and parameters
1717
This is a list of the possible module parameters you can tune:
1818

19-
* *zone*: A single string with the zone you want to monitor like "us-central1-a". If you need to fetch from multiple regions, you have to setup a different configuration for each (but you don't need a new instance of Metricbeat running)
20-
21-
* *region*: A single string with the region you want to monitor like "us-central1". This will enable monitoring for all zones under this region.
19+
* *zone*: A single string with the zone you want to monitor like `us-central1-a`.
20+
Or you can specific a partial zone name like `us-central1-`, which will monitor
21+
all zones start with `us-central1-`: `us-central1-a`, `us-central1-b`,
22+
`us-central1-c` and `us-central1-f`.
23+
Please see https://cloud.google.com/compute/docs/regions-zones#available[GCP zones]
24+
for zones that are available in GCP.
25+
26+
* *region*: A single string with the region you want to monitor like `us-central1`.
27+
This will enable monitoring for all zones under this region. Or you can specific
28+
a partial region name like `us-east`, which will monitor all regions start with
29+
`us-east`: `us-east1` and `us-east4`. If both region and zone are configured,
30+
only region will be used.
31+
Please see https://cloud.google.com/compute/docs/regions-zones#available[GCP regions]
32+
for regions that are available in GCP.
2233

2334
* *project_id*: A single string with your GCP Project ID
2435

@@ -28,6 +39,56 @@ This is a list of the possible module parameters you can tune:
2839

2940
* *period*: A single time duration specified for this module collection frequency.
3041

42+
[float]
43+
== Example configuration
44+
* `compute` metricset is enabled to collect metrics from `us-central1-a` zone
45+
in `elastic-observability` project.
46+
+
47+
[source,yaml]
48+
----
49+
- module: googlecloud
50+
metricsets:
51+
- compute
52+
zone: "us-central1-a"
53+
project_id: "elastic-observability"
54+
credentials_file_path: "your JSON credentials file path"
55+
exclude_labels: false
56+
period: 60s
57+
----
58+
59+
* `compute` and `pubsub` metricsets are enabled to collect metrics from all zones
60+
under `us-central1` region in `elastic-observability` project.
61+
+
62+
[source,yaml]
63+
----
64+
- module: googlecloud
65+
metricsets:
66+
- compute
67+
- pubsub
68+
region: "us-central1"
69+
project_id: "elastic-observability"
70+
credentials_file_path: "your JSON credentials file path"
71+
exclude_labels: false
72+
period: 60s
73+
----
74+
75+
* `compute` metricset is enabled to collect metrics from all regions starts with
76+
`us-west` in `elastic-observability` project, which includes all zones under
77+
`us-west1`, `us-west2`, `us-west3` and `us-west4`.
78+
+
79+
[source,yaml]
80+
----
81+
- module: googlecloud
82+
metricsets:
83+
- compute
84+
- pubsub
85+
region: "us-west"
86+
project_id: "elastic-observability"
87+
credentials_file_path: "your JSON credentials file path"
88+
exclude_labels: false
89+
period: 60s
90+
----
91+
3192
[float]
3293
== Authentication, authorization and permissions.
3394
Authentication and authorization in Google Cloud Platform can be achieved in many ways. For the current version of the Google Cloud Platform module for Metricbeat, the only supported method is using Service Account JSON files. A typical JSON with a private key looks like this:
@@ -133,6 +194,15 @@ metricbeat.modules:
133194
credentials_file_path: "your JSON credentials file path"
134195
exclude_labels: false
135196
period: 300s
197+
198+
- module: googlecloud
199+
metricsets:
200+
- compute
201+
region: "us-"
202+
project_id: "your project id"
203+
credentials_file_path: "your JSON credentials file path"
204+
exclude_labels: false
205+
period: 60s
136206
----
137207

138208
[float]

x-pack/metricbeat/metricbeat.reference.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,15 @@ metricbeat.modules:
523523
exclude_labels: false
524524
period: 300s
525525

526+
- module: googlecloud
527+
metricsets:
528+
- compute
529+
region: "us-"
530+
project_id: "your project id"
531+
credentials_file_path: "your JSON credentials file path"
532+
exclude_labels: false
533+
period: 60s
534+
526535
#------------------------------- Graphite Module -------------------------------
527536
- module: graphite
528537
metricsets: ["server"]

x-pack/metricbeat/module/googlecloud/_meta/config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@
2525
credentials_file_path: "your JSON credentials file path"
2626
exclude_labels: false
2727
period: 300s
28+
29+
- module: googlecloud
30+
metricsets:
31+
- compute
32+
region: "us-"
33+
project_id: "your project id"
34+
credentials_file_path: "your JSON credentials file path"
35+
exclude_labels: false
36+
period: 60s

x-pack/metricbeat/module/googlecloud/_meta/docs.asciidoc

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,82 @@ Note: extra GCP charges on Stackdriver Monitoring API requests will be generated
66
== Module config and parameters
77
This is a list of the possible module parameters you can tune:
88

9-
* *zone*: A single string with the zone you want to monitor like "us-central1-a". If you need to fetch from multiple regions, you have to setup a different configuration for each (but you don't need a new instance of Metricbeat running)
10-
11-
* *region*: A single string with the region you want to monitor like "us-central1". This will enable monitoring for all zones under this region.
9+
* *zone*: A single string with the zone you want to monitor like `us-central1-a`.
10+
Or you can specific a partial zone name like `us-central1-` or `us-central1-*`,
11+
which will monitor all zones start with `us-central1-`: `us-central1-a`,
12+
`us-central1-b`, `us-central1-c` and `us-central1-f`.
13+
Please see https://cloud.google.com/compute/docs/regions-zones#available[GCP zones]
14+
for zones that are available in GCP.
15+
16+
* *region*: A single string with the region you want to monitor like `us-central1`.
17+
This will enable monitoring for all zones under this region. Or you can specific
18+
a partial region name like `us-east` or `us-east*`, which will monitor all regions start with
19+
`us-east`: `us-east1` and `us-east4`. If both region and zone are configured,
20+
only region will be used.
21+
Please see https://cloud.google.com/compute/docs/regions-zones#available[GCP regions]
22+
for regions that are available in GCP.
1223

1324
* *project_id*: A single string with your GCP Project ID
1425

15-
* *credentials_file_path*: A single string pointing to the JSON file path reachable by Metricbeat that you have created using IAM.
26+
* *credentials_file_path*: A single string pointing to the JSON file path
27+
reachable by Metricbeat that you have created using IAM.
1628

17-
* *exclude_labels*: (`true`/`false` default `false`) Do not extract extra labels and metadata information from Metricsets and fetch metrics onlly. At the moment, *labels and metadata extraction is only supported* in Compute Metricset.
29+
* *exclude_labels*: (`true`/`false` default `false`) Do not extract extra labels
30+
and metadata information from metricsets and fetch metrics only. At the moment,
31+
*labels and metadata extraction is only supported* in `compute` metricset.
1832

1933
* *period*: A single time duration specified for this module collection frequency.
2034

35+
[float]
36+
== Example configuration
37+
* `compute` metricset is enabled to collect metrics from `us-central1-a` zone
38+
in `elastic-observability` project.
39+
+
40+
[source,yaml]
41+
----
42+
- module: googlecloud
43+
metricsets:
44+
- compute
45+
zone: "us-central1-a"
46+
project_id: "elastic-observability"
47+
credentials_file_path: "your JSON credentials file path"
48+
exclude_labels: false
49+
period: 60s
50+
----
51+
52+
* `compute` and `pubsub` metricsets are enabled to collect metrics from all zones
53+
under `us-central1` region in `elastic-observability` project.
54+
+
55+
[source,yaml]
56+
----
57+
- module: googlecloud
58+
metricsets:
59+
- compute
60+
- pubsub
61+
region: "us-central1"
62+
project_id: "elastic-observability"
63+
credentials_file_path: "your JSON credentials file path"
64+
exclude_labels: false
65+
period: 60s
66+
----
67+
68+
* `compute` metricset is enabled to collect metrics from all regions starts with
69+
`us-west` in `elastic-observability` project, which includes all zones under
70+
`us-west1`, `us-west2`, `us-west3` and `us-west4`.
71+
+
72+
[source,yaml]
73+
----
74+
- module: googlecloud
75+
metricsets:
76+
- compute
77+
- pubsub
78+
region: "us-west"
79+
project_id: "elastic-observability"
80+
credentials_file_path: "your JSON credentials file path"
81+
exclude_labels: false
82+
period: 60s
83+
----
84+
2185
[float]
2286
== Authentication, authorization and permissions.
2387
Authentication and authorization in Google Cloud Platform can be achieved in many ways. For the current version of the Google Cloud Platform module for Metricbeat, the only supported method is using Service Account JSON files. A typical JSON with a private key looks like this:

x-pack/metricbeat/module/googlecloud/stackdriver/metrics_requester.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"fmt"
1010
"regexp"
11+
"strings"
1112
"sync"
1213
"time"
1314

@@ -69,18 +70,6 @@ func (r *stackdriverMetricsRequester) Metric(ctx context.Context, metricType str
6970
return
7071
}
7172

72-
func constructFilter(m string, region string, zone string) string {
73-
filter := fmt.Sprintf(`metric.type="%s" AND resource.labels.zone = `, m)
74-
// If region is specified, use region as filter resource label.
75-
// If region is empty but zone is given, use zone instead.
76-
if region != "" {
77-
filter += fmt.Sprintf(`starts_with("%s")`, region)
78-
} else if zone != "" {
79-
filter += fmt.Sprintf(`"%s"`, zone)
80-
}
81-
return filter
82-
}
83-
8473
func (r *stackdriverMetricsRequester) Metrics(ctx context.Context, stackDriverConfigs []stackDriverConfig, metricsMeta map[string]metricMeta) ([]timeSeriesWithAligner, error) {
8574
var lock sync.Mutex
8675
var wg sync.WaitGroup
@@ -134,9 +123,17 @@ func (r *stackdriverMetricsRequester) getFilterForMetric(m string) (f string) {
134123
"both are provided, only use region", r.config.Region, r.config.Zone)
135124
}
136125
if r.config.Region != "" {
137-
f = fmt.Sprintf(`%s AND resource.labels.zone = starts_with("%s")`, f, r.config.Region)
126+
region := r.config.Region
127+
if strings.HasSuffix(r.config.Region, "*") {
128+
region = strings.TrimSuffix(r.config.Region, "*")
129+
}
130+
f = fmt.Sprintf(`%s AND resource.labels.zone = starts_with("%s")`, f, region)
138131
} else if r.config.Zone != "" {
139-
f = fmt.Sprintf(`%s AND resource.labels.zone = "%s"`, f, r.config.Zone)
132+
zone := r.config.Zone
133+
if strings.HasSuffix(r.config.Zone, "*") {
134+
zone = strings.TrimSuffix(r.config.Zone, "*")
135+
}
136+
f = fmt.Sprintf(`%s AND resource.labels.zone = starts_with("%s")`, f, zone)
140137
}
141138
}
142139
return

x-pack/metricbeat/module/googlecloud/stackdriver/metrics_requester_test.go

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,6 @@ import (
1414
"github.com/elastic/beats/v7/libbeat/logp"
1515
)
1616

17-
func TestStringInSlice(t *testing.T) {
18-
cases := []struct {
19-
title string
20-
m string
21-
region string
22-
zone string
23-
expectedFilter string
24-
}{
25-
{
26-
"construct filter with zone",
27-
"compute.googleapis.com/instance/cpu/utilization",
28-
"",
29-
"us-east1-b",
30-
"metric.type=\"compute.googleapis.com/instance/cpu/utilization\" AND resource.labels.zone = \"us-east1-b\"",
31-
},
32-
{
33-
"construct filter with region",
34-
"compute.googleapis.com/instance/cpu/utilization",
35-
"us-east1",
36-
"",
37-
"metric.type=\"compute.googleapis.com/instance/cpu/utilization\" AND resource.labels.zone = starts_with(\"us-east1\")",
38-
},
39-
}
40-
41-
for _, c := range cases {
42-
t.Run(c.title, func(t *testing.T) {
43-
filter := constructFilter(c.m, c.region, c.zone)
44-
assert.Equal(t, c.expectedFilter, filter)
45-
})
46-
}
47-
}
48-
4917
func TestGetFilterForMetric(t *testing.T) {
5018
var logger = logp.NewLogger("test")
5119
cases := []struct {
@@ -58,7 +26,7 @@ func TestGetFilterForMetric(t *testing.T) {
5826
"compute service with zone in config",
5927
"compute.googleapis.com/firewall/dropped_bytes_count",
6028
stackdriverMetricsRequester{config: config{Zone: "us-central1-a"}},
61-
"metric.type=\"compute.googleapis.com/firewall/dropped_bytes_count\" AND resource.labels.zone = \"us-central1-a\"",
29+
"metric.type=\"compute.googleapis.com/firewall/dropped_bytes_count\" AND resource.labels.zone = starts_with(\"us-central1-a\")",
6230
},
6331
{
6432
"pubsub service with zone in config",
@@ -96,6 +64,30 @@ func TestGetFilterForMetric(t *testing.T) {
9664
stackdriverMetricsRequester{config: config{Region: "us-central1", Zone: "us-central1-a"}, logger: logger},
9765
"metric.type=\"compute.googleapis.com/firewall/dropped_bytes_count\" AND resource.labels.zone = starts_with(\"us-central1\")",
9866
},
67+
{
68+
"compute uptime with partial region",
69+
"compute.googleapis.com/instance/uptime",
70+
stackdriverMetricsRequester{config: config{Region: "us-west"}, logger: logger},
71+
"metric.type=\"compute.googleapis.com/instance/uptime\" AND resource.labels.zone = starts_with(\"us-west\")",
72+
},
73+
{
74+
"compute uptime with partial zone",
75+
"compute.googleapis.com/instance/uptime",
76+
stackdriverMetricsRequester{config: config{Zone: "us-west1-"}, logger: logger},
77+
"metric.type=\"compute.googleapis.com/instance/uptime\" AND resource.labels.zone = starts_with(\"us-west1-\")",
78+
},
79+
{
80+
"compute uptime with wildcard in region",
81+
"compute.googleapis.com/instance/uptime",
82+
stackdriverMetricsRequester{config: config{Region: "us-*"}, logger: logger},
83+
"metric.type=\"compute.googleapis.com/instance/uptime\" AND resource.labels.zone = starts_with(\"us-\")",
84+
},
85+
{
86+
"compute uptime with wildcard in zone",
87+
"compute.googleapis.com/instance/uptime",
88+
stackdriverMetricsRequester{config: config{Zone: "us-west1-*"}, logger: logger},
89+
"metric.type=\"compute.googleapis.com/instance/uptime\" AND resource.labels.zone = starts_with(\"us-west1-\")",
90+
},
9991
}
10092

10193
for _, c := range cases {

x-pack/metricbeat/modules.d/googlecloud.yml.disabled

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@
2828
credentials_file_path: "your JSON credentials file path"
2929
exclude_labels: false
3030
period: 300s
31+
32+
- module: googlecloud
33+
metricsets:
34+
- compute
35+
region: "us-"
36+
project_id: "your project id"
37+
credentials_file_path: "your JSON credentials file path"
38+
exclude_labels: false
39+
period: 60s

0 commit comments

Comments
 (0)