Skip to content

Commit

Permalink
Merge pull request #91 from codecov/spalmurray/sentry-tracing
Browse files Browse the repository at this point in the history
Add sentry tracing to network requests
  • Loading branch information
spalmurray-codecov authored Nov 7, 2024
2 parents d230c35 + e284f67 commit 0f5bc4e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 55 deletions.
49 changes: 33 additions & 16 deletions src/background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ import {
registerContentScript,
unregisterContentScriptIfExists,
} from "./dynamic_content_scripts";
import * as Sentry from "@sentry/browser";

async function main(): Promise<void> {
Sentry.init({
// @ts-ignore SENTRY_DSN is populated by Webpack at build time
dsn: SENTRY_DSN,

integrations: [
Sentry.browserTracingIntegration({
// disable automatic span creation
instrumentNavigation: false,
instrumentPageLoad: false,
}),
],

tracesSampleRate: 1.0,
});
browser.runtime.onMessage.addListener(handleMessages);
}

Expand All @@ -16,22 +31,24 @@ async function handleMessages(message: {
payload: any;
referrer?: string;
}) {
switch (message.type) {
case MessageType.FETCH_COMMIT_REPORT:
return Codecov.fetchCommitReport(message.payload, message.referrer!);
case MessageType.FETCH_PR_COMPARISON:
return Codecov.fetchPRComparison(message.payload, message.referrer!);
case MessageType.FETCH_FLAGS_LIST:
return Codecov.listFlags(message.payload, message.referrer!);
case MessageType.FETCH_COMPONENTS_LIST:
return Codecov.listComponents(message.payload, message.referrer!);
case MessageType.CHECK_AUTH:
return Codecov.checkAuth(message.payload);
case MessageType.REGISTER_CONTENT_SCRIPTS:
return registerContentScript(message.payload);
case MessageType.UNREGISTER_CONTENT_SCRIPTS:
return unregisterContentScriptIfExists(message.payload);
}
Sentry.startSpan({ name: message.type }, async () => {
switch (message.type) {
case MessageType.FETCH_COMMIT_REPORT:
return Codecov.fetchCommitReport(message.payload, message.referrer!);
case MessageType.FETCH_PR_COMPARISON:
return Codecov.fetchPRComparison(message.payload, message.referrer!);
case MessageType.FETCH_FLAGS_LIST:
return Codecov.listFlags(message.payload, message.referrer!);
case MessageType.FETCH_COMPONENTS_LIST:
return Codecov.listComponents(message.payload, message.referrer!);
case MessageType.CHECK_AUTH:
return Codecov.checkAuth(message.payload);
case MessageType.REGISTER_CONTENT_SCRIPTS:
return registerContentScript(message.payload);
case MessageType.UNREGISTER_CONTENT_SCRIPTS:
return unregisterContentScriptIfExists(message.payload);
}
});
}

main().catch(console.log);
21 changes: 13 additions & 8 deletions src/content/common/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ import {
getDefaultIntegrations,
makeFetchTransport,
Scope,
} from '@sentry/browser';
} from "@sentry/browser";

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

const sentryIntegrations = getDefaultIntegrations({}).filter(defaultIntegration => {
return !['BrowserApiErrors', 'TryCatch', 'Breadcrumbs', 'GlobalHandlers'].includes(
defaultIntegration.name
);
});
const sentryIntegrations = getDefaultIntegrations({}).filter(
(defaultIntegration) => {
return ![
"BrowserApiErrors",
"TryCatch",
"Breadcrumbs",
"GlobalHandlers",
].includes(defaultIntegration.name);
}
);

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

export default Sentry
export default Sentry;
58 changes: 29 additions & 29 deletions src/content/github/file/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
} from "../common/fetchers";
import { print } from "src/utils";
import { isFileUrl } from "../common/utils";
import Sentry from '../../common/sentry';
import Sentry from "../../common/sentry";

const globals: {
coverageReport?: FileCoverageReport;
Expand All @@ -47,7 +47,7 @@ const globals: {
prompt?: HTMLElement;
} = {};

init()
init();

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

globals.coverageButton = createCoverageButton();

process(metadata)
process(metadata);
} catch (e) {
Sentry.captureException(e)
throw e
Sentry.captureException(e);
throw e;
}
}

Expand Down Expand Up @@ -111,17 +111,16 @@ async function process(metadata: FileMetadata): Promise<void> {
previousElement: globals.coverageButton!,
selectedOptions: selectedFlags,
onClick: handleFlagClick,
})
.then(({ button, list }) => {
globals.flagsButton = button;
globals.flagsDrop = new Drop({
target: button,
content: list,
classes: "drop-theme-arrows codecov-z1 codecov-bg-white",
position: "bottom right",
openOn: "click",
});
})
}).then(({ button, list }) => {
globals.flagsButton = button;
globals.flagsDrop = new Drop({
target: button,
content: list,
classes: "drop-theme-arrows codecov-z1 codecov-bg-white",
position: "bottom right",
openOn: "click",
});
});
}

const components = await getComponents(metadata);
Expand Down Expand Up @@ -151,17 +150,16 @@ async function process(metadata: FileMetadata): Promise<void> {
previousElement: globals.coverageButton!,
onClick: handleComponentClick,
selectedOptions: selectedComponents,
})
.then(({ button, list }) => {
globals.componentsButton = button;
globals.componentsDrop = new Drop({
target: button,
content: list,
classes: "drop-theme-arrows codecov-z1 codecov-bg-white",
position: "bottom right",
openOn: "click",
});
})
}).then(({ button, list }) => {
globals.componentsButton = button;
globals.componentsDrop = new Drop({
target: button,
content: list,
classes: "drop-theme-arrows codecov-z1 codecov-bg-white",
position: "bottom right",
openOn: "click",
});
});
}

let coverageReportResponses: Array<FileCoverageReportResponse>;
Expand Down Expand Up @@ -241,7 +239,9 @@ async function promptPastReport(metadata: FileMetadata): Promise<void> {
const matches = regexp.exec(response.commit_file_url);
const commit = matches?.groups?.commit;
if (!commit) {
throw new Error("Could not parse commit hash from response for past coverage report")
throw new Error(
"Could not parse commit hash from response for past coverage report"
);
}
const link = document.URL.replace(
`blob/${metadata.branch}`,
Expand All @@ -260,7 +260,7 @@ function createPrompt(child: any) {
const ref = document.querySelector('[data-testid="latest-commit"]')
?.parentElement?.parentElement;
if (!ref) {
throw new Error("Could not find reference element to render prompt")
throw new Error("Could not find reference element to render prompt");
}
const prompt = <div className="codecov-mb2 codecov-mx1">{child}</div>;
return ref.insertAdjacentElement("afterend", prompt) as HTMLElement;
Expand Down
4 changes: 2 additions & 2 deletions webpack/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = {
browser: "webextensions-polyfill",
}),
new DefinePlugin({
SENTRY_DSN: process.env.SENTRY_DSN
})
SENTRY_DSN: JSON.stringify(process.env.SENTRY_DSN),
}),
],
};

0 comments on commit 0f5bc4e

Please sign in to comment.