Skip to content

Commit 16d7141

Browse files
committed
DO NOT MERGE: Patch - Add sentry to long-time production tagged container
1 parent f5ba535 commit 16d7141

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

Dockerfile

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
FROM node:18.12.1-alpine3.16
2-
WORKDIR /usr/src/app
3-
COPY package*.json ./
4-
RUN npm install
5-
COPY . .
6-
RUN npm run build
7-
EXPOSE 3000
8-
CMD ["node", "-r", "source-map-support/register", "./build/src/index.js"]
1+
FROM ghcr.io/bidmcdigitalpsychiatry/lamp-server:2023.7.27
2+
3+
RUN npm install @sentry/node --save
4+
5+
ADD instrument.js instrument.js
6+
ADD tsconfig.json tsconfig.json
7+
8+
ADD src/app.ts src/app.ts
9+
ADD src/applySentry.ts src/applySentry.ts
10+
11+
RUN npx tsc --skipLibCheck
12+
13+
CMD [ "node", "-r", "instrument.js", "-r", "source-map-support/register", "./build/src/index.js"]
14+

instrument.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const Sentry = require("@sentry/node");
2+
3+
Sentry.init({
4+
dsn: "https://cc88e5b5710c7b3a3ed7aefcb8fedc68@o4505783127769088.ingest.us.sentry.io/4509763729620992",
5+
6+
// Setting this option to true will send default PII data to Sentry.
7+
// For example, automatic IP address collection on events
8+
sendDefaultPii: true,
9+
});

src/app.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express, { Application } from "express"
22
import cors from "cors"
33
import morgan from "morgan"
44
import API from "./service"
5+
import { applySentry } from "./applySentry"
56

67
const app: Application = express()
78
app.set("json spaces", 2)
@@ -16,4 +17,6 @@ app.use("/", API)
1617
app.get(["/favicon.ico", "/service-worker.js"], (req, res) => res.status(204))
1718
app.all("*", (req, res) => res.status(404).json({ message: "404.api-endpoint-unimplemented" }))
1819

19-
export default app
20+
applySentry(app)
21+
22+
export default app

src/applySentry.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as Sentry from "@sentry/node";
2+
import { Application } from "express";
3+
4+
export function applySentry(app: Application) {
5+
if (process.env.SENTRY_DSN != "") {
6+
Sentry.setupExpressErrorHandler(app);
7+
8+
// Optional fallthrough error handler
9+
app.use(function onError(err: any, req: any, res: any, next: any) {
10+
// The error id is attached to `res.sentry` to be returned
11+
// and optionally displayed to the user for support.
12+
res.statusCode = 500;
13+
res.end(res.sentry + "\n");
14+
});
15+
16+
app.get("/debug-sentry", function mainHandler(req: any, res: any) {
17+
throw new Error("My first Sentry error!");
18+
});
19+
}
20+
}

tsconfig.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
"./src",
99
"./test"
1010
],
11-
"outDir": "./build",
11+
"outDir": "./build/src",
1212
"esModuleInterop": true,
1313
"strict": true,
1414
"resolveJsonModule": true,
1515
"inlineSourceMap": true,
1616
"allowSyntheticDefaultImports": true,
17-
}
18-
}
17+
},
18+
"files": [
19+
"src/app.ts",
20+
"src/applySentry.ts"
21+
]
22+
}

0 commit comments

Comments
 (0)