@@ -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.
6163func (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+ }
0 commit comments