Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

sentry: Delay reading current_user and realm until they’re available.

Anders Kaseorgf530f6749ca3
diff --git a/web/src/sentry.ts b/web/src/sentry.ts
index 4fb25a067e8..91d5350e4da 100644
--- a/web/src/sentry.ts
+++ b/web/src/sentry.ts
@@ -1,4 +1,5 @@
import * as Sentry from "@sentry/browser";
+import assert from "minimalistic-assert";
import {page_params} from "./base_page_params";
import {current_user, realm} from "./state_data";
@@ -9,6 +10,14 @@ type UserInfo = {
role?: string;
};
+const sentry_key =
+ // No parameter is the portico pages, empty string is the empty realm
+ page_params.realm_sentry_key === undefined
+ ? "www"
+ : page_params.realm_sentry_key === ""
+ ? "(root)"
+ : page_params.realm_sentry_key;
+
export function normalize_path(path: string, is_portico = false): string {
if (path === undefined) {
return "unknown";
@@ -30,15 +39,36 @@ export function shouldCreateSpanForRequest(url: string): boolean {
return parsed.pathname !== "/json/events";
}
-if (page_params.server_sentry_dsn) {
- const sentry_key =
- // No parameter is the portico pages, empty string is the empty realm
- page_params.realm_sentry_key === undefined
- ? "www"
- : page_params.realm_sentry_key === ""
- ? "(root)"
- : page_params.realm_sentry_key;
+export function initialize(): void {
+ // The current_user and realm structures are not available until this is
+ // called from ui_init.initialize_everything.
+ assert(page_params.page_type === "home");
+ const user_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";
+ const user_info: UserInfo = {realm: sentry_key, role: user_role};
+ if (current_user.user_id) {
+ user_info.id = current_user.user_id.toString();
+ }
+ Sentry.setTags({
+ user_role,
+ server_version: realm.zulip_version,
+ });
+ Sentry.setUser(user_info);
+}
+
+if (page_params.server_sentry_dsn) {
const sample_rates = new Map([
// This is controlled by shouldCreateSpanForRequest, above, but also put here for consistency
["call GET /json/events", 0],
@@ -76,35 +106,10 @@ if (page_params.server_sentry_dsn) {
const user_info: UserInfo = {
realm: sentry_key,
};
- if (
- sentry_key !== "www" &&
- page_params.page_type === "home" &&
- 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",
+ user_role: "Browser",
});
- if (realm !== undefined) {
- scope.setTag("server_version", realm.zulip_version);
- }
scope.setUser(user_info);
return scope;
},
diff --git a/web/src/ui_init.js b/web/src/ui_init.js
index 3cb3d42a700..2702b4148c7 100644
--- a/web/src/ui_init.js
+++ b/web/src/ui_init.js
@@ -102,6 +102,7 @@ import * as scheduled_messages_ui from "./scheduled_messages_ui";
import * as scroll_bar from "./scroll_bar";
import * as scroll_util from "./scroll_util";
import * as search from "./search";
+import * as sentry from "./sentry";
import * as server_events from "./server_events";
import * as settings from "./settings";
import * as settings_data from "./settings_data";
@@ -645,6 +646,7 @@ export function initialize_everything(state_data) {
set_current_user(current_user_params);
set_realm(realm_params);
+ sentry.initialize();
/* To store theme data for spectators, we need to initialize
user_settings before setting the theme. */