feat(tools): sentry apm and other telemetry (#49385)

* feat(tools): sentry apm and other telemetry

* fix: use middleware json to remove duplication

---------

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Mrugesh Mohapatra
2023-02-17 18:22:00 +05:30
committed by Mrugesh Mohapatra
parent c29ecaf14f
commit 537ef39a8d
5 changed files with 193 additions and 25 deletions
+2 -1
View File
@@ -29,7 +29,8 @@
},
"dependencies": {
"@freecodecamp/loopback-component-passport": "1.2.0",
"@sentry/node": "6.19.7",
"@sentry/node": "7.37.1",
"@sentry/tracing": "7.37.1",
"accepts": "1.3.8",
"axios": "0.23.0",
"bad-words": "3.0.4",
+21 -9
View File
@@ -2,6 +2,7 @@ const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, '../../../.env') });
const Sentry = require('@sentry/node');
const Tracing = require('@sentry/tracing');
const createDebugger = require('debug');
const _ = require('lodash');
const loopback = require('loopback');
@@ -14,15 +15,6 @@ const { setupPassport } = require('./component-passport');
const log = createDebugger('fcc:server');
const reqLogFormat = ':date[iso] :status :method :response-time ms - :url';
if (sentry.dsn === 'dsn_from_sentry_dashboard') {
log('Sentry reporting disabled unless DSN is provided.');
} else {
Sentry.init({
dsn: sentry.dsn
});
log('Sentry initialized');
}
const app = loopback();
app.set('state namespace', '__fcc__');
@@ -62,6 +54,7 @@ db.on(
'connected',
_.once(() => log('db connected'))
);
app.start = _.once(function () {
const server = app.listen(app.get('port'), function () {
app.emit('started');
@@ -89,6 +82,25 @@ app.start = _.once(function () {
});
});
if (sentry.dsn === 'dsn_from_sentry_dashboard') {
log('Sentry reporting disabled unless DSN is provided.');
} else {
Sentry.init({
dsn: sentry.dsn,
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Express({
app
})
],
// Capture 20% of transactions to avoid
// overwhelming Sentry and remain within
// the usage quota
tracesSampleRate: 0.2
});
log('Sentry initialized');
}
module.exports = app;
if (require.main === module) {
+2 -1
View File
@@ -1,6 +1,7 @@
{
"initial:before": {
"./middlewares/sentry-request-handler": {}
"./middlewares/sentry-request-handler": {},
"./middlewares/sentry-tracing-handler": {}
},
"initial": {
"compression": {},
@@ -0,0 +1,8 @@
import { Handlers } from '@sentry/node';
import { sentry } from '../../../../config/secrets';
export default function sentryRequestHandler() {
return sentry.dsn === 'dsn_from_sentry_dashboard'
? (req, res, next) => next()
: Handlers.tracingHandler();
}