Commit 170fb0a
[Tracer] fix: Re-use runtime metrics writer resources to limit memory growth (#7434)
## Summary of changes
Updates `TracerManagerFactory.CreateTracerManager` to pass and re-use
the previous `RuntimeMetricsWriter`, if runtime metrics is enabled for
the new TracerManager. We still create a new `IDogStatsd` client every
time, so any updated Agent settings are adopted by the DogStatsd client,
but this could be further optimized at a later time to re-use the client
if none of its configuration (e.g. host/port/tags) have changed.
## Reason for change
We've observed a scenario where the number of `RuntimeEventListener`
instances continues to grow, consuming more and more memory. This
happens whenever new Dynamic Configuration settings are received by the
tracer and runtime metrics are enabled. This PR resolves the issue.
## Implementation details
When creating the new `TracerManager`, pass in the previous
`RuntimeMetricsWriter` instance and only update the `IDogStatsd` object
with new settings. This makes sure that we maintain only one
`RuntimeMetricsWriter` instance while getting up-to-date DogStatsD
settings throughout the application lifetime.
## Test coverage
Adds a small unit test to confirm that the previous
`RuntimeMetricsWriter` is re-used. Additionally, local testing was done
to confirm that the number of
`Datadog.Trace.RuntimeMetrics.RuntimeEventListener` objects does not
grow when Dynamic Configuration is updated.
### Without the fix
After some number of Dynamic Configuration settings were made in the
Datadog UI, a dump was taken with the following analysis:
```
> dumpheap -type Datadog.Trace.RuntimeMetrics.RuntimeEventListener
Address MT Size
0159c3c7fe88 7ffe4e801810 88
0159c442ed28 7ffe4e801810 88
0159c44eac60 7ffe4e801810 88
0159c4500f00 7ffe4e801810 88
0159c454ea68 7ffe4e801810 88
Statistics:
MT Count TotalSize Class Name
7ffe4e801810 5 440 Datadog.Trace.RuntimeMetrics.RuntimeEventListener
Total 5 objects, 440 bytes
```
Then after one additional Dynamic Configuration update was made in the
Datadog UI, a dump was taken with the following analysis:
```
> dumpheap -type Datadog.Trace.RuntimeMetrics.RuntimeEventListener
Address MT Size
0159c3c7fe88 7ffe4e801810 88
0159c442ed28 7ffe4e801810 88
0159c44eac60 7ffe4e801810 88
0159c4500f00 7ffe4e801810 88
0159c454ea68 7ffe4e801810 88
0159c61fef48 7ffe4e801810 88
Statistics:
MT Count TotalSize Class Name
7ffe4e801810 6 528 Datadog.Trace.RuntimeMetrics.RuntimeEventListener
Total 6 objects, 528 bytes
```
### With the fix
After some number of Dynamic Configuration settings were made in the
Datadog UI, a dump was taken with the following analysis:
```
> dumpheap -type Datadog.Trace.RuntimeMetrics.RuntimeEventListener
Address MT Size
01527107faf0 7ffe4e7acc00 88
Statistics:
MT Count TotalSize Class Name
7ffe4e7acc00 1 88 Datadog.Trace.RuntimeMetrics.RuntimeEventListener
Total 1 objects, 88 bytes
```
Then after one additional Dynamic Configuration update was made in the
Datadog UI, a dump was taken with the following analysis:
```
> dumpheap -type Datadog.Trace.RuntimeMetrics.RuntimeEventListener
Address MT Size
01527107faf0 7ffe4e7acc00 88
Statistics:
MT Count TotalSize Class Name
7ffe4e7acc00 1 88 Datadog.Trace.RuntimeMetrics.RuntimeEventListener
Total 1 objects, 88 bytes
```
## Other details
<!-- Fixes #{issue} -->
<!-- 1 parent 47d8c5e commit 170fb0a
File tree
3 files changed
+32
-5
lines changed- tracer
- src/Datadog.Trace
- RuntimeMetrics
- test/Datadog.Trace.Tests/Configuration
3 files changed
+32
-5
lines changedLines changed: 6 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | 42 | | |
44 | 43 | | |
45 | 44 | | |
| |||
52 | 51 | | |
53 | 52 | | |
54 | 53 | | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
161 | 166 | | |
162 | 167 | | |
163 | 168 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
64 | | - | |
| 63 | + | |
| 64 | + | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
140 | | - | |
| 140 | + | |
141 | 141 | | |
142 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
143 | 147 | | |
144 | 148 | | |
145 | 149 | | |
| |||
Lines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
93 | 111 | | |
94 | 112 | | |
95 | 113 | | |
| |||
0 commit comments