microsoft / microsoft/ApplicationInsights-JS

[BUG] Very high cpu usage when moderate number of logs (100-200) gathered offline

Open
#2,035 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement offline-support
Dominant language
TypeScript
Stars
685
Forks
261
Avg merge
21h 33m
Merged PRs (30d)
5

Description

Description/Screenshot
I'm using ApplicationInsights-JS from within a cordova-based application, which often operates offline for reasonable periods of time.
After a fairly small number of offline logs have been collected (e.g., 100-200 items), the track call starts taking significant amounts of CPU, and console logs in Chrome debugger start to be spammed with warnings like:

[Violation] 'readystatechange' handler took 469ms

I think this is because the current mechanism deserialises and then re-serialises all the telemetry items on each send attempt. A quick profile shows that the there's a lot of time being lost to the _setBuffer call in SessionStorage:

image

https://github.com/microsoft/ApplicationInsights-JS/blob/master/channels/applicationinsights-channel-js/src/SendBuffer.ts#L312

This issue is made worse due to #2034, which causes a send attempt after each new track call once the send buffer size is exceeded.

Steps to Reproduce

  • OS/Browser: Any. These profiles were captured with Android 12's WebView class
  • SDK Version [e.g. 22]: 2.8.3
  • How you initialized the SDK: All default settings
  • Track ~200 items while being offline

Expected behavior
CPU usage remains nominal, despite being offline.

Additional context
Issue doesn't happen while online, as the stored session storage never gets that large.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in applicationinsights-channel-js/src/SendBuffer.ts around line 312 and inspect the _setBuffer path used when telemetry is stored offline. Reproduce the issue by tracking roughly 200 items while offline, then profile the repeated storage work. Done means offline tracking no longer causes the reported high CPU usage or slow handlers.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, javascript, typescript
Domain
observability, performance
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.