Skip to content

Commit 6871a36

Browse files
authored
chore(examples): sentry (#444)
1 parent 7ee13ce commit 6871a36

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

examples/sentry/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Sentry example
2+
3+
Very minimal Sentry integration with Tinyhttp to provide error handler and tracing capabilities.
4+
5+
## Setup
6+
7+
```sh
8+
tinyhttp new sentry
9+
```
10+
11+
## Run
12+
13+
```sh
14+
node --import ./instrument.js index.js
15+
```

examples/sentry/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as Sentry from '@sentry/node'
2+
import { App } from '@tinyhttp/app'
3+
4+
const app = new App({
5+
onError: (err, req, res, next) => {
6+
Sentry.expressErrorHandler()(err, req, res, next ? next : () => {})
7+
res.status(500).send('Internal server error')
8+
}
9+
})
10+
11+
app.get('/', () => {
12+
throw new Error('Oh no, an unexpected error!')
13+
})
14+
15+
app.get('/page/:page/', (req, res) => {
16+
Sentry.startSpan(
17+
{
18+
name: 'someCoolTask',
19+
op: 'cool.task'
20+
},
21+
() => {
22+
res.status(200).send(`
23+
<h1>Some cool page</h1>
24+
<h2>URL</h2>
25+
${req.url}
26+
<h2>Params</h2>
27+
${JSON.stringify(req.params, null, 2)}
28+
`)
29+
}
30+
)
31+
})
32+
33+
app.listen(3000, () => console.log('Listening on http://localhost:3000'))

examples/sentry/instrument.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as Sentry from '@sentry/node'
2+
3+
Sentry.init({
4+
dsn: 'https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@o123456.ingest.sentry.io/123456789',
5+
sampleRate: 1.0,
6+
tracesSampleRate: 1.0
7+
})

examples/sentry/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "sentry",
3+
"private": true,
4+
"type": "module",
5+
"module": "index.js",
6+
"dependencies": {
7+
"@tinyhttp/app": "workspace:*",
8+
"@sentry/node": "^8.30.0"
9+
},
10+
"scripts": {
11+
"start": "node --import ./instrument.js index.js"
12+
}
13+
}

0 commit comments

Comments
 (0)