Skip to content

Commit 0ee3a3c

Browse files
authored
Merge pull request #25 from getsentry/sig/add-crons-v7
feat(crons): Add express crons v7
2 parents 170ca9b + 0e8b23e commit 0ee3a3c

File tree

10 files changed

+1079
-10
lines changed

10 files changed

+1079
-10
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "express_profiling-crons-test-application",
3+
"version": "1.0.0",
4+
"main": "dist/main.js",
5+
"directories": {
6+
"lib": "lib"
7+
},
8+
"scripts": {
9+
"build": "tsc",
10+
"start": "yarn build && node dist/app.js",
11+
"clean": "npx rimraf node_modules,pnpm-lock.yaml"
12+
},
13+
"license": "MIT",
14+
"volta": {
15+
"extends": "../../package.json"
16+
},
17+
"dependencies": {
18+
"@sentry/node": "7.112.1",
19+
"@sentry/profiling-node": "7.112.1",
20+
"cron": "^3.1.7",
21+
"dotenv": "^16.4.5",
22+
"express": "^4.19.2",
23+
"node-cron": "^3.0.3",
24+
"node-schedule": "^2.1.1"
25+
},
26+
"devDependencies": {
27+
"@types/express": "^4",
28+
"@types/node-cron": "^3.0.11",
29+
"@types/node-schedule": "^2.1.7"
30+
}
31+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import * as Sentry from '@sentry/node';
2+
import express from 'express';
3+
import dotenv from 'dotenv';
4+
import { CronJob } from 'cron';
5+
import cron from 'node-cron';
6+
import * as schedule from 'node-schedule';
7+
import { nodeProfilingIntegration } from '@sentry/profiling-node';
8+
9+
dotenv.config({ path: './../../.env' });
10+
11+
const app = express();
12+
const port = 3030;
13+
14+
Sentry.init({
15+
environment: 'qa', // dynamic sampling bias to keep transactions
16+
dsn: process.env.SENTRY_DSN,
17+
includeLocalVariables: true,
18+
debug: true,
19+
tunnel: `http://localhost:3031/`, // proxy server
20+
tracesSampleRate: 1,
21+
profilesSampleRate: 1,
22+
integrations: [new Sentry.Integrations.Express({ app }), nodeProfilingIntegration()],
23+
});
24+
25+
app.use(Sentry.Handlers.requestHandler());
26+
app.use(Sentry.Handlers.tracingHandler());
27+
28+
// cron
29+
const CronJobWithCheckIn = Sentry.cron.instrumentCron(CronJob, 'cron_slug');
30+
31+
const job = CronJobWithCheckIn.from({
32+
cronTime: '7 * * * * *',
33+
onTick: () => {
34+
console.log('cron: Job is running every few seconds');
35+
},
36+
});
37+
38+
job.start();
39+
40+
// node-cron
41+
const cronWithCheckIn = Sentry.cron.instrumentNodeCron(cron);
42+
43+
cronWithCheckIn.schedule(
44+
'11 * * * * *',
45+
() => {
46+
console.log('node-cron: Job is running every few seconds');
47+
},
48+
{ name: 'node-cron_slug' },
49+
);
50+
51+
// node-schedule
52+
const scheduleWithCheckIn = Sentry.cron.instrumentNodeSchedule(schedule);
53+
54+
scheduleWithCheckIn.scheduleJob('node-schedule_slug', '13 * * * * *', () => {
55+
console.log('node-schedule: Job is running every few seconds');
56+
});
57+
58+
// profiling
59+
app.get('/test-profiling-manual', async function (req, res) {
60+
Sentry.startSpan({ name: 'test-transaction', op: 'e2e-test' }, () => {
61+
Sentry.startSpan({ name: 'test-span' }, () => console.log('Did some work'));
62+
});
63+
64+
await Sentry.flush();
65+
66+
res.send({ profiling: 'manual' });
67+
});
68+
69+
app.get('/test-profiling-auto', async function (req, res) {
70+
setTimeout(() => {
71+
console.log('Did some work');
72+
}, 1000);
73+
74+
await Sentry.flush();
75+
76+
res.send({ profiling: 'auto' });
77+
});
78+
79+
// @ts-ignore
80+
app.use(function onError(err, req, res, next) {
81+
// The error id is attached to `res.sentry` to be returned
82+
// and optionally displayed to the user for support.
83+
res.statusCode = 500;
84+
res.end(res.sentry + '\n');
85+
});
86+
87+
app.listen(port, () => {
88+
console.log(`Example app listening on port ${port}`);
89+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"include": ["src/**/*.ts"],
4+
"compilerOptions": {
5+
"outDir": "dist"
6+
}
7+
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"packageManager": "[email protected]",
1010
"scripts": {
1111
"start:proxy-server": "yarn workspace event-proxy-server run start",
12+
"start:proxy-server:express_profiling-crons": "APP_NAME=express_profiling-crons yarn workspace event-proxy-server run start",
1213
"start:proxy-server:express": "APP_NAME=express yarn workspace event-proxy-server run start",
1314
"start:proxy-server:fastify": "APP_NAME=fastify yarn workspace event-proxy-server run start",
1415
"start:proxy-server:connect": "APP_NAME=connect yarn workspace event-proxy-server run start",
@@ -18,6 +19,7 @@
1819
"start:proxy-server:nextjs-14_2_1": "APP_NAME=nextjs-14_2_1 yarn workspace event-proxy-server run start",
1920
"start:proxy-server:sveltekit-2": "APP_NAME=sveltekit-2 yarn workspace event-proxy-server run start",
2021
"start:proxy-server:remix": "APP_NAME=remix yarn workspace event-proxy-server run start",
22+
"start:app:express_profiling-crons": "yarn workspace express_profiling-crons-test-application run start",
2123
"start:app:express": "yarn workspace express-test-application run start",
2224
"start:app:fastify": "yarn workspace fastify-test-application run start",
2325
"start:app:connect": "APP_NAME=connect yarn workspace connect-test-application run start",
@@ -27,6 +29,7 @@
2729
"start:app:nextjs-14_2_1": "yarn workspace nextjs-14_2_1-test-application run dev",
2830
"start:app:sveltekit-2": "yarn workspace sveltekit-2-test-application run dev",
2931
"start:app:remix": "yarn workspace remix-test-application run dev",
32+
"start:express_profiling-crons": "run-p start:proxy-server:express_profiling-crons start:app:express_profiling-crons",
3033
"start:express": "run-p start:proxy-server:express start:app:express",
3134
"start:fastify": "run-p start:proxy-server:fastify start:app:fastify",
3235
"start:connect": "run-p start:proxy-server:connect start:app:connect",
@@ -42,6 +45,7 @@
4245
},
4346
"workspaces": [
4447
"utils/event-proxy-server",
48+
"apps/express_profiling-crons",
4549
"apps/express",
4650
"apps/fastify",
4751
"apps/connect",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[
2+
{
3+
"dsn": "[[dsn]]",
4+
"sdk": {
5+
"name": "sentry.javascript.node",
6+
"version": "7.112.1"
7+
},
8+
"sent_at": "[[ISODateString]]",
9+
"trace": {
10+
"environment": "qa",
11+
"public_key": "[[publicKey]]",
12+
"trace_id": "[[ID1]]"
13+
}
14+
},
15+
{
16+
"type": "check_in"
17+
},
18+
{
19+
"check_in_id": "[[ID2]]",
20+
"contexts": {
21+
"trace": {
22+
"span_id": "[[ID3]]",
23+
"trace_id": "[[ID1]]"
24+
}
25+
},
26+
"duration": "[[duration]]",
27+
"environment": "qa",
28+
"monitor_slug": "cron_slug",
29+
"status": "ok"
30+
}
31+
]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[
2+
{
3+
"dsn": "[[dsn]]",
4+
"sdk": {
5+
"name": "sentry.javascript.node",
6+
"version": "7.112.1"
7+
},
8+
"sent_at": "[[ISODateString]]",
9+
"trace": {
10+
"environment": "qa",
11+
"public_key": "[[publicKey]]",
12+
"trace_id": "[[ID1]]"
13+
}
14+
},
15+
{
16+
"type": "check_in"
17+
},
18+
{
19+
"check_in_id": "[[ID2]]",
20+
"contexts": {
21+
"trace": {
22+
"span_id": "[[ID3]]",
23+
"trace_id": "[[ID1]]"
24+
}
25+
},
26+
"duration": "[[duration]]",
27+
"environment": "qa",
28+
"monitor_slug": "node-schedule_slug",
29+
"status": "ok"
30+
}
31+
]

0 commit comments

Comments
 (0)