newrelic / newrelic-browser-agent

New Relic Browser Agent
Apache License 2.0
77 stars 40 forks source link

fix: Release backlog memory when features are blocked by RUM #1102

Closed cwli24 closed 2 months ago

cwli24 commented 3 months ago

Plug a memory leak in Session Trace and other features. When entitlement or enable flag from RUM request returns a 0 for a feature, the agent will now release the reference to the events buffer under that feature so it can be garbage collected.

Overview

The pre-load cycle of the agent sets up listeners and buffers events in a backlog and an internal handlers object to be flushed and processed after the page loads. It was discovered that if the RUM call getting made post-load returns a 0 for an applicable feature, those things are never cleared for such feature. This caused the backlog for instance to indefinitely hold onto events awaiting processing, consuming memory. This fix will flush those events in such cases when the features are not activated via the deregisterDrain call made by each.

Related Issue(s)

https://new-relic.atlassian.net/browse/NR-284960 This issue was also seen in other features and have been addressed together.

Testing

New e2e on -b *:

image
github-actions[bot] commented 3 months ago

Asset Size Report

Merging this pull request will result in the following asset size changes:

Agent Asset Previous Size New Size Diff
lite loader 34.16 kB / 11.9 kB (gzip) 34.25 kB / 11.92 kB (gzip) 0.26% / 0.16% (gzip)
lite async-chunk 50.09 kB / 16.18 kB (gzip) 50.09 kB / 16.18 kB (gzip) 0% / 0% (gzip)
pro loader 54.83 kB / 18.55 kB (gzip) 54.92 kB / 18.57 kB (gzip) 0.16% / 0.11% (gzip)
pro async-chunk 96.73 kB / 28.99 kB (gzip) 96.73 kB / 28.99 kB (gzip) 0% / 0% (gzip)
spa loader 62.77 kB / 20.97 kB (gzip) 62.86 kB / 21 kB (gzip) 0.14% / 0.14% (gzip)
spa async-chunk 111.84 kB / 33.37 kB (gzip) 111.84 kB / 33.37 kB (gzip) 0% / 0% (gzip)
lite-polyfills loader 127.79 kB / 41.22 kB (gzip) 127.83 kB / 41.24 kB (gzip) 0.03% / 0.04% (gzip)
lite-polyfills async-chunk 63.99 kB / 18.45 kB (gzip) 63.99 kB / 18.45 kB (gzip) 0% / 0% (gzip)
pro-polyfills loader 150.17 kB / 47.51 kB (gzip) 150.21 kB / 47.54 kB (gzip) 0.03% / 0.05% (gzip)
pro-polyfills async-chunk 116.73 kB / 31.22 kB (gzip) 116.73 kB / 31.22 kB (gzip) 0% / 0% (gzip)
spa-polyfills loader 158.37 kB / 49.67 kB (gzip) 158.41 kB / 49.69 kB (gzip) 0.03% / 0.04% (gzip)
spa-polyfills async-chunk 133.71 kB / 35.92 kB (gzip) 133.71 kB / 35.92 kB (gzip) 0% / 0% (gzip)
github-actions[bot] commented 3 months ago

Static Badge

Last ran on July 09, 2024 11:59:53 CDT Checking merge of (d6f1e8dff62a7a056d34499d34cb933d3c92fdb5) into main (3d815a3988583911322541007bd5e176a5bba4c1)

codecov[bot] commented 3 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 87.63%. Comparing base (3d815a3) to head (c4d8ba5). Report is 1 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1102 +/- ## ======================================= Coverage 87.62% 87.63% ======================================= Files 162 162 Lines 7259 7263 +4 Branches 1423 1426 +3 ======================================= + Hits 6361 6365 +4 Misses 782 782 Partials 116 116 ``` | [Flag](https://app.codecov.io/gh/newrelic/newrelic-browser-agent/pull/1102/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=newrelic) | Coverage Δ | | |---|---|---| | [unit-tests](https://app.codecov.io/gh/newrelic/newrelic-browser-agent/pull/1102/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=newrelic) | `76.44% <100.00%> (+<0.01%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=newrelic#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

patrickhousley commented 2 months ago

I did some manual testing locally. Here are my findings.

Looks like the backlog is getting cleared correctly when SR and ST are enabled in the agent config but RUM responds with a 0.

image