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

feat(FlameGraph): Keep focus whenever the profile data changes #325

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@
"@emotion/css": "11.10.6",
"@grafana/data": "11.3.2",
"@grafana/faro-web-sdk": "^1.10.0",
"@grafana/flamegraph": "11.3.2",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed and added all its dependencies instead.

"@grafana/llm": "^0.10.7",
"@grafana/runtime": "11.3.2",
"@grafana/scenes": "^4.22.0",
"@grafana/schema": "11.3.2",
"@grafana/ui": "11.3.2",
"@leeoniya/ufuzzy": "^1.0.17",
"@react-aria/utils": "^3.25.3",
"@tanstack/react-query": "^5.17.19",
"color": "^4.2.3",
"compression-streams-polyfill": "^0.1.7",
"d3": "^7.9.0",
"file-saver": "^2.0.5",
"history": "^5.3.0",
"markdown-to-jsx": "^7.3.2",
Expand All @@ -59,7 +60,10 @@
"react-helmet": "^6.1.0",
"react-inlinesvg": "^4.1.3",
"react-router-dom": "^6.22.0",
"react-use": "^17.6.0",
"react-virtualized-auto-sizer": "^1.0.25",
"rxjs": "7.8.1",
"tinycolor2": "^1.6.0",
"tslib": "2.5.3",
"xstate": "^4.38.3"
},
Expand All @@ -79,18 +83,19 @@
"@testing-library/react-hooks": "^8.0.1",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/color": "^3.0.2",
"@types/d3": "^7",
"@types/d3-scale": "^4.0.2",
"@types/file-saver": "^2.0.7",
"@types/flot": "^0.0.32",
"@types/jest": "^29.5.0",
"@types/jquery": "^3.5.16",
"@types/lodash": "^4.14.188",
"@types/node": "^22.7.4",
"@types/prismjs": "^1.26.0",
"@types/react": "18.2.37",
"@types/react-dom": "18.2.15",
"@types/react-helmet": "^6.1.5",
"@types/testing-library__jest-dom": "5.14.8",
"@types/tinycolor2": "^1",
"@typescript-eslint/eslint-plugin": "6.18.1",
"@typescript-eslint/parser": "6.18.1",
"bundlewatch": "^0.4.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { css } from '@emotion/css';
import { createTheme, GrafanaTheme2, LoadingState, TimeRange } from '@grafana/data';
import { FlameGraph } from '@grafana/flamegraph';
import { SceneComponentProps, SceneObjectBase, SceneObjectState, SceneQueryRunner } from '@grafana/scenes';
import { Spinner, useStyles2, useTheme2 } from '@grafana/ui';
import { displayWarning } from '@shared/domain/displayStatus';
Expand All @@ -14,6 +13,7 @@ import { PyroscopeLogo } from '@shared/ui/PyroscopeLogo';
import React, { useEffect, useMemo } from 'react';
import { Unsubscribable } from 'rxjs';

import { FlameGraph } from '../../../../tmp/grafana-flamegraph/src/';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TS was complaining when I tried aliasing @gfrafana/flamegraph. Webpack didn't 🤷🏾‍♂️

import { useBuildPyroscopeQuery } from '../../domain/useBuildPyroscopeQuery';
import { getSceneVariableValue } from '../../helpers/getSceneVariableValue';
import { buildFlameGraphQueryRunner } from '../../infrastructure/flame-graph/buildFlameGraphQueryRunner';
Expand Down Expand Up @@ -201,6 +201,7 @@ export class SceneFlameGraph extends SceneObjectBase<SceneFlameGraphState> {
timeRange={data.export.timeRange}
/>
}
keepFocusOnDataChange
/>
</Panel>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IconName } from '@grafana/data';
import { Props as FlameGraphProps } from '@grafana/flamegraph';
import { reportInteraction } from '@shared/domain/reportInteraction';
import { useFetchPluginSettings } from '@shared/infrastructure/settings/useFetchPluginSettings';
import { DomainHookReturnValue } from '@shared/types/DomainHookReturnValue';
import { useCallback, useState } from 'react';

import { Props as FlameGraphProps } from '../../../../../../../tmp/grafana-flamegraph/src';
import { useGitHubContext } from '../components/GitHubContextProvider/useGitHubContext';
import { buildStackTrace } from './buildStackTrace';

Expand Down
3 changes: 2 additions & 1 deletion src/shared/components/FlameGraph/FlameGraph.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createTheme } from '@grafana/data';
import { FlameGraph as GrafanaFlameGraph, Props } from '@grafana/flamegraph';
import { useTheme2 } from '@grafana/ui';
import React, { memo, useMemo } from 'react';

import { FlameGraph as GrafanaFlameGraph, Props } from '../../../tmp/grafana-flamegraph/src/';
import type { FlamebearerProfile } from '../../types/FlamebearerProfile';
import { ExportData } from './components/ExportData';
import { flamebearerToDataFrameDTO } from './domain/flamebearerToDataFrameDTO';
Expand Down Expand Up @@ -46,6 +46,7 @@ function FlameGraphComponent({
vertical={vertical}
getTheme={getTheme as any}
getExtraContextMenuButtons={getExtraContextMenuButtons}
keepFocusOnDataChange
/>
);
}
Expand Down
9 changes: 9 additions & 0 deletions src/tmp/grafana-flamegraph/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"rules": {
"no-unused-vars": "off",
"react/react-in-jsx-scope": "off",
"@grafana/no-border-radius-literal": "off",
"sonarjs/cognitive-complexity": "off",
"sonarjs/no-collapsible-if": "off"
}
}
212 changes: 212 additions & 0 deletions src/tmp/grafana-flamegraph/src/FlameGraph/FlameGraph.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
// This component is based on logic from the flamebearer project
// https://github.com/mapbox/flamebearer
// ISC License
// Copyright (c) 2018, Mapbox
// Permission to use, copy, modify, and/or distribute this software for any purpose
// with or without fee is hereby granted, provided that the above copyright notice
// and this permission notice appear in all copies.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
// OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
// THIS SOFTWARE.
import * as React from 'react';
import { css, cx } from '@emotion/css';
import { Icon } from '@grafana/ui';
import { useEffect, useState } from 'react';

import { PIXELS_PER_LEVEL } from '../constants';
import { ClickedItemData, ColorScheme, ColorSchemeDiff, SelectedView, TextAlign } from '../types';
import { CollapsedMap, FlameGraphDataContainer, LevelItem } from './dataTransform';
import FlameGraphCanvas from './FlameGraphCanvas';
import { GetExtraContextMenuButtonsFunction } from './FlameGraphContextMenu';
import FlameGraphMetadata from './FlameGraphMetadata';

type Props = {
data: FlameGraphDataContainer;
rangeMin: number;
rangeMax: number;
matchedLabels?: Set<string>;
setRangeMin: (range: number) => void;
setRangeMax: (range: number) => void;
style?: React.CSSProperties;
onItemFocused: (data: ClickedItemData) => void;
focusedItemData?: ClickedItemData;
textAlign: TextAlign;
sandwichItem?: string;
onSandwich: (label: string) => void;
onFocusPillClick: () => void;
onSandwichPillClick: () => void;
colorScheme: ColorScheme | ColorSchemeDiff;
showFlameGraphOnly?: boolean;
getExtraContextMenuButtons?: GetExtraContextMenuButtonsFunction;
collapsing?: boolean;
selectedView: SelectedView;
search: string;
collapsedMap: CollapsedMap;
setCollapsedMap: (collapsedMap: CollapsedMap) => void;
};

const FlameGraph = ({
data,
rangeMin,
rangeMax,
matchedLabels,
setRangeMin,
setRangeMax,
onItemFocused,
focusedItemData,
textAlign,
onSandwich,
sandwichItem,
onFocusPillClick,
onSandwichPillClick,
colorScheme,
showFlameGraphOnly,
getExtraContextMenuButtons,
collapsing,
selectedView,
search,
collapsedMap,
setCollapsedMap,
}: Props) => {
const styles = getStyles();

const [levels, setLevels] = useState<LevelItem[][]>();
const [levelsCallers, setLevelsCallers] = useState<LevelItem[][]>();
const [totalProfileTicks, setTotalProfileTicks] = useState<number>(0);
const [totalProfileTicksRight, setTotalProfileTicksRight] = useState<number>();
const [totalViewTicks, setTotalViewTicks] = useState<number>(0);

useEffect(() => {
if (data) {
let levels = data.getLevels();
let totalProfileTicks = levels.length ? levels[0][0].value : 0;
let totalProfileTicksRight = levels.length ? levels[0][0].valueRight : undefined;
let totalViewTicks = totalProfileTicks;
let levelsCallers = undefined;

if (sandwichItem) {
const [callers, callees] = data.getSandwichLevels(sandwichItem);
levels = callees;
levelsCallers = callers;
// We need this separate as in case of diff profile we want to compute diff colors based on the original ticks.
totalViewTicks = callees[0]?.[0]?.value ?? 0;
}
setLevels(levels);
setLevelsCallers(levelsCallers);
setTotalProfileTicks(totalProfileTicks);
setTotalProfileTicksRight(totalProfileTicksRight);
setTotalViewTicks(totalViewTicks);
}
}, [data, sandwichItem]);

if (!levels) {
return null;
}

const commonCanvasProps = {
data,
rangeMin,
rangeMax,
matchedLabels,
setRangeMin,
setRangeMax,
onItemFocused,
focusedItemData,
textAlign,
onSandwich,
colorScheme,
totalProfileTicks,
totalProfileTicksRight,
totalViewTicks,
showFlameGraphOnly,
collapsedMap,
setCollapsedMap,
getExtraContextMenuButtons,
collapsing,
search,
selectedView,
};
const canvas = levelsCallers?.length ? (
grafakus marked this conversation as resolved.
Show resolved Hide resolved
<>
<div className={styles.sandwichCanvasWrapper}>
<div className={styles.sandwichMarker}>
Callers
<Icon className={styles.sandwichMarkerIcon} name={'arrow-down'} />
</div>
<FlameGraphCanvas
{...commonCanvasProps}
root={levelsCallers[levelsCallers.length - 1][0]}
depth={levelsCallers.length}
direction={'parents'}
// We do not support collapsing in sandwich view for now.
collapsing={false}
/>
</div>

<div className={styles.sandwichCanvasWrapper}>
<div className={cx(styles.sandwichMarker, styles.sandwichMarkerCalees)}>
<Icon className={styles.sandwichMarkerIcon} name={'arrow-up'} />
Callees
</div>
<FlameGraphCanvas
{...commonCanvasProps}
root={levels[0][0]}
depth={levels.length}
direction={'children'}
collapsing={false}
/>
</div>
</>
) : (
<FlameGraphCanvas {...commonCanvasProps} root={levels[0][0]} depth={levels.length} direction={'children'} />
);

return (
<div className={styles.graph}>
<FlameGraphMetadata
data={data}
focusedItem={focusedItemData}
sandwichedLabel={sandwichItem}
totalTicks={totalViewTicks}
onFocusPillClick={onFocusPillClick}
onSandwichPillClick={onSandwichPillClick}
/>
{canvas}
</div>
);
};

const getStyles = () => ({
graph: css({
label: 'graph',
overflow: 'auto',
flexGrow: 1,
flexBasis: '50%',
}),
sandwichCanvasWrapper: css({
label: 'sandwichCanvasWrapper',
display: 'flex',
marginBottom: `${PIXELS_PER_LEVEL / window.devicePixelRatio}px`,
}),
sandwichMarker: css({
label: 'sandwichMarker',
writingMode: 'vertical-lr',
transform: 'rotate(180deg)',
overflow: 'hidden',
whiteSpace: 'nowrap',
}),
sandwichMarkerCalees: css({
label: 'sandwichMarkerCalees',
textAlign: 'right',
}),
sandwichMarkerIcon: css({
label: 'sandwichMarkerIcon',
verticalAlign: 'baseline',
}),
});

export default FlameGraph;
Loading
Loading