-
Notifications
You must be signed in to change notification settings - Fork 290
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
[OpenTelemetry.Instrumentation.AWS] Fix Memory Leak by Reusing ActivitySources, Meters, and Instruments #2039
[OpenTelemetry.Instrumentation.AWS] Fix Memory Leak by Reusing ActivitySources, Meters, and Instruments #2039
Conversation
…and instruments to avoid memory leaks
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2039 +/- ##
===========================================
+ Coverage 73.91% 84.78% +10.86%
===========================================
Files 267 20 -247
Lines 9615 368 -9247
===========================================
- Hits 7107 312 -6795
+ Misses 2508 56 -2452
Flags with carried forward coverage won't be shown. Click here to find out more.
|
@@ -9,11 +10,25 @@ namespace OpenTelemetry.Instrumentation.AWS.Implementation.Metrics; | |||
internal sealed class AWSHistogram<T> : Histogram<T> | |||
where T : struct | |||
{ | |||
private static readonly ConcurrentDictionary<string, System.Diagnostics.Metrics.Histogram<T>> HistogramsDictionary |
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.
naive question - any concern with effectively caching Histograms, Meters, etc indefinitely? Looking to confirm it is necessary to clean up any old objects.
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.
I don't think there are major side effects other than the memory overhead considering that other instrumentations packages do reuse the instruments too (ex:https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs#L36)
though we do store more than just one histogram and meter.
For cleaning up old objects, we do create one meter per service the first time it is used and around a dozen metrics instruments each, the names of these instruments are constants that doesn't include any user defined value, so there will always be a chance that the old objects are needed 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.
Approving per component owner review.
Merging as agreed with @ppittle. |
These are very common mistakes, and has now been explicitly called out in docs. See below for Metrics. Similar exists for Traces too. |
Fixes #2025
Changes
This pull request addresses issue #2025 a memory leak issue in
OpenTelemetry.Instrumentation.AWS
, particularly related to the new metrics features.The memory leak was identified after running a significant number of requests (around 10,000). The root cause was found to be the non-reuse of
ActivitySources
,Meters
, and Metrics Instruments. These objects were being instantiated repeatedly, leading to a gradual increase in memory usage.This PR includes refactoring the code to reuse
ActivitySources
,Meters
, and Metrics Instruments instead of creating new instances for each request.Here are memory graph screenshots to illustrate the impact of the fix:
Before the Fix (with memory leak):
After the Fix (no memory leak):
OpenTelemetry.Instrumentation.AWS
.Merge requirement checklist
CHANGELOG.md
files updated for non-trivial changes