-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathapp.ts
More file actions
86 lines (65 loc) · 2.4 KB
/
app.ts
File metadata and controls
86 lines (65 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import './instrument';
// Other imports below
import * as Sentry from '@sentry/node';
import { trace, type Span } from '@opentelemetry/api';
import express from 'express';
const app = express();
const port = 3030;
Sentry.setTag('root-level-tag', 'yes');
app.get('/test-success', function (req, res) {
res.send({ version: 'v1' });
});
app.get('/test-param/:param', function (req, res) {
res.send({ paramWas: req.params.param });
});
app.get('/test-transaction', function (req, res) {
Sentry.withActiveSpan(null, async () => {
Sentry.startSpan({ name: 'test-transaction', op: 'e2e-test' }, () => {
Sentry.startSpan({ name: 'test-span' }, () => undefined);
});
await fetch('http://localhost:3030/test-success');
res.send({});
});
});
app.get('/test-only-if-parent', function (req, res) {
// Remove the HTTP span from the context to simulate no parent span
Sentry.withActiveSpan(null, () => {
// This should NOT create a span because onlyIfParent is true and there's no parent
Sentry.startSpan({ name: 'test-only-if-parent', onlyIfParent: true }, () => {
// This custom OTel span SHOULD be created and exported
// This tests that custom OTel spans aren't suppressed when onlyIfParent triggers
const customTracer = trace.getTracer('custom-tracer');
customTracer.startActiveSpan('custom-span-with-only-if-parent', (span: Span) => {
span.end();
});
});
res.send({});
});
});
app.get('/test-error', async function (req, res) {
const exceptionId = Sentry.captureException(new Error('This is an error'));
await Sentry.flush(2000);
res.send({ exceptionId });
});
app.get('/test-exception/:id', function (req, _res) {
const id = req.params.id;
Sentry.setTag(`param-${id}`, id);
throw new Error(`This is an exception with id ${id}`);
});
app.get('/test-logs/:id', function (req, res) {
const id = req.params.id;
Sentry.startSpan({ name: `log-operation-${id}` }, () => {
Sentry.logger.info(`test-log-${id}`, { requestId: id });
});
res.send({ ok: true, id });
});
Sentry.setupExpressErrorHandler(app);
app.use(function onError(err: unknown, req: any, res: any, next: any) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + '\n');
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});