Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metricbeat] Add GCP CloudSQL region filter #32943

Merged
merged 14 commits into from
Sep 19, 2022
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]
*Metricbeat*

- Fix and improve AWS metric period calculation to avoid zero-length intervals {pull}32724[32724]
- Add GCP CloudSQL region filter {pull}32943[32943]

*Packetbeat*

Expand Down
3 changes: 2 additions & 1 deletion x-pack/metricbeat/module/gcp/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
ServiceStorage = "storage"
ServiceFirestore = "firestore"
ServiceDataproc = "dataproc"
ServiceCloudSQL = "cloudsql"
)

//Paths within the GCP monitoring.TimeSeries response, if converted to JSON, where you can find each ECS field required for the output event
Expand Down Expand Up @@ -76,7 +77,7 @@ const (
LabelMetadata = "metadata"
)

// Available perSeriesAligner map
// AlignersMapToGCP map contains available perSeriesAligner
// https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.alertPolicies#Aligner
var AlignersMapToGCP = map[string]monitoringpb.Aggregation_Aligner{
"ALIGN_NONE": monitoringpb.Aggregation_ALIGN_NONE,
Expand Down
7 changes: 6 additions & 1 deletion x-pack/metricbeat/module/gcp/metrics/metrics_requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func (r *metricsRequester) getFilterForMetric(serviceName, m string) string {
}

f = fmt.Sprintf(`%s AND resource.labels.location = "%s"`, f, r.config.Region)
case gcp.ServiceCloudSQL:
if r.config.Region != "" {
region := strings.TrimSuffix(r.config.Region, "*")
f = fmt.Sprintf(`%s AND resource.labels.region = starts_with("%s")`, f, region)
}
default:
if r.config.Region != "" && r.config.Zone != "" {
r.logger.Warnf("when region %s and zone %s config parameter "+
Expand All @@ -152,7 +157,7 @@ func (r *metricsRequester) getFilterForMetric(serviceName, m string) string {
// }

region := strings.TrimSuffix(r.config.Region, "*")
f = fmt.Sprintf(`%s AND resource.labels.zone = starts_with("%s")`, f, region)
f = fmt.Sprintf(`%s AND resource.labels.region = starts_with("%s")`, f, region)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not include this change here, as it's not related to CloudSQL. This change is also a breaking change, so we need to take another approach.
I would add a comment, so we can remove this in the future: FIXME: this is using zone but should use region. This is a breaking change.

} else if r.config.Zone != "" {
// zone := r.config.Zone
// if strings.HasSuffix(r.config.Zone, "*") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestGetFilterForMetric(t *testing.T) {
"compute",
"compute.googleapis.com/firewall/dropped_bytes_count",
metricsRequester{config: config{Region: "us-east1"}, logger: logger},
"metric.type=\"compute.googleapis.com/firewall/dropped_bytes_count\" AND resource.labels.zone = starts_with(\"us-east1\")",
"metric.type=\"compute.googleapis.com/firewall/dropped_bytes_count\" AND resource.labels.region = starts_with(\"us-east1\")",
},
{
"pubsub service with region in config",
Expand All @@ -70,14 +70,14 @@ func TestGetFilterForMetric(t *testing.T) {
"compute",
"compute.googleapis.com/firewall/dropped_bytes_count",
metricsRequester{config: config{Region: "us-central1", Zone: "us-central1-a"}, logger: logger},
"metric.type=\"compute.googleapis.com/firewall/dropped_bytes_count\" AND resource.labels.zone = starts_with(\"us-central1\")",
"metric.type=\"compute.googleapis.com/firewall/dropped_bytes_count\" AND resource.labels.region = starts_with(\"us-central1\")",
},
{
"compute uptime with partial region",
"compute",
"compute.googleapis.com/instance/uptime",
metricsRequester{config: config{Region: "us-west"}, logger: logger},
"metric.type=\"compute.googleapis.com/instance/uptime\" AND resource.labels.zone = starts_with(\"us-west\")",
"metric.type=\"compute.googleapis.com/instance/uptime\" AND resource.labels.region = starts_with(\"us-west\")",
},
{
"compute uptime with partial zone",
Expand All @@ -91,7 +91,7 @@ func TestGetFilterForMetric(t *testing.T) {
"compute",
"compute.googleapis.com/instance/uptime",
metricsRequester{config: config{Region: "us-*"}, logger: logger},
"metric.type=\"compute.googleapis.com/instance/uptime\" AND resource.labels.zone = starts_with(\"us-\")",
"metric.type=\"compute.googleapis.com/instance/uptime\" AND resource.labels.region = starts_with(\"us-\")",
},
{
"compute uptime with wildcard in zone",
Expand Down