Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit d3da837

Browse files
committed
✨ Pass level and metadata to sentry scope
Related: #28
1 parent 04d10cc commit d3da837

2 files changed

Lines changed: 66 additions & 11 deletions

File tree

src/sentry.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
import { Transform, TransformCallback } from 'stream';
2-
import { captureException } from '@sentry/node';
2+
import { withScope, captureException, captureMessage, Severity } from '@sentry/node';
33

44
class SentryTransformStream extends Transform {
55
// tslint:disable-next-line:function-name
66
public _transform(chunk: any, _encoding: string, callback: TransformCallback) {
7+
const PINO_TO_SENTRY: { [key: number]: Severity } = {
8+
10: Severity.Debug,
9+
20: Severity.Debug,
10+
30: Severity.Info,
11+
40: Severity.Warning,
12+
50: Severity.Error,
13+
60: Severity.Critical,
14+
};
715
const obj = JSON.parse(chunk);
816
captureException(obj);
17+
withScope(scope => {
18+
scope.setLevel(PINO_TO_SENTRY[obj.level]);
19+
scope.setContext('data', obj);
20+
if (obj.stack) {
21+
captureException(obj);
22+
} else {
23+
captureMessage(obj.message || obj);
24+
}
25+
});
926
this.push(chunk);
1027
callback();
1128
}

src/tests/sentry-mocked.test.ts

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
import { testWriteStream } from './utils';
2-
import { setContext } from '@sentry/core';
3-
41
let loggerFactory;
5-
const captureException = jest.fn();
6-
const captureMessage = jest.fn();
2+
const scope: any = {};
3+
const withScope = jest.fn(fn =>
4+
fn({
5+
setContext: (key: string, val: any) => {
6+
scope.context = { [key]: val };
7+
},
8+
setLevel: (level: any) => {
9+
scope.level = level;
10+
},
11+
})
12+
);
13+
14+
const createCapture = (cb = () => {}) => data => {
15+
cb();
16+
return { data, scope };
17+
};
18+
19+
const captureException = jest.fn(createCapture());
20+
const captureMessage = jest.fn(createCapture());
721

822
describe('sentry mocked', () => {
923
beforeAll(() => {
1024
jest.mock('@sentry/node', () => {
1125
return {
1226
captureException,
27+
withScope,
28+
Severity: {
29+
Debug: 'debug',
30+
Info: 'info',
31+
Warning: 'warning',
32+
Error: 'error',
33+
Critical: 'critical',
34+
},
1335
};
1436
});
1537
loggerFactory = require('..').default;
@@ -19,15 +41,12 @@ describe('sentry mocked', () => {
1941
expect(() => loggerFactory({ sentryDsn: 'DSN' })).not.toThrowError();
2042
});
2143

22-
test('can use custom stream', async () => {
44+
test('sentry is called with correct scope', async () => {
2345
await new Promise((resolve, reject) => {
2446
const logger = loggerFactory({
2547
sentryDsn: 'DSN',
2648
});
27-
captureException.mockImplementation(x => {
28-
resolve();
29-
console.log({ captureException: x });
30-
});
49+
captureException.mockImplementation(createCapture(resolve));
3150
logger.info('Foo');
3251
});
3352
expect(captureException.mock.calls[0]).toMatchInlineSnapshot(`
@@ -39,5 +58,24 @@ describe('sentry mocked', () => {
3958
},
4059
]
4160
`);
61+
expect(captureException.mock.results[0].value).toMatchInlineSnapshot(`
62+
Object {
63+
"data": Object {
64+
"level": 30,
65+
"message": "Foo",
66+
"v": 1,
67+
},
68+
"scope": Object {
69+
"context": Object {
70+
"data": Object {
71+
"level": 30,
72+
"message": "Foo",
73+
"v": 1,
74+
},
75+
},
76+
"level": "info",
77+
},
78+
}
79+
`);
4280
});
4381
});

0 commit comments

Comments
 (0)