-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from acmutd/internal/sentry-tracing-and-premiu…
…m-config feature/sentry-upgrades
- Loading branch information
Showing
6 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,58 @@ | ||
/** | ||
* Initialize express & setup all middleware here | ||
* Do not handle any routes in this file | ||
*/ | ||
|
||
import * as functions from "firebase-functions"; | ||
import express from "express"; | ||
import * as Sentry from "@sentry/node"; | ||
import * as Tracing from "@sentry/tracing"; | ||
import cors from "cors"; | ||
import * as bodyParser from "body-parser"; | ||
import * as functions from "firebase-functions"; | ||
import * as Sentry from "@sentry/node"; | ||
|
||
if (functions.config()?.sentry?.dns) Sentry.init({ dsn: functions.config().sentry.dns }); | ||
|
||
const app = express(); | ||
|
||
//setup sentry | ||
if (functions.config()?.sentry?.dns) | ||
Sentry.init({ | ||
dsn: functions.config().sentry.dns, | ||
integrations: [ | ||
// enable HTTP calls tracing | ||
new Sentry.Integrations.Http({ tracing: true }), | ||
// enable Express.js middleware tracing | ||
new Tracing.Integrations.Express({ app }), | ||
], | ||
tracesSampleRate: 1.0, | ||
}); | ||
|
||
// The request handler must be the first middleware on the app | ||
app.use(Sentry.Handlers.requestHandler()); | ||
// TracingHandler creates a trace for every incoming request | ||
app.use(Sentry.Handlers.tracingHandler()); | ||
|
||
app.use(cors({ origin: true })); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
// The error handler must be before any other error middleware and after all controllers | ||
app.use( | ||
Sentry.Handlers.errorHandler({ | ||
// report the error to sentry if >=400 | ||
shouldHandleError: (error) => (error.status as number) >= 400, | ||
}) | ||
); | ||
|
||
function errorHandler(error: Error, request: any, response: any, next: (err?: Error) => void) { | ||
Sentry.captureException(error); | ||
response.status(500).json({ | ||
message: "Error encountered", | ||
error: error, | ||
}); | ||
next(error); //just incase we have additional error handlers | ||
} | ||
app.use(errorHandler); | ||
|
||
// Automatically send uncaught exception errors to Sentry | ||
process.on("uncaughtException", (err) => Sentry.captureException(err)); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters