Skip to content

Commit d5c2cfb

Browse files
Merge pull request #1103 from roberto0arruda/fix/sentry-auto-instrument-express
chore: improve instrumentSentry.ts import before another import
2 parents 682eaa9 + cdab5e2 commit d5c2cfb

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/main.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Import this first from sentry instrument!
2+
import '@utils/instrumentSentry';
3+
4+
// Now import other modules
15
import { ProviderFiles } from '@api/provider/sessions';
26
import { PrismaRepository } from '@api/repository/repository.service';
37
import { HttpStatus, router } from '@api/routes/index.router';
@@ -21,19 +25,6 @@ function initWA() {
2125
async function bootstrap() {
2226
const logger = new Logger('SERVER');
2327
const app = express();
24-
const dsn = process.env.SENTRY_DSN;
25-
26-
if (dsn) {
27-
logger.info('Sentry - ON');
28-
Sentry.init({
29-
dsn: dsn,
30-
environment: process.env.NODE_ENV || 'development',
31-
tracesSampleRate: 1.0,
32-
profilesSampleRate: 1.0,
33-
});
34-
35-
Sentry.setupExpressErrorHandler(app);
36-
}
3728

3829
let providerFiles: ProviderFiles = null;
3930
if (configService.get<ProviderSession>('PROVIDER').ENABLED) {
@@ -141,6 +132,14 @@ async function bootstrap() {
141132

142133
eventManager.init(server);
143134

135+
if (process.env.SENTRY_DSN) {
136+
logger.info('Sentry - ON');
137+
138+
// Add this after all routes,
139+
// but before any and other error-handling middlewares are defined
140+
Sentry.setupExpressErrorHandler(app);
141+
}
142+
144143
server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));
145144

146145
initWA();

src/utils/instrumentSentry.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as Sentry from "@sentry/node";
2+
3+
const dsn = process.env.SENTRY_DSN;
4+
5+
if (dsn) {
6+
Sentry.init({
7+
dsn: dsn,
8+
environment: process.env.NODE_ENV || 'development',
9+
tracesSampleRate: 1.0,
10+
profilesSampleRate: 1.0,
11+
});
12+
}

0 commit comments

Comments
 (0)