designcourse icon indicating copy to clipboard operation
designcourse copied to clipboard

Expose ability to manually flush

Open faizan-ali opened this issue 1 year ago • 9 comments

Background I'm running the browser SDK installed through npm inside a Chrome extension (React + Next.js)

Problem The extension is meant to be used for a few seconds before closing, meaning all Javascript code execution halts.

This happens before the SDK flushes logs to Datadog almost 100% of the time.

Potential solution Expose a function to flush logs manually. The Node.js client allows this.

Somewhat related to: https://github.com/DataDog/browser-sdk/issues/767 https://github.com/DataDog/browser-sdk/issues/956

faizan-ali avatar Jul 09 '23 23:07 faizan-ali

~~I've found a workaround by triggering a visibilitychange event similar to your tests but unsure what the downstream implications of this could be.~~

EDIT: The workaround does not work

faizan-ali avatar Jul 10 '23 03:07 faizan-ali

Also relevant to #432 (service workers are both short-lived and don't have comparable page exit events to observe)

dbjorge avatar Jul 10 '23 15:07 dbjorge

Hello,

For manual flush, we want to be careful and avoid to end up in situations where every event is sent in a dedicated request, impacting both the end user bandwidth and our servers traffic. An approach that we were considering was to to trigger a flush when calling the stopSession() API (not yet implemented in logs), forcing the sending of the last events with the end of the session. Would it be helpful for your use cases?

bcaudan avatar Jul 19 '23 10:07 bcaudan

@bcaudan From what I understand, that's functionality equivalent to a manual flush so I believe it would be :)

Does stopSession() have a delay, or is it a synchronous call? I recognize the dangers of exposing an easy way to overload your servers - maybe a middle ground could be a rate limit so end users attempt a custom batching solution that works for them?

faizan-ali avatar Aug 10 '23 19:08 faizan-ali

Does stopSession() have a delay, or is it a synchronous call?

it is a synchronous call.

maybe a middle ground could be a rate limit so end users attempt a custom batching solution that works for them

Rate limiting a manual flush API would indeed be an interesting tradeoff, we'll keep this approach in mind.

bcaudan avatar Aug 11 '23 07:08 bcaudan

We have a similar use-case for our Office Add-in, where users will generally have completed their interactions and closed the add-in iframe in which our application runs in under 30 seconds.

As a workaround, it seems manually raising a freeze event will force any batched messages to be flushed:

document.dispatchEvent(new Event('freeze'));

BodrickLight avatar Aug 14 '23 18:08 BodrickLight

The above workaround does work as long as allowUntrustedEvents is set to true in the init configuration.

+1 to exposing this.

cdtinney avatar Jan 17 '24 21:01 cdtinney

would be really nice to have this for local development. Often I want to test if some more complex logger calls appear properly in DD, so I trigger them and then have to sit around waiting for the API call to send them

zacaj avatar Feb 22 '24 17:02 zacaj

Hello @zacaj , For local development, I suggest using the browser SDK extension which allow you to see the events collected, among other things.

amortemousque avatar Feb 23 '24 14:02 amortemousque