about summary refs log tree commit diff stats
path: root/src/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.ts')
-rw-r--r--src/index.ts52
1 files changed, 45 insertions, 7 deletions
diff --git a/src/index.ts b/src/index.ts
index 98958d1..f4279c8 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -6,7 +6,9 @@ import prom from "bun-prometheus-client";
 
 import readConfig from "./config";
 
-Sentry.init({});
+Sentry.init({
+  tracesSampleRate: 1.0,
+});
 
 const base = ".";
 const publicDir = path.resolve(base, "public") + path.sep;
@@ -156,25 +158,48 @@ console.info(
 
 const server = Bun.serve({
   fetch: async function (request) {
-    const pathname = new URL(request.url).pathname;
+    const pathname = new URL(request.url).pathname.replace(/\/\/+/g, "/");
     const endTimer = metrics.requestDuration.startTimer({ path: pathname });
+    const transaction = Sentry.startTransaction({
+      name: pathname,
+      op: "http.server",
+      description: `${request.method} ${pathname}`,
+      data: {
+        url: request.url,
+        "http.method": request.method,
+      },
+      tags: {
+        region: Bun.env.FLY_APP_REGION,
+      },
+    });
+    const span = transaction.startChild({
+      op: "http.server",
+      description: `${request.method} ${pathname}`,
+      data: {
+        url: request.url,
+        "http.method": request.method,
+      },
+    });
     try {
       const file = files.get(pathname);
       metrics.requestsByPath.inc({ path: pathname });
-      if (file) {
+      if (file && (await file.handle.exists())) {
         if (
           parseIfModifiedSinceHeader(
             request.headers.get("if-modified-since"),
           ) >= file?.mtime.getTime()
         ) {
           metrics.requestsByStatus.inc({ status_code: 304 });
+          span.setTag("http.status_code", 304);
           return new Response("", { status: 304, headers: defaultHeaders });
         }
         const encodings = (request.headers.get("accept-encoding") || "")
           .split(",")
           .map((x) => x.trim().toLowerCase());
         if (encodings.includes("br") && files.has(file.relPath + ".br")) {
-          return serveEncodedFile(files.get(file.relPath + ".br"), 200, {
+          span.setTag("http.encoding", "br");
+          span.setTag("http.status_code", 200);
+          return serveFile(files.get(file.relPath + ".br"), 200, {
             "content-encoding": "br",
             "content-type": file.handle.type,
           });
@@ -182,7 +207,9 @@ const server = Bun.serve({
           encodings.includes("zstd") &&
           files.has(file.relPath + ".zst")
         ) {
-          return serveEncodedFile(files.get(file.relPath + ".zst"), 200, {
+          span.setTag("http.encoding", "zstd");
+          span.setTag("http.status_code", 200);
+          return serveFile(files.get(file.relPath + ".zst"), 200, {
             "content-encoding": "zstd",
             "content-type": file.handle.type,
           });
@@ -190,20 +217,31 @@ const server = Bun.serve({
           encodings.includes("gzip") &&
           files.has(file.relPath + ".gz")
         ) {
-          return serveEncodedFile(files.get(file.relPath + ".gz"), 200, {
+          span.setTag("http.encoding", "gzip");
+          span.setTag("http.status_code", 200);
+          return serveFile(files.get(file.relPath + ".gz"), 200, {
             "content-encoding": "gzip",
             "content-type": file.handle.type,
           });
         }
+        span.setTag("http.encoding", "identity");
+        span.setTag("http.status_code", 200);
+        return serveFile(file);
+      } else {
+        metrics.requestsByStatus.inc({ status_code: 404 });
+        transaction.setTag("http.status_code", 404);
+        return serveFile(files.get("/404.html"), 404);
       }
-      return serveFile(file);
     } catch (error) {
+      span.setTag("http.status_code", 503);
       metrics.requestsByStatus.inc({ status_code: 503 });
       Sentry.captureException(error);
       return new Response("Something went wrong", { status: 503 });
     } finally {
       const seconds = endTimer();
       metrics.requestDuration.observe(seconds);
+      span.finish();
+      transaction.finish();
     }
   },
 });