feat(api): create sentry plugin (#49731)

* feat(api): add sentry plugin

Apply suggestions from code review

Revert "feat(api): add sentry plugin"

This reverts commit fcde4ee03e9b83e335a6a2bccd490490e9993597.

install sentryNode

WIP: create sentry debug

WIP: find out why use errorhandler isn't typed correct

install sentry

add the deleted sentry code

create sentry plugin

* fix error found through sentry

* Polish sentry plugin

Co-authored-by: Niraj Nandish <nirajnandish@icloud.com>

* duplicate the changes made in the other plugin

* add done to seterrorHandler

* Fix a typo in sentry option

Co-authored-by: Naomi Carrigan <nhcarrigan@gmail.com>

* Stop the dns from running if a DSN wasn't provided

Co-authored-by: Naomi Carrigan <nhcarrigan@gmail.com>

* Polish the function and check the variable value

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* check the dsn dashboard in the env

* export dsn value if it isn't sentrydashboard

Co-authored-by: Naomi Carrigan <nhcarrigan@gmail.com>

* when the value is undefined init errors

* revert the if statement

* throw an error whenever an environment variable is not right

---------

Co-authored-by: Niraj Nandish <nirajnandish@icloud.com>
Co-authored-by: Naomi Carrigan <nhcarrigan@gmail.com>
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Muhammed Mustafa
2023-04-05 17:35:23 +02:00
committed by GitHub
parent 162336365b
commit 1d8e9fb0b7
5 changed files with 39 additions and 1 deletions
+1
View File
@@ -10,6 +10,7 @@
"@fastify/swagger": "^8.3.1",
"@fastify/swagger-ui": "^1.5.0",
"@prisma/client": "4.11.0",
"@sentry/node": "7.37.1",
"connect-mongo": "4.6.0",
"fastify": "4.15.0",
"fastify-auth0-verify": "^1.0.0",
+6 -1
View File
@@ -14,6 +14,7 @@ import MongoStore from 'connect-mongo';
import type { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';
import fastifySentry from './plugins/fastify-sentry';
import jwtAuthz from './plugins/fastify-jwt-authz';
import sessionAuth from './plugins/session-auth';
@@ -31,7 +32,8 @@ import {
SESSION_SECRET,
FCC_ENABLE_SWAGGER_UI,
API_LOCATION,
FCC_ENABLE_DEV_LOGIN_MODE
FCC_ENABLE_DEV_LOGIN_MODE,
SENTRY_DSN
} from './utils/env';
export type FastifyInstanceWithTypeProvider = FastifyInstance<
@@ -52,6 +54,9 @@ export const build = async (
});
// NOTE: Awaited to ensure `.use` is registered on `fastify`
await fastify.register(middie);
if (SENTRY_DSN) {
await fastify.register(fastifySentry, { dsn: SENTRY_DSN });
}
await fastify.register(fastifyCookie);
// @ts-expect-error - @fastify/session's types are not, yet, compatible with
// express-session's types
+19
View File
@@ -0,0 +1,19 @@
import { init, captureException } from '@sentry/node';
import { FastifyPluginCallback } from 'fastify';
import fp from 'fastify-plugin';
const fastifySentry: FastifyPluginCallback<{ dsn: string }> = (
fastify,
options,
done
) => {
init(options);
fastify.setErrorHandler((error, request) => {
captureException(error);
request.log.error(error);
});
done();
};
export default fp(fastifySentry);
+10
View File
@@ -34,6 +34,12 @@ assert.ok(process.env.FCC_ENABLE_DEV_LOGIN_MODE);
if (process.env.FREECODECAMP_NODE_ENV !== 'development') {
assert.ok(process.env.PORT);
assert.ok(process.env.MONGOHQ_URL);
assert.ok(process.env.SENTRY_DSN);
assert.notEqual(
process.env.SENTRY_DSN,
'dsn_from_sentry_dashboard',
`The DSN from Sentry's dashboard should be used.`
);
assert.notEqual(
process.env.SESSION_SECRET,
'a_thirty_two_plus_character_session_secret',
@@ -58,3 +64,7 @@ export const FCC_ENABLE_SWAGGER_UI =
process.env.FCC_ENABLE_SWAGGER_UI === 'true';
export const FCC_ENABLE_DEV_LOGIN_MODE =
process.env.FCC_ENABLE_DEV_LOGIN_MODE === 'true';
export const SENTRY_DSN =
process.env.SENTRY_DSN === 'dsn_from_sentry_dashboard'
? ''
: process.env.SENTRY_DSN;