Skip to content

Commit

Permalink
Merge pull request #25 from acmutd/internal/sentry-tracing-and-premiu…
Browse files Browse the repository at this point in the history
…m-config

feature/sentry-upgrades
  • Loading branch information
WillieCubed authored Nov 11, 2020
2 parents b79ca2b + 7592805 commit b824d41
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 6 deletions.
48 changes: 48 additions & 0 deletions functions/package-lock.json

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

1 change: 1 addition & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@mailchimp/mailchimp_marketing": "^3.0.12",
"@sendgrid/mail": "^7.2.3",
"@sentry/node": "^5.19.2",
"@sentry/tracing": "^5.25.0",
"axios": "^0.19.2",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
Expand Down
51 changes: 47 additions & 4 deletions functions/src/express_configs/express_open.ts
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;
28 changes: 26 additions & 2 deletions functions/src/express_configs/express_secure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,48 @@ import express from "express";
import jwt from "express-jwt";
import jwksRsa from "jwks-rsa";
import * as Sentry from "@sentry/node";
import * as Tracing from "@sentry/tracing";
import cors from "cors";
import * as bodyParser from "body-parser";
import { Response, Request } from "express";

const app = express();

//setup sentry
if (functions.config()?.sentry?.dns) Sentry.init({ dsn: functions.config().sentry.dns });
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());
app.use(
Sentry.Handlers.errorHandler({
shouldHandleError(error) {
// Capture all errors over 400
if ((error.status as number) >= 400) {
return true;
}
return false;
},
})
);

function errorHandler(error: Error, request: Request, response: Response, next: (err?: Error) => void) {
Sentry.captureException(error);
Expand Down
7 changes: 7 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as eventFunctions from "./events/events";
import * as vanityFunctions from "./custom/vanity";
import * as hacktoberfestFunctions from "./custom/hacktoberfest";
import * as typeformFunctions from "./application/typeform";
import * as errorFunctions from "./services/errorService";

//this will match every call made to this api.
app_secure.all("/", (request, response, next) => {
Expand Down Expand Up @@ -99,6 +100,12 @@ app_open.delete("/tags/:tag/:token", challengeFunctions.deleteTag);
*/
app_open.post("/typeform", typeformFunctions.typeform_webhook);

/**
* Debugging endpoints
*/
app_secure.get("/debug-sentry", errorFunctions.debug_sentry);
app_open.get("/debug-sentry", errorFunctions.debug_sentry);

/**
* htf-development retrieval
*/
Expand Down
15 changes: 15 additions & 0 deletions functions/src/services/ErrorService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */

import * as Sentry from "@sentry/node";
export default class ErrorService {
static generatePostError<T>(reqObj: any, exampleObj: T) {
const response = { error: { message: "You are missing the " } };
Expand All @@ -25,3 +27,16 @@ export default class ErrorService {
return response;
}
}

export const debug_sentry = (request: any, response: any) => {
try {
throw new Error("My first Sentry error!");
} catch (error) {
console.log("error recording");
Sentry.captureException(error);
Sentry.flush(2000);
}
response.json({
error: "Sentry debug error",
});
};

0 comments on commit b824d41

Please sign in to comment.