Skip to content

Add user identifier for Sentry - #863

Merged
fredrikekelund merged 2 commits into
trunkfrom
f26d/sentry-user-identifier
Jan 31, 2025
Merged

Add user identifier for Sentry#863
fredrikekelund merged 2 commits into
trunkfrom
f26d/sentry-user-identifier

Conversation

@fredrikekelund

Copy link
Copy Markdown
Contributor

Related issues

Proposed Changes

Store a UUID in appdata-v1.json to identify users in Sentry. This will help us determine how many users are affected by the issues logged to Sentry.

The UUID is (like all UUIDs) random, meaning we don't log any PII to Sentry.

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.

Testing Instructions

  1. Apply the following diff
diff --git a/src/index.ts b/src/index.ts
index 741245a..838464b 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -41,7 +41,7 @@ if ( ! isCLI() && ! process.env.IS_DEV_BUILD ) {
        Sentry.init( {
                dsn: 'https://97693275b2716fb95048c6d12f4318cf@o248881.ingest.sentry.io/4506612776501248',
                debug: true,
-               enabled: process.env.NODE_ENV !== 'development' && process.env.NODE_ENV !== 'test',
+               enabled: process.env.NODE_ENV !== 'test',
                release: `${ app.getVersion() ? app.getVersion() : COMMIT_HASH }-${ getPlatformName() }`,
        } );
 }
@@ -297,6 +297,8 @@ async function appBoot() {
                // Bump stat for unique weekly app launch, approximates weekly active users
                bumpAggregatedUniqueStat( 'local-environment-launch-uniques', process.platform, 'weekly' );
 
+               Sentry.captureException( new Error( 'App booted' ) );
+
                finishedInitialization = true;
        } );
  1. Start the app
  2. Find the line Setting Sentry user ID: xxxxx… in your console output and take note of your ID
  3. Check Sentry issues from the development environment
  4. Find the issue with the message App booted
  5. Ensure that your Sentry user ID appears under Tags > user (feel free to ping me for directions)

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?
@fredrikekelund
fredrikekelund requested a review from a team January 30, 2025 14:51
@fredrikekelund fredrikekelund self-assigned this Jan 30, 2025

@nightnei nightnei left a comment

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.

In general LGTM, but I want to talk a little bit about naming in Studio and Sentry :)

Comment thread src/index.ts
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.

Comment thread src/index.ts
}

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 👍

nightnei
nightnei approved these changes Jan 31, 2025
@fredrikekelund
fredrikekelund merged commit 03f13dd into trunk Jan 31, 2025
@fredrikekelund
fredrikekelund deleted the f26d/sentry-user-identifier branch January 31, 2025 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants