New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[metricbeat] [gcp] remove compute metadata cache #33655
Merged
gpop63
merged 11 commits into
elastic:main
from
gpop63:remove_gcp-compute_metadata_cache
Dec 5, 2022
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c606c49
remove compute metadata cache
gpop63 bad28ca
add logs and change map key type
gpop63 114a009
Merge remote-tracking branch 'upstream/main' into remove_gcp-compute_…
gpop63 62396a5
Merge branch 'main' into remove_gcp-compute_metadata_cache
gpop63 b38858b
add import aliases
gpop63 fd29c5f
Merge remote-tracking branch 'upstream/main' into remove_gcp-compute_…
gpop63 2d18131
add changelog entry
gpop63 d9f4192
cleanup comments
gpop63 0f0233a
Merge remote-tracking branch 'upstream/main' into remove_gcp-compute_…
gpop63 23b6c18
remove comments
gpop63 f5cf092
Merge branch 'main' into remove_gcp-compute_metadata_cache
gpop63 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,32 +9,33 @@ import ( | |
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"google.golang.org/api/compute/v1" | ||
compute "google.golang.org/api/compute/v1" | ||
"google.golang.org/api/option" | ||
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" | ||
|
||
"github.com/elastic/beats/v7/libbeat/common" | ||
"github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp" | ||
"github.com/elastic/elastic-agent-libs/logp" | ||
) | ||
|
||
/* | ||
const ( | ||
cacheTTL = 30 * time.Second | ||
initialCacheSize = 13 | ||
) | ||
*/ | ||
|
||
// NewMetadataService returns the specific Metadata service for a GCP Compute resource | ||
func NewMetadataService(projectID, zone string, region string, regions []string, opt ...option.ClientOption) (gcp.MetadataService, error) { | ||
return &metadataCollector{ | ||
projectID: projectID, | ||
zone: zone, | ||
region: region, | ||
regions: regions, | ||
opt: opt, | ||
instanceCache: common.NewCache(cacheTTL, initialCacheSize), | ||
logger: logp.NewLogger("metrics-compute"), | ||
projectID: projectID, | ||
zone: zone, | ||
region: region, | ||
regions: regions, | ||
opt: opt, | ||
// instanceCache: common.NewCache(cacheTTL, initialCacheSize), | ||
computeInstances: make(map[uint64]*compute.Instance), | ||
logger: logp.NewLogger("metrics-compute"), | ||
}, nil | ||
} | ||
|
||
|
@@ -55,13 +56,14 @@ type computeMetadata struct { | |
} | ||
|
||
type metadataCollector struct { | ||
projectID string | ||
zone string | ||
region string | ||
regions []string | ||
opt []option.ClientOption | ||
instanceCache *common.Cache | ||
logger *logp.Logger | ||
projectID string | ||
zone string | ||
region string | ||
regions []string | ||
opt []option.ClientOption | ||
// instanceCache *common.Cache | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get rid of this too? |
||
computeInstances map[uint64]*compute.Instance | ||
logger *logp.Logger | ||
} | ||
|
||
// Metadata implements googlecloud.MetadataCollector to the known set of labels from a Compute TimeSeries single point of data. | ||
|
@@ -120,6 +122,7 @@ func (s *metadataCollector) instanceMetadata(ctx context.Context, instanceID, zo | |
} | ||
|
||
if instance == nil { | ||
s.logger.Debugf("couldn't find instance %s, call Instances.AggregatedList", instanceID) | ||
return computeMetadata, nil | ||
} | ||
|
||
|
@@ -144,14 +147,27 @@ func (s *metadataCollector) instanceMetadata(ctx context.Context, instanceID, zo | |
|
||
// instance returns data from an instance ID using the cache or making a request | ||
func (s *metadataCollector) instance(ctx context.Context, instanceID string) (*compute.Instance, error) { | ||
s.refreshInstanceCache(ctx) | ||
instanceCachedData := s.instanceCache.Get(instanceID) | ||
if instanceCachedData != nil { | ||
if computeInstance, ok := instanceCachedData.(*compute.Instance); ok { | ||
return computeInstance, nil | ||
/* | ||
s.refreshInstanceCache(ctx) | ||
instanceCachedData := s.instanceCache.Get(instanceID) | ||
if instanceCachedData != nil { | ||
if computeInstance, ok := instanceCachedData.(*compute.Instance); ok { | ||
return computeInstance, nil | ||
} | ||
} | ||
*/ | ||
|
||
s.getComputeInstances(ctx) | ||
|
||
instanceIdInt, _ := strconv.Atoi(instanceID) | ||
computeInstance, ok := s.computeInstances[uint64(instanceIdInt)] | ||
if ok { | ||
return computeInstance, nil | ||
} | ||
|
||
// Remake the compute instances map to avoid having stale data. | ||
s.computeInstances = make(map[uint64]*compute.Instance) | ||
|
||
return nil, nil | ||
} | ||
|
||
|
@@ -171,12 +187,21 @@ func (s *metadataCollector) instanceZone(ts *monitoringpb.TimeSeries) string { | |
return "" | ||
} | ||
|
||
func (s *metadataCollector) refreshInstanceCache(ctx context.Context) { | ||
// only refresh cache if it is empty | ||
if s.instanceCache.Size() > 0 { | ||
func (s *metadataCollector) getComputeInstances(ctx context.Context) { | ||
/* | ||
// only refresh cache if it is empty | ||
if s.instanceCache.Size() > 0 { | ||
return | ||
} | ||
s.logger.Debugf("refresh cache with Instances.AggregatedList API") | ||
*/ | ||
|
||
if len(s.computeInstances) > 0 { | ||
return | ||
} | ||
s.logger.Debugf("refresh cache with Instances.AggregatedList API") | ||
|
||
s.logger.Debug("Compute API Instances.AggregatedList") | ||
|
||
computeService, err := compute.NewService(ctx, s.opt...) | ||
if err != nil { | ||
s.logger.Errorf("error getting client from Compute service: %v", err) | ||
|
@@ -187,7 +212,8 @@ func (s *metadataCollector) refreshInstanceCache(ctx context.Context) { | |
if err := req.Pages(ctx, func(page *compute.InstanceAggregatedList) error { | ||
for _, instancesScopedList := range page.Items { | ||
for _, instance := range instancesScopedList.Instances { | ||
s.instanceCache.Put(strconv.Itoa(int(instance.Id)), instance) | ||
// s.instanceCache.Put(strconv.Itoa(int(instance.Id)), instance) | ||
s.computeInstances[instance.Id] = instance | ||
} | ||
} | ||
return nil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,12 @@ package compute | |
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/protobuf/ptypes/timestamp" | ||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/genproto/googleapis/api/metric" | ||
"google.golang.org/genproto/googleapis/api/monitoredres" | ||
"google.golang.org/genproto/googleapis/monitoring/v3" | ||
|
||
"github.com/elastic/beats/v7/libbeat/common" | ||
monitoring "google.golang.org/genproto/googleapis/monitoring/v3" | ||
) | ||
|
||
var fake = &monitoring.TimeSeries{ | ||
|
@@ -67,8 +64,8 @@ var fake = &monitoring.TimeSeries{ | |
} | ||
|
||
var m = &metadataCollector{ | ||
projectID: "projectID", | ||
instanceCache: common.NewCache(30*time.Second, 13), | ||
projectID: "projectID", | ||
// instanceCache: common.NewCache(30*time.Second, 13), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get rid of this too? |
||
} | ||
|
||
func TestInstanceID(t *testing.T) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we remove these const since we are not using it anymore? :)