Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
sentry: Avoid early reads from state data.
Anders Kaseorgac68996ac3ba
diff --git a/web/src/sentry.ts b/web/src/sentry.tsindex e544820f916..b35108e445b 100644--- a/web/src/sentry.ts+++ b/web/src/sentry.ts@@ -1,5 +1,4 @@import * as Sentry from "@sentry/browser";-import _ from "lodash";import {page_params} from "./page_params";import {current_user, realm} from "./state_data";@@ -32,15 +31,6 @@ export function shouldCreateSpanForRequest(url: string): boolean {}if (page_params.server_sentry_dsn) {- const url_matches = [/^\//];- if (document.currentScript instanceof HTMLScriptElement) {- url_matches.push(- new RegExp("^" + _.escapeRegExp(new URL(".", document.currentScript.src).href)),- );- }- if (realm.realm_uri !== undefined) {- url_matches.push(new RegExp("^" + _.escapeRegExp(realm.realm_uri) + "/"));- }const sentry_key =// No parameter is the portico pages, empty string is the empty realmpage_params.realm_sentry_key === undefined@@ -48,27 +38,6 @@ if (page_params.server_sentry_dsn) {: page_params.realm_sentry_key === ""? "(root)": page_params.realm_sentry_key;- const user_info: UserInfo = {- realm: sentry_key,- };- if (sentry_key !== "www") {- user_info.role = current_user.is_owner- ? "Organization owner"- : current_user.is_admin- ? "Organization administrator"- : current_user.is_moderator- ? "Moderator"- : current_user.is_guest- ? "Guest"- : page_params.is_spectator- ? "Spectator"- : current_user.user_id- ? "Member"- : "Logged out";- if (current_user.user_id) {- user_info.id = current_user.user_id.toString();- }- }const sample_rates = new Map([// This is controlled by shouldCreateSpanForRequest, above, but also put here for consistency@@ -86,7 +55,6 @@ if (page_params.server_sentry_dsn) {release: "zulip-server@" + ZULIP_VERSION,integrations: [new Sentry.BrowserTracing({- tracePropagationTargets: url_matches,startTransactionOnLocationChange: false,beforeNavigate(context) {return {@@ -98,20 +66,43 @@ if (page_params.server_sentry_dsn) {shouldCreateSpanForRequest,}),],- allowUrls: url_matches,sampleRate: page_params.server_sentry_sample_rate ?? 0,tracesSampler(samplingContext) {const base_rate = page_params.server_sentry_trace_rate ?? 0;const name = samplingContext.transactionContext.name;return base_rate * (sample_rates.get(name) ?? 1);},- initialScope: {- tags: {+ initialScope(scope) {+ const user_info: UserInfo = {+ realm: sentry_key,+ };+ if (sentry_key !== "www" && current_user !== undefined) {+ user_info.role = current_user.is_owner+ ? "Organization owner"+ : current_user.is_admin+ ? "Organization administrator"+ : current_user.is_moderator+ ? "Moderator"+ : current_user.is_guest+ ? "Guest"+ : page_params.is_spectator+ ? "Spectator"+ : current_user.user_id+ ? "Member"+ : "Logged out";+ if (current_user.user_id) {+ user_info.id = current_user.user_id.toString();+ }+ }+ scope.setTags({realm: sentry_key,user_role: user_info.role ?? "Browser",- server_version: realm.zulip_version,- },- user: user_info,+ });+ if (realm !== undefined) {+ scope.setTag("server_version", realm.zulip_version);+ }+ scope.setUser(user_info);+ return scope;},});} else {