Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

realm: Add Sentry #1084

Merged
merged 18 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/host/app/lib/current-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
type SerializedError,
} from '@cardstack/runtime-common/error';
import { RealmPaths, LocalPath } from '@cardstack/runtime-common/paths';
import { reportError } from '@cardstack/runtime-common/realm';
import {
isIgnored,
type Reader,
Expand Down Expand Up @@ -475,9 +476,9 @@ export class CurrentRun {
deferred.reject(err);
throw err;
}
log.warn(
`encountered error indexing card instance ${path}: ${error.error.detail}`,
);
let warning = `encountered error indexing card instance ${path}: ${error.error.detail}`;
log.warn(warning);
reportError(new Error(warning));
this.setInstance(instanceURL, error);
deferred.fulfill();
}
Expand Down
8 changes: 8 additions & 0 deletions packages/realm-server/fastboot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type RunnerOpts,
} from '@cardstack/runtime-common/search-index';
import { JSDOM } from 'jsdom';
import { type ErrorReporter } from '@cardstack/runtime-common/realm';

const appName = '@cardstack/host';
export async function makeFastBootIndexRunner(
Expand All @@ -15,13 +16,19 @@ export async function makeFastBootIndexRunner(
): Promise<{ getRunner: IndexRunner; distPath: string }> {
let fastboot: FastBootInstance;
let distPath: string;

let globalWithErrorReporter = global as typeof globalThis & {
__boxelErrorReporter: ErrorReporter;
};

if (typeof dist === 'string') {
distPath = dist;
fastboot = new FastBoot({
distPath,
resilient: false,
buildSandboxGlobals(defaultGlobals: any) {
return Object.assign({}, defaultGlobals, {
__boxelErrorReporter: globalWithErrorReporter.__boxelErrorReporter,
URL: globalThis.URL,
Request: globalThis.Request,
Response: globalThis.Response,
Expand All @@ -38,6 +45,7 @@ export async function makeFastBootIndexRunner(
dist,
(defaultGlobals: any) => {
return Object.assign({}, defaultGlobals, {
__boxelErrorReporter: globalWithErrorReporter.__boxelErrorReporter,
URL: globalThis.URL,
Request: globalThis.Request,
Response: globalThis.Response,
Expand Down
20 changes: 18 additions & 2 deletions packages/realm-server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,27 @@ import { RunnerOptionsManager } from '@cardstack/runtime-common/search-index';
import { readFileSync } from 'fs-extra';
import { shimExternals } from './lib/externals';
import { type RealmPermissions as RealmPermissionsInterface } from '@cardstack/runtime-common/realm';
import * as Sentry from '@sentry/node';
import { setErrorReporter } from '@cardstack/runtime-common/realm';

import fs from 'fs';

let log = logger('main');

if (process.env.REALM_SENTRY_DSN) {
log.info('Setting up Sentry.');
Sentry.init({
dsn: process.env.REALM_SENTRY_DSN,
environment: process.env.REALM_SENTRY_ENVIRONMENT || 'development',
});

setErrorReporter(Sentry.captureException);
} else {
log.warn(
`No REALM_SENTRY_DSN environment variable found, skipping Sentry setup.`,
);
}

const REALM_SECRET_SEED = process.env.REALM_SECRET_SEED;
if (!REALM_SECRET_SEED) {
console.error(
Expand Down Expand Up @@ -112,8 +130,6 @@ if (
process.exit(-1);
}

let log = logger('main');

let loader = new Loader();
shimExternals(loader);

Expand Down
3 changes: 3 additions & 0 deletions packages/realm-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,8 @@
},
"volta": {
"extends": "../../package.json"
},
"dependencies": {
"@sentry/node": "^7.105.0"
}
}
12 changes: 12 additions & 0 deletions packages/realm-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import './lib/externals';
import { nodeStreamToText } from './stream';
import mime from 'mime-types';
import { extractSupportedMimeType } from '@cardstack/runtime-common/router';
import * as Sentry from '@sentry/node';

interface Options {
assetsURL?: URL;
Expand Down Expand Up @@ -96,6 +97,13 @@ export class RealmServer {
.use(router.routes())
.use(this.serveFromRealm);

app.on('error', (err, ctx) => {
Sentry.withScope((scope) => {
scope.setSDKProcessingMetadata({ request: ctx.request });
Sentry.captureException(err);
});
});

return app;
}

Expand All @@ -119,6 +127,10 @@ export class RealmServer {
}

private serveFromRealm = async (ctxt: Koa.Context, _next: Koa.Next) => {
if (ctxt.request.path === '/_boom') {
throw new Error('boom');
}

let realm = this.realms.find((r) => {
let reversedResolution = r.loader.reverseResolution(
fullRequestURL(ctxt).href,
Expand Down
16 changes: 16 additions & 0 deletions packages/runtime-common/realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,22 @@ function lastModifiedHeader(
) as {} | { 'last-modified': string };
}

export type ErrorReporter = (error: Error) => void;

let globalWithErrorReporter = global as typeof globalThis & {
__boxelErrorReporter: ErrorReporter;
};

export function setErrorReporter(reporter: ErrorReporter) {
globalWithErrorReporter.__boxelErrorReporter = reporter;
}

export function reportError(error: Error) {
if (globalWithErrorReporter.__boxelErrorReporter) {
globalWithErrorReporter.__boxelErrorReporter(error);
}
}

export interface CardDefinitionResource {
id: string;
type: 'card-definition';
Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading