Skip to content

Commit

Permalink
feat: allow to pass our own addBreadcrumb
Browse files Browse the repository at this point in the history
BREAKING CHANGE: requires that user pass their own addBreadcrumb
  • Loading branch information
gajus committed Oct 21, 2022
1 parent d681c02 commit 3ce8a26
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 48 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@ Sentry [breadcrumbs](https://sentry.io/features/breadcrumbs/) allow one to trace
## Usage

```js
import {
getCurrentHub,
} from '@sentry/node';
import {
createRoarrSentryIntegration,
} from '@roarr/sentry';

createSentry({
integrations: [
createRoarrSentryIntegration(),
createRoarrSentryIntegration({
addBreadcrumb: (breadcrumb) => {
// Your implementation might vary
const hub = getCurrentHub();

hub.addBreadcrumb(breadcrumb);
},
}),
],
});

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
"ava": "^3.15.0",
"coveralls": "^3.1.1",
"del-cli": "^4.0.1",
"eslint": "^8.3.0",
"eslint-config-canonical": "^32.42.0",
"eslint": "^8.25.0",
"eslint-config-canonical": "^32.49.1",
"husky": "^7.0.4",
"nyc": "^15.1.0",
"semantic-release": "^18.0.1",
"sinon": "^12.0.1",
"ts-node": "^10.4.0",
"typescript": "^4.5.2"
"typescript": "^4.8.4"
},
"engines": {
"node": ">=10.0"
Expand Down
25 changes: 12 additions & 13 deletions src/factories/createRoarrSentryIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type {
EventProcessor,
Hub,
Integration,
SeverityLevel,
import {
type Breadcrumb,
type Integration,
type SeverityLevel,
} from '@sentry/types';
import type {
LogLevelName,
} from 'roarr';
import {
ROARR,
getLogLevelName,
type LogLevelName,
} from 'roarr';

const getSeverity = (logLevelName: LogLevelName): SeverityLevel => {
Expand All @@ -28,19 +25,21 @@ const getSeverity = (logLevelName: LogLevelName): SeverityLevel => {
}
};

export const createRoarrSentryIntegration = () => {
export const createRoarrSentryIntegration = ({
addBreadcrumb,
}: {
addBreadcrumb: (breadcrumb: Breadcrumb) => void,
}) => {
return new class CaptureRoarr implements Integration {
public name: string = 'CaptureRoarr';

public setupOnce = (addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub) => {
public setupOnce = () => {
const originalWrite = ROARR.write;

ROARR.write = (jsonMessage: string) => {
const hub = getCurrentHub();

const message = JSON.parse(jsonMessage);

hub.addBreadcrumb(
addBreadcrumb(
{
category: message.context.namespace,
data: {
Expand Down
49 changes: 18 additions & 31 deletions test/roarr-sentry/factories/createRoarrSentryIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import {
} from '../../../src/factories/createRoarrSentryIntegration';

test('creates class with CaptureRoarr constructor name', (t) => {
t.is(createRoarrSentryIntegration().constructor.name, 'CaptureRoarr');
t.is(createRoarrSentryIntegration({
addBreadcrumb: () => {},
}).constructor.name, 'CaptureRoarr');
});

const noop = (): any => {};

test('overrides ROARR.write method', (t) => {
const originalWrite = ROARR.write;

createRoarrSentryIntegration()
.setupOnce(
noop,
noop,
);
createRoarrSentryIntegration({
addBreadcrumb: () => {},
})
.setupOnce();

t.not(ROARR.write, originalWrite);
});
Expand All @@ -32,15 +33,10 @@ test('passes-through calls to ROARR.write', (t) => {

ROARR.write = spy;

createRoarrSentryIntegration()
.setupOnce(
noop,
(): any => {
return {
addBreadcrumb: () => {},
};
},
);
createRoarrSentryIntegration({
addBreadcrumb: () => {},
})
.setupOnce();

const payload = JSON.stringify({
context: {
Expand All @@ -55,23 +51,14 @@ test('passes-through calls to ROARR.write', (t) => {
});

test('adds logs to breadcrumbs', (t) => {
const spy = sinon.stub();
const addBreadcrumb = sinon.stub();

ROARR.write = spy;

const hub = {
addBreadcrumb: () => {},
};
ROARR.write = () => {};

const addBreadcrumbSpy = sinon.spy(hub, 'addBreadcrumb');

createRoarrSentryIntegration()
.setupOnce(
noop,
(): any => {
return hub;
},
);
createRoarrSentryIntegration({
addBreadcrumb,
})
.setupOnce();

const payload = JSON.stringify({
context: {
Expand All @@ -82,7 +69,7 @@ test('adds logs to breadcrumbs', (t) => {

ROARR.write(payload);

t.like(addBreadcrumbSpy.firstCall.firstArg, {
t.like(addBreadcrumb.firstCall.firstArg, {
category: 'bar',
data: {
context: {
Expand Down

0 comments on commit 3ce8a26

Please sign in to comment.