Skip to content

Commit

Permalink
Cherry-pick #18398 to 7.x: [Metricbeat] Remove checking region/zone a…
Browse files Browse the repository at this point in the history
…nd service name from googlecloud (#18433)

* [Metricbeat] Remove checking region/zone and service name from googlecloud (#18398)

* remove validate region/zone

* add stackdriver into googlecloud default config

(cherry picked from commit 2d7c9d5)

* add changelog
  • Loading branch information
kaiyan-sheng authored May 12, 2020
1 parent 6d0b831 commit 2d7f1a9
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix storage metricset to allow config without region/zone. {issue}17623[17623] {pull}17624[17624]
- Fix overflow on Prometheus rates when new buckets are added on the go. {pull}17753[17753]
- Add a switch to the driver definition on SQL module to use pretty names {pull}17378[17378]
- Remove required for region/zone and make stackdriver a metricset in googlecloud. {issue}16785[16785] {pull}18398[18398]

*Packetbeat*

Expand Down
20 changes: 19 additions & 1 deletion metricbeat/docs/modules/googlecloud.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ a partial region name like `us-east` or `us-east*`, which will monitor all regio
`us-east`: `us-east1` and `us-east4`. If both region and zone are configured,
only region will be used.
Please see https://cloud.google.com/compute/docs/regions-zones#available[GCP regions]
for regions that are available in GCP.
for regions that are available in GCP. If both `region` and `zone` are not
specified, metrics will be collected from all regions/zones.

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

Expand Down Expand Up @@ -236,6 +237,23 @@ metricbeat.modules:
credentials_file_path: "your JSON credentials file path"
exclude_labels: false
period: 5m
- module: googlecloud
metricsets:
- stackdriver
project_id: "your project id"
credentials_file_path: "your JSON credentials file path"
exclude_labels: false
period: 1m
stackdriver:
service: compute
metrics:
- aligner: ALIGN_NONE
metric_types:
- "compute.googleapis.com/instance/cpu/reserved_cores"
- "compute.googleapis.com/instance/cpu/usage_time"
- "compute.googleapis.com/instance/cpu/utilization"
- "compute.googleapis.com/instance/uptime"
----

[float]
Expand Down
17 changes: 17 additions & 0 deletions x-pack/metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,23 @@ metricbeat.modules:
exclude_labels: false
period: 5m

- module: googlecloud
metricsets:
- stackdriver
project_id: "your project id"
credentials_file_path: "your JSON credentials file path"
exclude_labels: false
period: 1m
stackdriver:
service: compute
metrics:
- aligner: ALIGN_NONE
metric_types:
- "compute.googleapis.com/instance/cpu/reserved_cores"
- "compute.googleapis.com/instance/cpu/usage_time"
- "compute.googleapis.com/instance/cpu/utilization"
- "compute.googleapis.com/instance/uptime"

#------------------------------- Graphite Module -------------------------------
- module: graphite
metricsets: ["server"]
Expand Down
17 changes: 17 additions & 0 deletions x-pack/metricbeat/module/googlecloud/_meta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@
credentials_file_path: "your JSON credentials file path"
exclude_labels: false
period: 5m

- module: googlecloud
metricsets:
- stackdriver
project_id: "your project id"
credentials_file_path: "your JSON credentials file path"
exclude_labels: false
period: 1m
stackdriver:
service: compute
metrics:
- aligner: ALIGN_NONE
metric_types:
- "compute.googleapis.com/instance/cpu/reserved_cores"
- "compute.googleapis.com/instance/cpu/usage_time"
- "compute.googleapis.com/instance/cpu/utilization"
- "compute.googleapis.com/instance/uptime"
3 changes: 2 additions & 1 deletion x-pack/metricbeat/module/googlecloud/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ a partial region name like `us-east` or `us-east*`, which will monitor all regio
`us-east`: `us-east1` and `us-east4`. If both region and zone are configured,
only region will be used.
Please see https://cloud.google.com/compute/docs/regions-zones#available[GCP regions]
for regions that are available in GCP.
for regions that are available in GCP. If both `region` and `zone` are not
specified, metrics will be collected from all regions/zones.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package stackdriver

import (
"github.com/pkg/errors"

"github.com/elastic/beats/v7/x-pack/metricbeat/module/googlecloud"
"github.com/elastic/beats/v7/x-pack/metricbeat/module/googlecloud/stackdriver/compute"
)
Expand All @@ -17,9 +15,7 @@ func NewMetadataServiceForConfig(c config) (googlecloud.MetadataService, error)
switch c.ServiceName {
case googlecloud.ServiceCompute:
return compute.NewMetadataService(c.ProjectID, c.Zone, c.Region, c.opt...)
case googlecloud.ServicePubsub, googlecloud.ServiceLoadBalancing, googlecloud.ServiceStorage:
return nil, nil
default:
return nil, errors.Errorf("service '%s' not supported", c.ServiceName)
return nil, nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ var serviceRegexp = regexp.MustCompile(`^(?P<service>[a-z]+)\.googleapis.com.*`)
// if they have a region specified.
func (r *stackdriverMetricsRequester) getFilterForMetric(m string) (f string) {
f = fmt.Sprintf(`metric.type="%s"`, m)
if r.config.Zone == "" && r.config.Region == "" {
return
}

service := serviceRegexp.ReplaceAllString(m, "${service}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func TestGetFilterForMetric(t *testing.T) {
stackdriverMetricsRequester{config: config{Zone: "us-west1-*"}, logger: logger},
"metric.type=\"compute.googleapis.com/instance/uptime\" AND resource.labels.zone = starts_with(\"us-west1-\")",
},
{
"compute service with no region/zone in config",
"compute.googleapis.com/firewall/dropped_bytes_count",
stackdriverMetricsRequester{config: config{}},
"metric.type=\"compute.googleapis.com/firewall/dropped_bytes_count\"",
},
}

for _, c := range cases {
Expand Down
40 changes: 20 additions & 20 deletions x-pack/metricbeat/module/googlecloud/stackdriver/metricset.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return nil, errors.Wrap(err, "error creating Stackdriver client")
}

m.metricsMeta, err = metricDescriptor(ctx, client, m.config.ProjectID, m.stackDriverConfig)
m.metricsMeta, err = m.metricDescriptor(ctx, client)
if err != nil {
return nil, errors.Wrap(err, "error calling metricDescriptor function")
}
Expand Down Expand Up @@ -188,19 +188,6 @@ func validatePeriodForGCP(d time.Duration) (err error) {
return nil
}

// Validate googlecloud module config
func (c *config) Validate() error {
// storage metricset does not require region or zone config parameter.
if c.ServiceName == "storage" {
return nil
}

if c.Region == "" && c.Zone == "" {
return errors.New("region and zone in Google Cloud config file cannot both be empty")
}
return nil
}

// Validate stackdriver related config
func (mc *stackDriverConfig) Validate() error {
gcpAlignerNames := make([]string, 0)
Expand All @@ -218,13 +205,13 @@ func (mc *stackDriverConfig) Validate() error {

// metricDescriptor calls ListMetricDescriptorsRequest API to get metric metadata
// (sample period and ingest delay) of each given metric type
func metricDescriptor(ctx context.Context, client *monitoring.MetricClient, projectID string, stackDriverConfigs []stackDriverConfig) (map[string]metricMeta, error) {
func (m *MetricSet) metricDescriptor(ctx context.Context, client *monitoring.MetricClient) (map[string]metricMeta, error) {
metricsWithMeta := make(map[string]metricMeta, 0)

for _, sdc := range stackDriverConfigs {
for _, sdc := range m.stackDriverConfig {
for _, mt := range sdc.MetricTypes {
req := &monitoringpb.ListMetricDescriptorsRequest{
Name: "projects/" + projectID,
Name: "projects/" + m.config.ProjectID,
Filter: fmt.Sprintf(`metric.type = "%s"`, mt),
}

Expand All @@ -234,10 +221,23 @@ func metricDescriptor(ctx context.Context, client *monitoring.MetricClient, proj
return metricsWithMeta, errors.Errorf("Could not make ListMetricDescriptors request: %s: %v", mt, err)
}

metricsWithMeta[mt] = metricMeta{
samplePeriod: time.Duration(out.Metadata.SamplePeriod.Seconds) * time.Second,
ingestDelay: time.Duration(out.Metadata.IngestDelay.Seconds) * time.Second,
// Set samplePeriod default to 60 seconds and ingestDelay default to 0.
meta := metricMeta{
samplePeriod: 60 * time.Second,
ingestDelay: 0 * time.Second,
}

if out.Metadata.SamplePeriod != nil {
m.Logger().Debugf("For metric type %s: sample period = %s", mt, out.Metadata.SamplePeriod)
meta.samplePeriod = time.Duration(out.Metadata.SamplePeriod.Seconds) * time.Second
}

if out.Metadata.IngestDelay != nil {
m.Logger().Debugf("For metric type %s: ingest delay = %s", mt, out.Metadata.IngestDelay)
meta.ingestDelay = time.Duration(out.Metadata.IngestDelay.Seconds) * time.Second
}

metricsWithMeta[mt] = meta
}
}

Expand Down
17 changes: 17 additions & 0 deletions x-pack/metricbeat/modules.d/googlecloud.yml.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,20 @@
credentials_file_path: "your JSON credentials file path"
exclude_labels: false
period: 5m

- module: googlecloud
metricsets:
- stackdriver
project_id: "your project id"
credentials_file_path: "your JSON credentials file path"
exclude_labels: false
period: 1m
stackdriver:
service: compute
metrics:
- aligner: ALIGN_NONE
metric_types:
- "compute.googleapis.com/instance/cpu/reserved_cores"
- "compute.googleapis.com/instance/cpu/usage_time"
- "compute.googleapis.com/instance/cpu/utilization"
- "compute.googleapis.com/instance/uptime"

0 comments on commit 2d7f1a9

Please sign in to comment.