Skip to content

Commit

Permalink
sentry: Extract functions used by channel into a util file.
Browse files Browse the repository at this point in the history
This is to make tests for channel work.

Channel mocks `$` to be an object, so it cannot be called as an
function in sentry.ts if it is imported in channel.ts.

So, to avoid it, we extract the functions which channel.ts uses
and fortunately they don't use `$`, so it works out.
  • Loading branch information
amanagr committed Oct 25, 2023
1 parent 3ea1dac commit 2ae4e5c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
1 change: 1 addition & 0 deletions tools/test-js-with-node
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ EXEMPT_FILES = make_set(
"web/src/search.js",
"web/src/sent_messages.ts",
"web/src/sentry.ts",
"web/src/sentry_util.ts",
"web/src/server_events.js",
"web/src/settings.js",
"web/src/settings_account.js",
Expand Down
6 changes: 3 additions & 3 deletions web/src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import _ from "lodash";
import * as blueslip from "./blueslip";
import {page_params} from "./page_params";
import * as reload_state from "./reload_state";
import {normalize_path, shouldCreateSpanForRequest} from "./sentry";
import * as sentry_util from "./sentry_util";
import * as spectators from "./spectators";

// We omit `success` handler from original `AjaxSettings` type because it types
Expand Down Expand Up @@ -46,7 +46,7 @@ function call(args: AjaxRequestHandlerOptions): JQuery.jqXHR<unknown> | undefine
}

const existing_span = Sentry.getCurrentHub().getScope().getSpan();
const txn_title = `call ${args.type} ${normalize_path(args.url)}`;
const txn_title = `call ${args.type} ${sentry_util.normalize_path(args.url)}`;
const span_data = {
op: "function",
description: txn_title,
Expand All @@ -56,7 +56,7 @@ function call(args: AjaxRequestHandlerOptions): JQuery.jqXHR<unknown> | undefine
},
};
let span: Sentry.Span | undefined;
if (!shouldCreateSpanForRequest(args.url)) {
if (!sentry_util.shouldCreateSpanForRequest(args.url)) {
// Leave the span unset, so we don't record a transaction
} else {
if (!existing_span) {
Expand Down
27 changes: 4 additions & 23 deletions web/src/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as Sentry from "@sentry/browser";
import $ from "jquery";
import _ from "lodash";

import * as sentry_util from "./sentry_util";

const page_params: {
is_admin: boolean;
is_guest: boolean;
Expand All @@ -24,27 +26,6 @@ type UserInfo = {
role?: string;
};

export function normalize_path(path: string, is_portico = false): string {
if (path === undefined) {
return "unknown";
}
path = path
.replace(/\/\d+(\/|$)/, "/*$1")
.replace(
/^\/(join|reactivate|new|accounts\/do_confirm|accounts\/confirm_new_email)\/[^/]+(\/?)$/,
"$1/*$2",
);
if (is_portico) {
return "portico: " + path;
}
return path;
}

export function shouldCreateSpanForRequest(url: string): boolean {
const parsed = new URL(url, window.location.href);
return parsed.pathname !== "/json/events";
}

if (page_params.server_sentry_dsn) {
const url_matches = [/^\//];
if (document.currentScript instanceof HTMLScriptElement) {
Expand Down Expand Up @@ -106,10 +87,10 @@ if (page_params.server_sentry_dsn) {
return {
...context,
metadata: {source: "custom"},
name: normalize_path(location.pathname, sentry_key === "www"),
name: sentry_util.normalize_path(location.pathname, sentry_key === "www"),
};
},
shouldCreateSpanForRequest,
shouldCreateSpanForRequest: sentry_util.shouldCreateSpanForRequest,
}),
],
allowUrls: url_matches,
Expand Down
20 changes: 20 additions & 0 deletions web/src/sentry_util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function normalize_path(path: string, is_portico = false): string {
if (path === undefined) {
return "unknown";
}
path = path
.replace(/\/\d+(\/|$)/, "/*$1")
.replace(
/^\/(join|reactivate|new|accounts\/do_confirm|accounts\/confirm_new_email)\/[^/]+(\/?)$/,
"$1/*$2",
);
if (is_portico) {
return "portico: " + path;
}
return path;
}

export function shouldCreateSpanForRequest(url: string): boolean {
const parsed = new URL(url, window.location.href);
return parsed.pathname !== "/json/events";
}

0 comments on commit 2ae4e5c

Please sign in to comment.