Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORE-764: new sentry logger #77

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/ToggleButtonGroup.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ describe('ToggleButtonGroup', () => {
selectionMode
${'multiple'}
${'single'}
`(`matches snapshot with selectionMode #selectionMode`, ({selectionMode}) => {
${undefined}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covering line 20 of ToggleButtonGroup file.

`(`matches snapshot with selectionMode $selectionMode`, ({selectionMode}) => {
const tree = renderer.create(
<ToggleButtonGroup
selectionMode={selectionMode}
Expand Down
139 changes: 137 additions & 2 deletions src/components/__snapshots__/ToggleButtonGroup.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ToggleButtonGroup matches snapshot with selectionMode #selectionMode 1`] = `
exports[`ToggleButtonGroup matches snapshot with selectionMode multiple 1`] = `
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix test descriptions

<div
aria-orientation="horizontal"
className="sc-gsnTZi hmfeGJ"
Expand Down Expand Up @@ -130,7 +130,142 @@ exports[`ToggleButtonGroup matches snapshot with selectionMode #selectionMode 1`
</div>
`;

exports[`ToggleButtonGroup matches snapshot with selectionMode #selectionMode 2`] = `
exports[`ToggleButtonGroup matches snapshot with selectionMode single 1`] = `
<div
aria-orientation="horizontal"
className="sc-gsnTZi hmfeGJ"
data-orientation="horizontal"
data-rac=""
onBlurCapture={[Function]}
onFocusCapture={[Function]}
onKeyDownCapture={[Function]}
role="radiogroup"
>
<button
aria-checked={true}
className="sc-dkzDqf jDeGjH"
data-rac=""
data-selected={true}
data-testid="red-testid"
disabled={false}
onBlur={[Function]}
onClick={[Function]}
onDragStart={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
onTouchMove={[Function]}
onTouchStart={[Function]}
role="radio"
type="button"
>
Red
</button>
<button
aria-checked={false}
className="sc-dkzDqf jDeGjH"
data-rac=""
data-testid="green-testid"
disabled={false}
onBlur={[Function]}
onClick={[Function]}
onDragStart={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
onTouchMove={[Function]}
onTouchStart={[Function]}
role="radio"
type="button"
>
Green
</button>
<button
aria-checked={false}
className="sc-dkzDqf jDeGjH"
data-rac=""
data-testid="blue-testid"
disabled={false}
onBlur={[Function]}
onClick={[Function]}
onDragStart={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
onTouchMove={[Function]}
onTouchStart={[Function]}
role="radio"
type="button"
>
Blue
</button>
<button
aria-checked={false}
className="sc-dkzDqf jDeGjH"
data-rac=""
data-testid="yellow-testid"
disabled={false}
onBlur={[Function]}
onClick={[Function]}
onDragStart={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
onTouchMove={[Function]}
onTouchStart={[Function]}
role="radio"
type="button"
>
Yellow
</button>
<button
aria-checked={false}
className="sc-dkzDqf jDeGjH"
data-rac=""
data-testid="orange-testid"
disabled={false}
onBlur={[Function]}
onClick={[Function]}
onDragStart={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
onTouchMove={[Function]}
onTouchStart={[Function]}
role="radio"
type="button"
>
Orange
</button>
</div>
`;

exports[`ToggleButtonGroup matches snapshot with selectionMode undefined 1`] = `
<div
aria-orientation="horizontal"
className="sc-gsnTZi hmfeGJ"
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export * from './contexts';
export * from './hooks';
export * from './theme';
export * from './types';
export * from './sentryLogger/sentryLog';
export type { Key } from 'react-aria-components';
68 changes: 68 additions & 0 deletions src/sentryLogger/sentryLog.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { createSentryLogger } from './sentryLog';
import * as Sentry from "@sentry/react";
import { Level, Logger } from '@openstax/ts-utils/services/logger';

describe('createConsoleLogger', () => {
let logFn: jest.SpyInstance;
let logger: Logger;

beforeEach(() => {
logger = createSentryLogger();
logFn = jest.spyOn(Sentry, 'addBreadcrumb').mockImplementation(() => null);
});

afterEach(() => {
logFn.mockReset();
});

it('logs all fields', () => {
logger.setContext({ extra: 'context' });
logger.logEvent(
Level.Info,
{
"timestamp": 1461165894000,
"event_id": "any-id",
"message": "This is a test",
"from": "/login",
"to": "/dashboard",
"profile": {},
"address": {
"city": "New York"
}
}
);

expect(logFn).toHaveBeenCalledWith(
{
"timestamp": 1461165894000,
"level": "info",
"message": "This is a test",
"category": "log",
"data": {
"from": "/login",
"to": "/dashboard",
"event_id": "any-id",
"extra": "context",
"city": "New York",
}
}
);
});

it('logs with missing fields', () => {
logger.logEvent(
Level.Warn,
{},
);

expect(logFn).toHaveBeenCalledWith(
{
"timestamp": undefined,
"category": "log",
"message": "",
"level": "warning",
"data": {}
}
);
});
});
47 changes: 47 additions & 0 deletions src/sentryLogger/sentryLog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { addBreadcrumb, SeverityLevel } from "@sentry/react";
import { createCoreLogger, Level } from '@openstax/ts-utils/services/logger';
import { JsonCompatibleStruct } from "@openstax/ts-utils/routing";

/**
* Flatten nested objects
* @see https://stackoverflow.com/a/70377608
*/

const flattenObj = (obj: { [x: string]: any; }) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when copying red-code from stack overflow i like to put a link to the SO answer in a comment above the function

Object.keys(obj).reduce((acc: { [x: string]: any; }, curKey) => {
if (typeof obj[curKey] === 'object') {
acc = { ...acc, ...flattenObj(obj[curKey]) }
} else {
acc[curKey] = obj[curKey]
}
return acc
}, {});

const serializeLevel = (level: Level): SeverityLevel =>
level === Level.Warn ? 'warning' : level;

const serializeBreadcrumb = (level: Level, breadcrumb: JsonCompatibleStruct): {
level?: SeverityLevel;
category?: string;
message?: string;
timestamp?: number;
data?: { [key: string]: any };
} => {
const { type, category, message, timestamp, ...rest } = breadcrumb;

return {
level: serializeLevel(level),
category: 'log',
message: typeof message === 'string' ? message : '',
timestamp: typeof timestamp === 'number' ? timestamp : undefined,
data: flattenObj(rest),
}
};

/**
* Creates a logger that creates breadcrumbs using Sentry.
* @see https://develop.sentry.dev/sdk/data-model/event-payloads/breadcrumbs/
*/
export const createSentryLogger = () => createCoreLogger((level, breadcrumb) =>
addBreadcrumb(serializeBreadcrumb(level, breadcrumb))
);
Loading