Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/__mocks__/@sentry/electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const init = jest.fn();
export const addBreadcrumb = jest.fn();
export const captureException = jest.fn();
export const setUser = jest.fn();
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
} from './migrations/migrate-from-wp-now-folder';
import { setupWPServerFiles, updateWPServerFiles } from './setup-wp-server-files';
import { stopAllServersOnQuit } from './site-server';
import { loadUserData } from './storage/user-data'; // eslint-disable-next-line import/order
import { loadUserData, saveUserData } from './storage/user-data'; // eslint-disable-next-line import/order
import { setupUpdates } from './updates';

if ( ! isCLI() && ! process.env.IS_DEV_BUILD ) {
Expand Down Expand Up @@ -271,14 +271,23 @@ async function appBoot() {
await migrateFromWpNowFolder();
}

const userData = await loadUserData();

if ( ! userData.sentryUserId ) {
userData.sentryUserId = crypto.randomUUID();

@nightnei nightnei Jan 30, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about studioInstanceId?

Suggested change
userData.sentryUserId = crypto.randomUUID();
userData.studioInstanceId = crypto.randomUUID();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I referred to in the PR description 😄

Per @nightnei's suggestion, I considered naming the sentryUserId key in the user config differently to indicate that it could be used for other reporting purposes in the future. However, I concluded that this is hypothetical, and it's better to optimize for the scenario at hand by using an explicit name.

To elaborate, I think a name like studioInstanceId is more difficult to understand at a glance, and there's currently no plan to use it for other purposes than Sentry reporting.

To that end, I think it's better to cross that bridge when and if we get there.

await saveUserData( userData );
}

console.log( 'Setting Sentry user ID:', userData.sentryUserId );
Sentry.setUser( { id: userData.sentryUserId } );

@nightnei nightnei Jan 30, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, looking at Sentry UI - it seems studio-instance-id will be more clear at a glance:

Suggested change
Sentry.setUser( { id: userData.sentryUserId } );
Sentry.setUser( { studio-instance-id: userData.sentryUserId } );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentry.setUser doesn't accept arbitrary keys. The id parameter is standardized. See docs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, in this case I definitely ok with naming 👍


createMainWindow();

// Handle CLI commands
listenCLICommands();
executeCLICommand();

// Bump stats for the first time the app runs - this is when no lastBumpStats are available
const userData = await loadUserData();
if ( ! userData.lastBumpStats ) {
bumpStat( 'studio-app-launch-first', process.platform );
}
Expand Down
5 changes: 5 additions & 0 deletions src/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,11 @@ export async function saveUserLocale( _event: IpcMainInvokeEvent, locale: string
} );
}

export async function getSentryUserId( _event: IpcMainInvokeEvent ): Promise< string | undefined > {
const userData = await loadUserData();
return userData.sentryUserId;
}

export async function getUserLocale( _event: IpcMainInvokeEvent ): Promise< SupportedLocale > {
return getUserLocaleWithFallback();
}
Expand Down
1 change: 1 addition & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const api: IpcApi = {
showSaveAsDialog: ( options: SaveDialogOptions ) =>
ipcRenderer.invoke( 'showSaveAsDialog', options ),
saveUserLocale: ( locale: string ) => ipcRenderer.invoke( 'saveUserLocale', locale ),
getSentryUserId: () => ipcRenderer.invoke( 'getSentryUserId' ),
getUserLocale: () => ipcRenderer.invoke( 'getUserLocale' ),
showUserSettings: () => ipcRenderer.invoke( 'showUserSettings' ),
startServer: ( id: string ) => ipcRenderer.invoke( 'startServer', id ),
Expand Down
10 changes: 6 additions & 4 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ window.onunhandledrejection = ( event ) => {
);
};

getIpcApi()
.getAppGlobals()
.then( ( appGlobals ) => {
Promise.all( [ getIpcApi().getAppGlobals(), getIpcApi().getSentryUserId() ] ).then(
( [ appGlobals, sentryUserId ] ) => {
// Ensure the app globals are available before any renderer code starts running
window.appGlobals = appGlobals;

Expand Down Expand Up @@ -125,9 +124,12 @@ getIpcApi()
showARM64MessageBox();
}

Sentry.setUser( { id: sentryUserId } );

const rootEl = document.getElementById( 'root' );
if ( rootEl ) {
const root = createRoot( rootEl );
root.render( createElement( StrictMode, null, createElement( Root ) ) );
}
} );
}
);
1 change: 1 addition & 0 deletions src/storage/storage-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface UserData {
};
promptWindowsSpeedUpResult?: PromptWindowsSpeedUpResult;
connectedWpcomSites?: { [ userId: number ]: SyncSite[] };
sentryUserId?: string;
}

export interface PersistedUserData extends Omit< UserData, 'sites' > {
Expand Down