-
Notifications
You must be signed in to change notification settings - Fork 0
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
jomcarvajal
wants to merge
5
commits into
main
Choose a base branch
from
CORE-764-sentry-logger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
feb6d5c
CORE-764: new sentry logger
jomcarvajal 05eb478
CORE-764: fix casting issues, append rest of data
jomcarvajal 5762d21
CORE-764: add export to index
jomcarvajal 5168453
CORE-764: validate timestamp as number
jomcarvajal 6f3266c
CORE-764: simplify typeof check, remove type key and set category as log
jomcarvajal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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`] = ` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix test descriptions |
||
<div | ||
aria-orientation="horizontal" | ||
className="sc-gsnTZi hmfeGJ" | ||
|
@@ -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" | ||
|
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,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": {} | ||
} | ||
); | ||
}); | ||
}); |
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,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; }) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.