Make topKMetrics thread-safe in metrics limiter #1738
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.
The shared topKMetrics data structure did not include protection for concurrent access, which led to crashes. Below is a sample log illustrating the issue.
This code change introduces a read/write mutex to ensure thread safety and prevent data races.
All unit tests pass, and manual end-to-end tests have been successfully completed.
fatal error: concurrent map writes
goroutine 180863 [running]:
internal/runtime/maps.fatal({0x552d503?, 0x22?})
runtime/panic.go:1058 +0x18
github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/internal/cardinalitycontrol.(*topKMetrics).Push(0xc000bd56e0, 0x5d2e590?, 0xc00145f780)
github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/internal/cardinalitycontrol/metrics_limiter.go:310 +0x1c5
github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/internal/cardinalitycontrol.(*service).InsertMetricDataToSecondary(0xc000a8e240, 0xc00145f700)
github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/internal/cardinalitycontrol/metrics_limiter.go:209 +0xec
github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/internal/cardinalitycontrol.(*MetricsLimiter).Admit(0xc000ad4050, {0xc000df1fc9, 0x5}, {0xc00081c1c0?, 0xc000df1f08?}, {0xc001156618?, 0xc000df1f08?}) --------------
License
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.