grafana / faro-web-sdk

The Grafana Faro Web SDK, part of the Grafana Faro project, is a highly configurable web SDK for real user monitoring (RUM) that instruments browser frontend applications to capture observability signals. Frontend telemetry can then be correlated with backend and infrastructure data for full-stack observability.
https://grafana.com/oss/faro/
Apache License 2.0
688 stars 62 forks source link

fix: infinite loop if session dom storage is turned off #519

Closed codecapitano closed 3 months ago

codecapitano commented 3 months ago

Why

If DOM storage is diabled teh web-sdk got stuck in a loop.

This is because in the updateSession we fetch a session from DOM storage, validate it and then either update the last activity timestamp of the valid session or create a new session.

In case DOM storage is not available, we always get a null value for stored session which is invalid. So we only update the in-memory session meta with the new session. Creating a new session via this function is basically a session extend, so it causes a session extend event to be send, which in turn leads to that the update function is called again, which cuases teh inifnifte loop beacuse the above steps are executed over and over again.

Responsible part of the update function


    if (isUserSessionValid(sessionFromStorage)) {
      storeUserSession({ ...sessionFromStorage!, lastActivity: dateNow() });
    } else {
      let newSession = addSessionMetadataToNextSession(
        createUserSessionObject({ isSampled: isSampled() }),
        sessionFromStorage
      );

      storeUserSession(newSession);

      faro.api?.setSession(newSession.sessionMeta);
      sessionTrackingConfig?.onSessionChange?.(sessionFromStorage?.sessionMeta ?? null, newSession.sessionMeta!);
    }

What

Check if DOM storage is availble and if not don't execute the update (session_extend).

This will still gives send data and sessionID, but:

Links

Checklist