getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.89k stars 1.56k forks source link

Synchronize SentryJS logger state among different extension contexts #10047

Open GaurangTandon opened 9 months ago

GaurangTandon commented 9 months ago

Problem Statement

With the upcoming Manifest V3 changes, almost all powerful extensions will end up having at least three separate extension contexts:

  1. an "extension service worker" that has access to all chrome.* APIs and is the core backbone of the extension (see introduction here)
  2. an "offscreen document" that has access to DOM APIs and enables extensions to perform DOM manipulation (see introduction here)
  3. a "sandbox" frame inside which extensions can run eval() - this frame is probably going to become more common now given strict MV3 requirements (see introduction here).

The extension service worker and the offscreen document are completely separate processes. The sandbox is iframed inside the offscreen document.

Coming back to SentryJS...right now, I have added Sentry.init and configureScope to all the three contexts. It is working fine. The issue I am facing is that all the breadcrumbs and tag data are separate between the three processes (nothing is shared).

So,

  1. when there any updates to the scope data - for example: the ID of the currently logged in user - I need to propagate that updates to all the three contexts and that's a lot of work to code up and do correctly.
  2. if there are any breadcrumbs, I only get the breadcrumbs from the context where this issue happened (let's say sandbox), and not the other contexts (service worker and the offscreen documents). This results in an incomplete understanding of the issue.

Question: what is a feature that SentryJS can implement to better support these use cases. As I mentioned at the beginning, with the upcoming Manifest V3 architecture, this scenario is going to be a lot more common.

Solution Brainstorm

I had one idea: allow me to initialise a "master" sentry context, and then once (at the initialisation step) I will setup the message hooks to connect other Sentry contexts with the master context. Then, from there on, SentryJS can internally use the provided message hooks to propagate updates from master to all the clients, and unify all the breadcrumbs (and other data) whenever any event occurs.

AbhiPrasad commented 8 months ago

Hey @GaurangTandon

To support this, it seems like you'll need to sync the scope and sent events across the different processes. An example of how we do this is with the Electron SDK.

An integration that syncs the scope from renderer process to main process: https://github.com/getsentry/sentry-electron/blob/master/src/renderer/integrations/scope-to-main.ts

Logic in the main process that handles IPC from renderer: https://github.com/getsentry/sentry-electron/blob/master/src/main/ipc.ts

I don't think we have the bandwidth to support this as a 1st class integration for now given our other priorities, but happy to help give feedback/review any solutions you try out.

GaurangTandon commented 8 months ago

Hi @AbhiPrasad thanks I'll try it out by next week and let you know how it goes.

spookyuser commented 8 months ago

this would be so cool!