bugsnag / bugsnag-js-performance

Monitor the performance of your JavaScript (web and React Native) and see the results in your BugSnag dashboard.
https://docs.bugsnag.com/performance/integration-guides
MIT License
5 stars 3 forks source link

Discard spans open for more than one hour #494

Closed yousif-bugsnag closed 2 months ago

yousif-bugsnag commented 3 months ago

Goal

Mitigates the potential for memory leaks caused by failing to end a span.

Currently if a span on the context stack is not ended for any reason (e.g. a developer has a problem with their logic for closing spans), then the span will remain open and there will be a reference to the span in the context stack until the thread / process / document (depending on platform) is killed.

Design

Ideally we would use WeakRefs in the context stack to allow spans on the context stack to be garbage collected, however as WeakRef isn't supported on older browser versions we need an alternative approach.

So to avoid holding references indefinitely, any spans with a start time older than 1 hour are considered 'invalid', allowing them to be removed from the context stack naturally (since invalid spans are popped off the stack whenever a span is added or removed).

If/when an invalid span is ended, it will be discarded. As part of this, also fixed a bug where spans that were explicitly discarded using the special discard end time (-1) would still be considered valid and remain on the context stack.

Testing

Added new unit tests

github-actions[bot] commented 3 months ago

Browser bundle size

NPM build

Package
Before 194.37 kB
After 195.00 kB
± +627 bytes ⚠️

CDN build

Unminified Minfied Minified + gzipped
Before 97.35 kB 36.18 kB 10.94 kB
After 97.88 kB 36.29 kB 10.98 kB
± +529 bytes ⚠️ +115 bytes ⚠️ +33 bytes ⚠️

Code coverage

Ok File (✨=New File) Lines Branches Functions Statements
/home/runner/work/bugsnag-js-performance/bugsnag-js-performance/packages/core/lib/span-factory.ts 100%
(+0%)
100%
(+3.13%)
100%
(+0%)
100%
(+1.25%)

Total:

Lines Branches Functions Statements
87.88%(+0.03%) 78.94%(+0.3%) 89.36%(+0%) 85.96%(+0.07%)

Generated against ca4c79257125ad33f38c2ce3add65846d00840c8 on 28 August 2024 at 11:49:45 UTC

gingerbenw commented 3 months ago

Also needs a changelog entry! 😄