Skip to content

Commit 0f5bc4e

Browse files
Merge pull request #91 from codecov/spalmurray/sentry-tracing
Add sentry tracing to network requests
2 parents d230c35 + e284f67 commit 0f5bc4e

File tree

4 files changed

+77
-55
lines changed

4 files changed

+77
-55
lines changed

src/background/main.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,23 @@ import {
66
registerContentScript,
77
unregisterContentScriptIfExists,
88
} from "./dynamic_content_scripts";
9+
import * as Sentry from "@sentry/browser";
910

1011
async function main(): Promise<void> {
12+
Sentry.init({
13+
// @ts-ignore SENTRY_DSN is populated by Webpack at build time
14+
dsn: SENTRY_DSN,
15+
16+
integrations: [
17+
Sentry.browserTracingIntegration({
18+
// disable automatic span creation
19+
instrumentNavigation: false,
20+
instrumentPageLoad: false,
21+
}),
22+
],
23+
24+
tracesSampleRate: 1.0,
25+
});
1126
browser.runtime.onMessage.addListener(handleMessages);
1227
}
1328

@@ -16,22 +31,24 @@ async function handleMessages(message: {
1631
payload: any;
1732
referrer?: string;
1833
}) {
19-
switch (message.type) {
20-
case MessageType.FETCH_COMMIT_REPORT:
21-
return Codecov.fetchCommitReport(message.payload, message.referrer!);
22-
case MessageType.FETCH_PR_COMPARISON:
23-
return Codecov.fetchPRComparison(message.payload, message.referrer!);
24-
case MessageType.FETCH_FLAGS_LIST:
25-
return Codecov.listFlags(message.payload, message.referrer!);
26-
case MessageType.FETCH_COMPONENTS_LIST:
27-
return Codecov.listComponents(message.payload, message.referrer!);
28-
case MessageType.CHECK_AUTH:
29-
return Codecov.checkAuth(message.payload);
30-
case MessageType.REGISTER_CONTENT_SCRIPTS:
31-
return registerContentScript(message.payload);
32-
case MessageType.UNREGISTER_CONTENT_SCRIPTS:
33-
return unregisterContentScriptIfExists(message.payload);
34-
}
34+
Sentry.startSpan({ name: message.type }, async () => {
35+
switch (message.type) {
36+
case MessageType.FETCH_COMMIT_REPORT:
37+
return Codecov.fetchCommitReport(message.payload, message.referrer!);
38+
case MessageType.FETCH_PR_COMPARISON:
39+
return Codecov.fetchPRComparison(message.payload, message.referrer!);
40+
case MessageType.FETCH_FLAGS_LIST:
41+
return Codecov.listFlags(message.payload, message.referrer!);
42+
case MessageType.FETCH_COMPONENTS_LIST:
43+
return Codecov.listComponents(message.payload, message.referrer!);
44+
case MessageType.CHECK_AUTH:
45+
return Codecov.checkAuth(message.payload);
46+
case MessageType.REGISTER_CONTENT_SCRIPTS:
47+
return registerContentScript(message.payload);
48+
case MessageType.UNREGISTER_CONTENT_SCRIPTS:
49+
return unregisterContentScriptIfExists(message.payload);
50+
}
51+
});
3552
}
3653

3754
main().catch(console.log);

src/content/common/sentry.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@ import {
44
getDefaultIntegrations,
55
makeFetchTransport,
66
Scope,
7-
} from '@sentry/browser';
7+
} from "@sentry/browser";
88

99
// Sentry config
10-
// Browser extensions must initialize Sentry a bit differently to avoid
10+
// Browser extensions must initialize Sentry a bit differently to avoid
1111
// conflicts between Sentry instances should the site the extension is running
1212
// on also use Sentry. Read more here:
1313
// https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/
1414

15-
const sentryIntegrations = getDefaultIntegrations({}).filter(defaultIntegration => {
16-
return !['BrowserApiErrors', 'TryCatch', 'Breadcrumbs', 'GlobalHandlers'].includes(
17-
defaultIntegration.name
18-
);
19-
});
15+
const sentryIntegrations = getDefaultIntegrations({}).filter(
16+
(defaultIntegration) => {
17+
return ![
18+
"BrowserApiErrors",
19+
"TryCatch",
20+
"Breadcrumbs",
21+
"GlobalHandlers",
22+
].includes(defaultIntegration.name);
23+
}
24+
);
2025

2126
const sentryClient = new BrowserClient({
2227
// @ts-ignore SENTRY_DSN is populated by Webpack at build time
@@ -30,4 +35,4 @@ const Sentry = new Scope();
3035
Sentry.setClient(sentryClient);
3136
sentryClient.init();
3237

33-
export default Sentry
38+
export default Sentry;

src/content/github/file/main.tsx

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
} from "../common/fetchers";
3636
import { print } from "src/utils";
3737
import { isFileUrl } from "../common/utils";
38-
import Sentry from '../../common/sentry';
38+
import Sentry from "../../common/sentry";
3939

4040
const globals: {
4141
coverageReport?: FileCoverageReport;
@@ -47,7 +47,7 @@ const globals: {
4747
prompt?: HTMLElement;
4848
} = {};
4949

50-
init()
50+
init();
5151

5252
function init(): Promise<void> {
5353
// this event discovered by "reverse-engineering GitHub"
@@ -73,10 +73,10 @@ async function main(): Promise<void> {
7373

7474
globals.coverageButton = createCoverageButton();
7575

76-
process(metadata)
76+
process(metadata);
7777
} catch (e) {
78-
Sentry.captureException(e)
79-
throw e
78+
Sentry.captureException(e);
79+
throw e;
8080
}
8181
}
8282

@@ -111,17 +111,16 @@ async function process(metadata: FileMetadata): Promise<void> {
111111
previousElement: globals.coverageButton!,
112112
selectedOptions: selectedFlags,
113113
onClick: handleFlagClick,
114-
})
115-
.then(({ button, list }) => {
116-
globals.flagsButton = button;
117-
globals.flagsDrop = new Drop({
118-
target: button,
119-
content: list,
120-
classes: "drop-theme-arrows codecov-z1 codecov-bg-white",
121-
position: "bottom right",
122-
openOn: "click",
123-
});
124-
})
114+
}).then(({ button, list }) => {
115+
globals.flagsButton = button;
116+
globals.flagsDrop = new Drop({
117+
target: button,
118+
content: list,
119+
classes: "drop-theme-arrows codecov-z1 codecov-bg-white",
120+
position: "bottom right",
121+
openOn: "click",
122+
});
123+
});
125124
}
126125

127126
const components = await getComponents(metadata);
@@ -151,17 +150,16 @@ async function process(metadata: FileMetadata): Promise<void> {
151150
previousElement: globals.coverageButton!,
152151
onClick: handleComponentClick,
153152
selectedOptions: selectedComponents,
154-
})
155-
.then(({ button, list }) => {
156-
globals.componentsButton = button;
157-
globals.componentsDrop = new Drop({
158-
target: button,
159-
content: list,
160-
classes: "drop-theme-arrows codecov-z1 codecov-bg-white",
161-
position: "bottom right",
162-
openOn: "click",
163-
});
164-
})
153+
}).then(({ button, list }) => {
154+
globals.componentsButton = button;
155+
globals.componentsDrop = new Drop({
156+
target: button,
157+
content: list,
158+
classes: "drop-theme-arrows codecov-z1 codecov-bg-white",
159+
position: "bottom right",
160+
openOn: "click",
161+
});
162+
});
165163
}
166164

167165
let coverageReportResponses: Array<FileCoverageReportResponse>;
@@ -241,7 +239,9 @@ async function promptPastReport(metadata: FileMetadata): Promise<void> {
241239
const matches = regexp.exec(response.commit_file_url);
242240
const commit = matches?.groups?.commit;
243241
if (!commit) {
244-
throw new Error("Could not parse commit hash from response for past coverage report")
242+
throw new Error(
243+
"Could not parse commit hash from response for past coverage report"
244+
);
245245
}
246246
const link = document.URL.replace(
247247
`blob/${metadata.branch}`,
@@ -260,7 +260,7 @@ function createPrompt(child: any) {
260260
const ref = document.querySelector('[data-testid="latest-commit"]')
261261
?.parentElement?.parentElement;
262262
if (!ref) {
263-
throw new Error("Could not find reference element to render prompt")
263+
throw new Error("Could not find reference element to render prompt");
264264
}
265265
const prompt = <div className="codecov-mb2 codecov-mx1">{child}</div>;
266266
return ref.insertAdjacentElement("afterend", prompt) as HTMLElement;

webpack/webpack.common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = {
5050
browser: "webextensions-polyfill",
5151
}),
5252
new DefinePlugin({
53-
SENTRY_DSN: process.env.SENTRY_DSN
54-
})
53+
SENTRY_DSN: JSON.stringify(process.env.SENTRY_DSN),
54+
}),
5555
],
5656
};

0 commit comments

Comments
 (0)