Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions lobby_worker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="apple-mobile-web-app-title" content="Pongo" />
<script src="sentry-config.js"></script>
<script src="sentry.js" defer></script>
</head>
<body>
<h1 class="game-title">PONGO</h1>
Expand Down
6 changes: 6 additions & 0 deletions lobby_worker/sentry-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
window.TRE_STATIC_SENTRY_CONFIG = {
app: "pongo",
dsn: "",
environment: "production",
release: "",
};
37 changes: 37 additions & 0 deletions lobby_worker/sentry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(() => {
const config = window.TRE_STATIC_SENTRY_CONFIG;
if (!config?.dsn) return;

const script = document.createElement("script");
script.src = "https://browser.sentry-cdn.com/10.57.0/bundle.min.js";
script.crossOrigin = "anonymous";
script.onload = () => {
if (!window.Sentry) return;
window.Sentry.init({
dsn: config.dsn,
environment: config.environment || "production",
release: config.release,
sendDefaultPii: false,
tracesSampleRate: config.environment === "production" ? 0.01 : 0,
replaysSessionSampleRate: 0,
replaysOnErrorSampleRate: 0,
beforeSend(event) {
if (event.request) {
delete event.request.cookies;
delete event.request.data;
if (event.request.headers) {
for (const key of Object.keys(event.request.headers)) {
const lowerKey = key.toLowerCase();
if (lowerKey.includes("authorization") || lowerKey.includes("cookie")) {
event.request.headers[key] = "[Filtered]";
}
}
}
}
event.tags = { ...event.tags, app: config.app || "pongo" };
return event;
},
});
};
document.head.appendChild(script);
})();
42 changes: 42 additions & 0 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"test:all": "prettier --check . && cargo fmt --check && cargo check --workspace && cargo clippy --workspace -- -D warnings && cargo test --workspace && npm run check:diagrams",
"format": "prettier --write .",
"build:client": "cd client_wasm && wasm-pack build --target web --out-dir ../worker/pkg/client_wasm",
"build:server": "cd lobby_worker && wasm-pack build --target web --out-dir ../worker/pkg && cp index.html style.css *.js manifest.webmanifest ../worker/pkg/ && cp -r icons ../worker/pkg/",
"build:server": "node scripts/write-sentry-config.mjs && cd lobby_worker && wasm-pack build --target web --out-dir ../worker/pkg && cp index.html style.css *.js manifest.webmanifest ../worker/pkg/ && cp -r icons ../worker/pkg/",
"build": "npm run build:server && npm run build:client && node scripts/stamp-sw.mjs",
"dev": "npx wrangler dev --assets worker/pkg",
"deploy": "npx wrangler deploy",
Expand All @@ -32,5 +32,8 @@
"lint-staged": {
"*.rs": "rustfmt",
"**/*": "prettier --write --ignore-unknown"
},
"dependencies": {
"@sentry/cloudflare": "^10.57.0"
}
}
24 changes: 24 additions & 0 deletions scripts/write-sentry-config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { writeFileSync } from "node:fs";
import { execSync } from "node:child_process";

function fallbackRelease() {
try {
return execSync("git rev-parse --short HEAD", { encoding: "utf8" }).trim();
} catch {
return String(Date.now());
}
}

writeFileSync(
"lobby_worker/sentry-config.js",
`window.TRE_STATIC_SENTRY_CONFIG = ${JSON.stringify(
{
app: "pongo",
dsn: process.env.SENTRY_DSN || "",
environment: process.env.SENTRY_ENVIRONMENT || "production",
release: process.env.SENTRY_RELEASE || process.env.GITHUB_SHA || fallbackRelease(),
},
null,
2
)};\n`
);
43 changes: 41 additions & 2 deletions worker/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as Sentry from "@sentry/cloudflare";
import init, { fetch as wasmFetch, MatchDO as WasmMatchDO } from "./pkg/lobby_worker.js";
import wasmUrl from "./pkg/lobby_worker_bg.wasm";

Expand All @@ -7,7 +8,34 @@ async function ensureInit() {
await initPromise;
}

export default {
const sentryOptions = (env) => {
if (!env.SENTRY_DSN) return undefined;
return {
dsn: env.SENTRY_DSN,
environment: env.SENTRY_ENVIRONMENT || "production",
release: env.SENTRY_RELEASE,
sendDefaultPii: false,
tracesSampleRate: env.SENTRY_ENVIRONMENT === "production" ? 0.01 : 0,
enableRpcTracePropagation: true,
beforeSend(event) {
if (event.request) {
delete event.request.cookies;
delete event.request.data;
if (event.request.headers) {
for (const key of Object.keys(event.request.headers)) {
const lowerKey = key.toLowerCase();
if (lowerKey.includes("authorization") || lowerKey.includes("cookie")) {
event.request.headers[key] = "[Filtered]";
}
}
}
}
return event;
},
};
};

const handler = {
async fetch(req, env, ctx) {
try {
await ensureInit();
Expand All @@ -23,6 +51,8 @@ export default {
},
};

export default Sentry.withSentry(sentryOptions, handler);

// The generated WasmMatchDO constructor is synchronous and needs the wasm
// module ready. The DO runtime can construct it before the top-level init()
// resolves, so defer construction behind ensureInit().
Expand All @@ -47,4 +77,13 @@ class MatchDOWrapper {
}
}

export { MatchDOWrapper as MatchDO };
export const MatchDO = Sentry.instrumentDurableObjectWithSentry(
(env) =>
sentryOptions(env) || {
environment: env.SENTRY_ENVIRONMENT || "production",
sendDefaultPii: false,
tracesSampleRate: 0,
enableRpcTracePropagation: true,
},
MatchDOWrapper
);
6 changes: 6 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name = "pongo"
main = "worker/index.js"
compatibility_date = "2024-01-01"
compatibility_flags = ["nodejs_als"]

[vars]
SENTRY_ENVIRONMENT = "production"
SENTRY_PROJECT = "pongo"

[build]
# Mirror `npm run build` so a standalone `wrangler deploy` produces a complete worker/pkg.
Expand All @@ -10,6 +15,7 @@ command = "npm run build"
directory = "./worker/pkg"
binding = "ASSETS"
html_handling = "auto-trailing-slash"
not_found_handling = "404-page"

[durable_objects]
bindings = [{ name = "MATCH", class_name = "MatchDO" }]
Expand Down