Skip to content

Commit f4c65f3

Browse files
authored
[Metricbeat] Fix getting compute instance metadata with partial zone/region config (#18757)
* use zone for getting instance metadata
1 parent f6bd8c6 commit f4c65f3

File tree

4 files changed

+100
-84
lines changed

4 files changed

+100
-84
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ field. You can revert this change by configuring tags for the module and omittin
206206
- Remove specific win32 api errors from events in perfmon. {issue}18292[18292] {pull}18361[18361]
207207
- Fix application_pool metricset after pdh changes. {pull}18477[18477]
208208
- Fix tags_filter for cloudwatch metricset in aws. {pull}18524[18524]
209+
- Fix getting gcp compute instance metadata with partial zone/region in config. {pull}18757[18757]
209210
- Add missing network.sent_packets_count metric into compute metricset in googlecloud module. {pull}18802[18802]
210211

211212
*Packetbeat*

x-pack/metricbeat/module/googlecloud/stackdriver/compute/metadata.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
1616

1717
"github.com/elastic/beats/v7/libbeat/common"
18-
18+
"github.com/elastic/beats/v7/libbeat/logp"
1919
"github.com/elastic/beats/v7/x-pack/metricbeat/module/googlecloud"
2020
)
2121

@@ -27,6 +27,7 @@ func NewMetadataService(projectID, zone string, region string, opt ...option.Cli
2727
region: region,
2828
opt: opt,
2929
instanceCache: common.NewCache(30*time.Second, 13),
30+
logger: logp.NewLogger("stackdriver-compute"),
3031
}, nil
3132
}
3233

@@ -55,12 +56,13 @@ type metadataCollector struct {
5556
computeMetadata *computeMetadata
5657

5758
instanceCache *common.Cache
59+
logger *logp.Logger
5860
}
5961

6062
// Metadata implements googlecloud.MetadataCollector to the known set of labels from a Compute TimeSeries single point of data.
6163
func (s *metadataCollector) Metadata(ctx context.Context, resp *monitoringpb.TimeSeries) (googlecloud.MetadataCollectorData, error) {
6264
if s.computeMetadata == nil {
63-
_, err := s.instanceMetadata(ctx, s.instanceID(resp), s.zone, s.region)
65+
_, err := s.instanceMetadata(ctx, s.instanceID(resp), s.instanceZone(resp))
6466
if err != nil {
6567
return googlecloud.MetadataCollectorData{}, err
6668
}
@@ -104,8 +106,8 @@ func (s *metadataCollector) Metadata(ctx context.Context, resp *monitoringpb.Tim
104106
}
105107

106108
// instanceMetadata returns the labels of an instance
107-
func (s *metadataCollector) instanceMetadata(ctx context.Context, instanceID, zone string, region string) (*computeMetadata, error) {
108-
i, err := s.instance(ctx, instanceID, zone, region)
109+
func (s *metadataCollector) instanceMetadata(ctx context.Context, instanceID, zone string) (*computeMetadata, error) {
110+
i, err := s.instance(ctx, instanceID, zone)
109111
if err != nil {
110112
return nil, errors.Wrapf(err, "error trying to get data from instance '%s' in zone '%s'", instanceID, zone)
111113
}
@@ -139,7 +141,7 @@ func (s *metadataCollector) instanceMetadata(ctx context.Context, instanceID, zo
139141
}
140142

141143
// instance returns data from an instance ID using the cache or making a request
142-
func (s *metadataCollector) instance(ctx context.Context, instanceID, zone string, region string) (*compute.Instance, error) {
144+
func (s *metadataCollector) instance(ctx context.Context, instanceID, zone string) (*compute.Instance, error) {
143145
service, err := compute.NewService(ctx, s.opt...)
144146
if err != nil {
145147
return nil, errors.Wrapf(err, "error getting client from Compute service")
@@ -152,29 +154,11 @@ func (s *metadataCollector) instance(ctx context.Context, instanceID, zone strin
152154
}
153155
}
154156

155-
if region != "" {
156-
regionData, err := service.Regions.Get(s.projectID, region).Do()
157-
if err != nil {
158-
return nil, errors.Wrapf(err, "error getting region information for '%s'", region)
159-
}
160-
161-
zones := regionData.Zones
162-
for _, zone := range zones {
163-
zString := strings.Split(zone, "/")
164-
zName := zString[len(zString)-1]
165-
instanceData, err := service.Instances.Get(s.projectID, zName, instanceID).Do()
166-
if err != nil {
167-
continue
168-
}
169-
s.instanceCache.Put(instanceID, instanceData)
170-
return instanceData, nil
171-
}
172-
}
173-
174157
if zone != "" {
175158
instanceData, err := service.Instances.Get(s.projectID, zone, instanceID).Do()
176159
if err != nil {
177-
return nil, errors.Wrapf(err, "error getting instance information for instance with ID '%s'", instanceID)
160+
s.logger.Warnf("failed to get instance information for instance '%s' in zone '%s', skipping metadata for instance", instanceID, zone)
161+
return nil, nil
178162
}
179163
s.instanceCache.Put(instanceID, instanceData)
180164
return instanceData, nil
@@ -189,3 +173,11 @@ func (s *metadataCollector) instanceID(ts *monitoringpb.TimeSeries) string {
189173

190174
return ""
191175
}
176+
177+
func (s *metadataCollector) instanceZone(ts *monitoringpb.TimeSeries) string {
178+
if ts.Resource != nil && ts.Resource.Labels != nil {
179+
return ts.Resource.Labels[googlecloud.TimeSeriesResponsePathForECSAvailabilityZone]
180+
}
181+
182+
return ""
183+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package compute
6+
7+
import (
8+
"testing"
9+
"time"
10+
11+
"github.com/golang/protobuf/ptypes/timestamp"
12+
"github.com/stretchr/testify/assert"
13+
"google.golang.org/genproto/googleapis/api/metric"
14+
"google.golang.org/genproto/googleapis/api/monitoredres"
15+
"google.golang.org/genproto/googleapis/monitoring/v3"
16+
17+
"github.com/elastic/beats/v7/libbeat/common"
18+
)
19+
20+
var fake = &monitoring.TimeSeries{
21+
Resource: &monitoredres.MonitoredResource{
22+
Type: "gce_instance",
23+
Labels: map[string]string{
24+
"instance_id": "4624337448093162893",
25+
"project_id": "elastic-metricbeat",
26+
"zone": "us-central1-a",
27+
},
28+
},
29+
Metadata: &monitoredres.MonitoredResourceMetadata{
30+
UserLabels: map[string]string{
31+
"user": "label",
32+
},
33+
},
34+
Metric: &metric.Metric{
35+
Labels: map[string]string{
36+
"instance_name": "instance-1",
37+
},
38+
Type: "compute.googleapis.com/instance/cpu/usage_time",
39+
},
40+
MetricKind: metric.MetricDescriptor_GAUGE,
41+
ValueType: metric.MetricDescriptor_DOUBLE,
42+
Points: []*monitoring.Point{{
43+
Value: &monitoring.TypedValue{
44+
Value: &monitoring.TypedValue_DoubleValue{DoubleValue: 0.0041224284852319215},
45+
},
46+
Interval: &monitoring.TimeInterval{
47+
StartTime: &timestamp.Timestamp{
48+
Seconds: 1569932700,
49+
},
50+
EndTime: &timestamp.Timestamp{
51+
Seconds: 1569932700,
52+
},
53+
},
54+
}, {
55+
Value: &monitoring.TypedValue{
56+
Value: &monitoring.TypedValue_DoubleValue{DoubleValue: 0.004205757571772513},
57+
},
58+
Interval: &monitoring.TimeInterval{
59+
StartTime: &timestamp.Timestamp{
60+
Seconds: 1569932640,
61+
},
62+
EndTime: &timestamp.Timestamp{
63+
Seconds: 1569932640,
64+
},
65+
},
66+
}},
67+
}
68+
69+
var m = &metadataCollector{
70+
projectID: "projectID",
71+
instanceCache: common.NewCache(30*time.Second, 13),
72+
}
73+
74+
func TestInstanceID(t *testing.T) {
75+
instanceID := m.instanceID(fake)
76+
assert.Equal(t, "4624337448093162893", instanceID)
77+
}
78+
79+
func TestInstanceZone(t *testing.T) {
80+
zone := m.instanceZone(fake)
81+
assert.Equal(t, "us-central1-a", zone)
82+
}

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

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,9 @@ package stackdriver
77
import (
88
"testing"
99

10-
"github.com/golang/protobuf/ptypes/timestamp"
1110
"github.com/stretchr/testify/assert"
12-
"google.golang.org/genproto/googleapis/api/metric"
13-
"google.golang.org/genproto/googleapis/api/monitoredres"
14-
"google.golang.org/genproto/googleapis/monitoring/v3"
1511
)
1612

17-
var fake *monitoring.TimeSeries = &monitoring.TimeSeries{
18-
Resource: &monitoredres.MonitoredResource{
19-
Type: "gce_instance",
20-
Labels: map[string]string{
21-
"instance_id": "4624337448093162893",
22-
"project_id": "elastic-metricbeat",
23-
"zone": "us-central1-a",
24-
},
25-
},
26-
Metadata: &monitoredres.MonitoredResourceMetadata{
27-
UserLabels: map[string]string{
28-
"user": "label",
29-
},
30-
},
31-
Metric: &metric.Metric{
32-
Labels: map[string]string{
33-
"instance_name": "instance-1",
34-
},
35-
Type: "compute.googleapis.com/instance/cpu/usage_time",
36-
},
37-
MetricKind: metric.MetricDescriptor_GAUGE,
38-
ValueType: metric.MetricDescriptor_DOUBLE,
39-
Points: []*monitoring.Point{{
40-
Value: &monitoring.TypedValue{
41-
Value: &monitoring.TypedValue_DoubleValue{DoubleValue: 0.0041224284852319215},
42-
},
43-
Interval: &monitoring.TimeInterval{
44-
StartTime: &timestamp.Timestamp{
45-
Seconds: 1569932700,
46-
},
47-
EndTime: &timestamp.Timestamp{
48-
Seconds: 1569932700,
49-
},
50-
},
51-
}, {
52-
Value: &monitoring.TypedValue{
53-
Value: &monitoring.TypedValue_DoubleValue{DoubleValue: 0.004205757571772513},
54-
},
55-
Interval: &monitoring.TimeInterval{
56-
StartTime: &timestamp.Timestamp{
57-
Seconds: 1569932640,
58-
},
59-
EndTime: &timestamp.Timestamp{
60-
Seconds: 1569932640,
61-
},
62-
},
63-
}},
64-
}
65-
66-
var metrics = []string{
67-
"compute.googleapis.com/instance/cpu/utilization",
68-
"compute.googleapis.com/instance/disk/read_bytes_count",
69-
"compute.googleapis.com/http/server/response_latencies",
70-
}
71-
7213
func TestCleanMetricNameString(t *testing.T) {
7314
cases := []struct {
7415
title string

0 commit comments

Comments
 (0)