Skip to content

Commit

Permalink
[Metricbeat] Remove checking region/zone and service name from google…
Browse files Browse the repository at this point in the history
…cloud (#18398)

* remove validate region/zone

* add stackdriver into googlecloud default config
  • Loading branch information
kaiyan-sheng committed May 11, 2020
1 parent 24eb767 commit 2d7c9d5
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 27 deletions.
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 @@ -235,6 +236,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 @@ -523,6 +523,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 @@ -24,3 +24,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 @@ -27,3 +27,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 2d7c9d5

Please sign in to comment.