This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { captureException, captureMessage, Severity, withScope } from '@sentry/node'; | ||
import { Transform, TransformCallback } from 'stream'; | ||
|
||
const reportToSentry = (obj: any) => { | ||
if (!obj.stack) { | ||
return captureMessage(obj.message || obj); | ||
} | ||
const error = new Error(obj.message); | ||
error.message = obj.message; | ||
error.stack = obj.stack; | ||
error.name = obj.name; | ||
return captureException(error); | ||
}; | ||
|
||
const PINO_TO_SENTRY: { [key: number]: Severity } = { | ||
10: Severity.Debug, | ||
20: Severity.Debug, | ||
30: Severity.Info, | ||
40: Severity.Warning, | ||
50: Severity.Error, | ||
60: Severity.Critical, | ||
}; | ||
|
||
class SentryTransformStream extends Transform { | ||
// tslint:disable-next-line:function-name | ||
public _transform(chunk: any, _encoding: string, callback: TransformCallback) { | ||
const obj = JSON.parse(chunk); | ||
withScope(scope => { | ||
scope.setLevel(PINO_TO_SENTRY[obj.level]); | ||
scope.setExtras(obj); | ||
reportToSentry(obj); | ||
}); | ||
this.push(chunk); | ||
callback(); | ||
} | ||
} | ||
export { SentryTransformStream }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
let loggerFactory; | ||
const scope: any = {}; | ||
const withScope = jest.fn(fn => | ||
fn({ | ||
setContext: (key: string, val: any) => { | ||
scope.context = { [key]: val }; | ||
}, | ||
setExtras: (val: any) => { | ||
scope.extras = val; | ||
}, | ||
setLevel: (level: any) => { | ||
scope.level = level; | ||
}, | ||
}) | ||
); | ||
|
||
const createCapture = (cb = () => {}) => data => { | ||
cb(); | ||
return { data, scope }; | ||
}; | ||
|
||
const captureException = jest.fn(createCapture()); | ||
const captureMessage = jest.fn(createCapture()); | ||
const init = jest.fn(); | ||
|
||
describe('sentry mocked', () => { | ||
beforeAll(() => { | ||
jest.mock('@sentry/node', () => { | ||
return { | ||
captureException, | ||
captureMessage, | ||
withScope, | ||
init, | ||
Severity: { | ||
Debug: 'debug', | ||
Info: 'info', | ||
Warning: 'warning', | ||
Error: 'error', | ||
Critical: 'critical', | ||
}, | ||
}; | ||
}); | ||
loggerFactory = require('..').default; | ||
}); | ||
beforeEach(() => { | ||
captureException.mockReset(); | ||
captureMessage.mockReset(); | ||
}); | ||
test('can create logger with options', () => { | ||
expect(() => loggerFactory()).not.toThrowError(); | ||
expect(() => loggerFactory({ sentry: true })).not.toThrowError(); | ||
expect(init).not.toHaveBeenCalled(); | ||
expect(() => loggerFactory({ sentry: 'dummy' })).not.toThrowError(); | ||
expect(init.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"dsn": "dummy", | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test('sentry captureMessage is called with correct scope', async () => { | ||
await new Promise((resolve, reject) => { | ||
const logger = loggerFactory({ | ||
sentry: 'DSN', | ||
}); | ||
captureMessage.mockImplementation(createCapture(resolve)); | ||
logger.info('Foo'); | ||
}); | ||
expect(captureMessage).toHaveBeenCalledTimes(1); | ||
expect(captureException).not.toHaveBeenCalled(); | ||
expect(captureMessage.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
"Foo", | ||
] | ||
`); | ||
expect(captureMessage.mock.results[0].value).toMatchInlineSnapshot(` | ||
Object { | ||
"data": "Foo", | ||
"scope": Object { | ||
"extras": Object { | ||
"level": 30, | ||
"message": "Foo", | ||
"v": 1, | ||
}, | ||
"level": "info", | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
test('sentry captureException with stack and correct levels', async () => { | ||
await new Promise((resolve, reject) => { | ||
const logger = loggerFactory({ | ||
sentry: 'DSN', | ||
}); | ||
captureException.mockReset(); | ||
captureException.mockImplementation(createCapture(resolve)); | ||
logger.error(new Error()); | ||
}); | ||
expect(captureException).toHaveBeenCalledTimes(1); | ||
expect(captureMessage).not.toHaveBeenCalled(); | ||
expect(captureException.mock.results[0].value).toMatchObject({ | ||
data: expect.any(Error), | ||
scope: { | ||
level: 'error', | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import loggerFactory from '..'; | ||
|
||
describe('sentry not available', () => { | ||
beforeAll(() => { | ||
jest.mock('@sentry/node', () => { | ||
throw new Error("Cannot find module '@sentry/node' from 'index.ts'"); | ||
}); | ||
}); | ||
test('without sentry lib works by default, but crashes on provided', () => { | ||
expect(() => loggerFactory()).not.toThrowError(); | ||
expect(() => loggerFactory({ sentry: 'DSN' })).toThrowErrorMatchingInlineSnapshot( | ||
`"Cannot find module '@sentry/node' from 'index.ts'"` | ||
); | ||
}); | ||
}); |