-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APM] pull out components and util functions into own files and added…
… tests
- Loading branch information
Showing
5 changed files
with
220 additions
and
90 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
x-pack/plugins/apm/public/components/shared/Stacktrace/LibraryFrames.tsx
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,63 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { EuiLink } from '@elastic/eui'; | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { Stackframe } from '../../../../typings/APMDoc'; | ||
import { px, units } from '../../../style/variables'; | ||
import { CodePreview } from '../../shared/CodePreview'; | ||
// @ts-ignore | ||
import { Ellipsis } from '../../shared/Icons'; | ||
import { FrameHeading } from './FrameHeading'; | ||
import { hasSourceLines } from './stacktraceUtils'; | ||
|
||
const LibraryFrameToggle = styled.div` | ||
margin: 0 0 ${px(units.plus)} 0; | ||
user-select: none; | ||
`; | ||
|
||
interface Props { | ||
visible?: boolean; | ||
stackframes: Stackframe[]; | ||
codeLanguage?: string; | ||
onClick: () => void; | ||
} | ||
|
||
export const LibraryFrames: React.SFC<Props> = ({ | ||
visible, | ||
stackframes, | ||
codeLanguage, | ||
onClick | ||
}) => { | ||
return ( | ||
<div> | ||
<LibraryFrameToggle> | ||
<EuiLink onClick={onClick}> | ||
<Ellipsis horizontal={visible} style={{ marginRight: units.half }} />{' '} | ||
{stackframes.length} library frames | ||
</EuiLink> | ||
</LibraryFrameToggle> | ||
|
||
<div> | ||
{visible && | ||
stackframes.map( | ||
(stackframe, i) => | ||
hasSourceLines(stackframe) ? ( | ||
<CodePreview | ||
key={i} | ||
stackframe={stackframe} | ||
isLibraryFrame | ||
codeLanguage={codeLanguage} | ||
/> | ||
) : ( | ||
<FrameHeading key={i} stackframe={stackframe} isLibraryFrame /> | ||
) | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; |
36 changes: 36 additions & 0 deletions
36
x-pack/plugins/apm/public/components/shared/Stacktrace/__test__/stacktraceUtils.test.ts
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,36 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { Stackframe } from '../../../../../typings/APMDoc'; | ||
import { getCollapsedLibraryFrames, hasSourceLines } from '../stacktraceUtils'; | ||
import stacktracesMock from './stacktraces.json'; | ||
|
||
const stackframeMockWithSource = stacktracesMock[0]; | ||
const stackframeMockWithoutSource = stacktracesMock[1]; | ||
|
||
describe('stactraceUtils', () => { | ||
describe('getCollapsedLibraryFrames', () => { | ||
it('should collapse the library frames into a set of grouped, nested stackframes', () => { | ||
const result = getCollapsedLibraryFrames(stacktracesMock as Stackframe[]); | ||
expect(result.length).toBe(3); | ||
expect(result[0].libraryFrame).toBe(false); | ||
expect(result[1].libraryFrame).toBe(true); | ||
expect(result[1].stackframes).toHaveLength(2); // two nested stackframes | ||
expect(result[2].libraryFrame).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('hasSourceLines', () => { | ||
it('should return true given a stackframe with a source context', () => { | ||
const result = hasSourceLines(stackframeMockWithSource as Stackframe); | ||
expect(result).toBe(true); | ||
}); | ||
it('should return false given a stackframe with no source context', () => { | ||
const result = hasSourceLines(stackframeMockWithoutSource as Stackframe); | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
}); |
67 changes: 67 additions & 0 deletions
67
x-pack/plugins/apm/public/components/shared/Stacktrace/__test__/stacktraces.json
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,67 @@ | ||
[ | ||
{ | ||
"function": "<anonymous>", | ||
"libraryFrame": false, | ||
"excludeFromGrouping": false, | ||
"context": { | ||
"pre": ["", "app.get('/log-error', function (req, res) {"], | ||
"post": [ | ||
" if (err) {", | ||
" res.status(500).send('could not capture error: ' + err.message)" | ||
] | ||
}, | ||
"line": { | ||
"number": 17, | ||
"context": " apm.captureError(new Error('foo'), function (err) {" | ||
}, | ||
"filename": "server/coffee.js", | ||
"absPath": "/app/server/coffee.js" | ||
}, | ||
{ | ||
"function": "get", | ||
"libraryFrame": true, | ||
"excludeFromGrouping": false, | ||
"context": {}, | ||
"line": { | ||
"number": 123, | ||
"context": "" | ||
}, | ||
"filename": "express/get.js", | ||
"absPath": "/node_modules/express/get.js" | ||
}, | ||
{ | ||
"function": "use", | ||
"libraryFrame": true, | ||
"excludeFromGrouping": false, | ||
"context": { | ||
"pre": ["", "app.use('/log-error', function (req, res) {"], | ||
"post": [ | ||
" if (err) {", | ||
" res.status(500).send('could not capture error: ' + err.message)" | ||
] | ||
}, | ||
"line": { | ||
"number": 234, | ||
"context": " apm.captureError(new Error('foo'), function (err) {" | ||
}, | ||
"filename": "express/use.js", | ||
"absPath": "/node_modules/express/use.js" | ||
}, | ||
{ | ||
"function": "handleCoffee", | ||
"libraryFrame": false, | ||
"excludeFromGrouping": false, | ||
"context": { | ||
"pre": ["", ""], | ||
"post": [ | ||
" reply(getCoffee(req.id));" | ||
] | ||
}, | ||
"line": { | ||
"number": 45, | ||
"context": " handleCoffee(req => {" | ||
}, | ||
"filename": "server/handleCoffee.js", | ||
"absPath": "/app/server/handleCoffee.js" | ||
} | ||
] |
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
45 changes: 45 additions & 0 deletions
45
x-pack/plugins/apm/public/components/shared/Stacktrace/stacktraceUtils.ts
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,45 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { get, isEmpty } from 'lodash'; | ||
import { Stackframe } from '../../../../typings/APMDoc'; | ||
|
||
export interface StackframeCollapsed extends Stackframe { | ||
libraryFrame?: boolean; | ||
stackframes?: Stackframe[]; | ||
} | ||
|
||
export function getCollapsedLibraryFrames( | ||
stackframes: Stackframe[] | ||
): StackframeCollapsed[] { | ||
return stackframes.reduce((acc: any, stackframe: StackframeCollapsed) => { | ||
if (!stackframe.libraryFrame) { | ||
return [...acc, stackframe]; | ||
} | ||
|
||
// current stackframe is library frame | ||
const prevItem: StackframeCollapsed = acc[acc.length - 1]; | ||
if (!get(prevItem, 'libraryFrame')) { | ||
return [...acc, { libraryFrame: true, stackframes: [stackframe] }]; | ||
} | ||
|
||
return [ | ||
...acc.slice(0, -1), | ||
{ | ||
...prevItem, | ||
stackframes: prevItem.stackframes | ||
? [...prevItem.stackframes, stackframe] | ||
: [stackframe] | ||
} | ||
]; | ||
}, []); | ||
} | ||
|
||
export function hasSourceLines(stackframe: Stackframe) { | ||
return ( | ||
!isEmpty(stackframe.context) || !isEmpty(get(stackframe, 'line.context')) | ||
); | ||
} |