Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]
- Fix and improve AWS metric period calculation to avoid zero-length intervals {pull}32724[32724]
- Add network name to data.json {pull}32986[32986]
- Add missing cluster metadata to k8s module metricsets {pull}32979[32979]
- GCP refactor getFilterForMetric ifelse to switch {pull}33047[33047]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This changeset does not require a changelog, as no functionality changes, so you could remove this.


*Packetbeat*

Expand Down
65 changes: 15 additions & 50 deletions x-pack/metricbeat/module/gcp/metrics/metrics_requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,12 @@ func (r *metricsRequester) getFilterForMetric(serviceName, m string) string {
r.logger.Warnf("when region %s and regions config parameters are both provided, use region", r.config.Region)
}

if r.config.Region != "" {
f = fmt.Sprintf(
"%s AND %s = starts_with(\"%s\")",
f,
gcp.ComputeResourceLabelZone,
strings.TrimSuffix(r.config.Region, "*"),
)
break
}

if r.config.Zone != "" {
f = fmt.Sprintf(
"%s AND %s = starts_with(\"%s\")",
f,
gcp.ComputeResourceLabelZone,
strings.TrimSuffix(r.config.Zone, "*"),
)
break
}

if len(r.config.Regions) != 0 {
switch {
case r.config.Region != "":
f = fmt.Sprintf("%s AND %s = starts_with(\"%s\")", f, gcp.ComputeResourceLabelZone, strings.TrimSuffix(r.config.Region, "*"))
case r.config.Zone != "":
f = fmt.Sprintf("%s AND %s = starts_with(\"%s\")", f, gcp.ComputeResourceLabelZone, strings.TrimSuffix(r.config.Zone, "*"))
case len(r.config.Regions) != 0:
regionsFilter := r.buildRegionsFilter(r.config.Regions, gcp.ComputeResourceLabelZone)
f = fmt.Sprintf("%s AND %s", f, regionsFilter)
}
Expand All @@ -180,27 +165,14 @@ func (r *metricsRequester) getFilterForMetric(serviceName, m string) string {
"both are provided, only use region", r.config.Region, r.config.Zone)
}

region := r.config.Region
if region != "" {
// if strings.HasSuffix(region, "*") {
// region = strings.TrimSuffix(region, "*")
// }
region = strings.TrimSuffix(region, "*")

switch {
case r.config.Region != "":
region := strings.TrimSuffix(r.config.Region, "*")
f = fmt.Sprintf("%s AND resource.label.location=starts_with(\"%s\")", f, region)
break
}
zone := r.config.Zone
if zone != "" {
// if strings.HasSuffix(zone, "*") {
// zone = strings.TrimSuffix(zone, "*")
// }
zone = strings.TrimSuffix(zone, "*")
case r.config.Zone != "":
zone := strings.TrimSuffix(r.config.Zone, "*")
f = fmt.Sprintf("%s AND resource.label.location=starts_with(\"%s\")", f, zone)
break
}

if len(r.config.Regions) != 0 {
case len(r.config.Regions) != 0:
regionsFilter := r.buildRegionsFilter(r.config.Regions, gcp.GKEResourceLabelLocation)
f = fmt.Sprintf("%s AND %s", f, regionsFilter)
}
Expand All @@ -223,19 +195,12 @@ func (r *metricsRequester) getFilterForMetric(serviceName, m string) string {
r.logger.Warnf("when region %s and zone %s config parameter "+
"both are provided, only use region", r.config.Region, r.config.Zone)
}
if r.config.Region != "" {
// region := r.config.Region
// if strings.HasSuffix(r.config.Region, "*") {
// region = strings.TrimSuffix(r.config.Region, "*")
// }

switch {
case r.config.Region != "":
region := strings.TrimSuffix(r.config.Region, "*")
f = fmt.Sprintf(`%s AND resource.labels.zone = starts_with("%s")`, f, region)
} else if r.config.Zone != "" {
// zone := r.config.Zone
// if strings.HasSuffix(r.config.Zone, "*") {
// zone = strings.TrimSuffix(r.config.Zone, "*")
// }
case r.config.Zone != "":
zone := strings.TrimSuffix(r.config.Zone, "*")
f = fmt.Sprintf(`%s AND resource.labels.zone = starts_with("%s")`, f, zone)
}
Expand Down