Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/promql-heatmap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/app': minor
---

feat: Support PromQL heatmaps (series-mode)
19 changes: 14 additions & 5 deletions packages/app/src/DBDashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
displayTypeRequiresSource,
isBuilderChartConfig,
isBuilderSavedChartConfig,
isPromqlChartConfig,
isPromqlSavedChartConfig,
isRawSqlChartConfig,
isRawSqlSavedChartConfig,
Expand All @@ -50,7 +51,6 @@ import {
import { isMissingFiltersMacro } from '@hyperdx/common-utils/dist/macros';
import {
AlertState,
BuilderChartConfigWithDateRange,
ChartConfigWithDateRange,
ChartVariable,
DashboardContainer as DashboardContainerSchema,
Expand Down Expand Up @@ -177,7 +177,10 @@ import ChartContainer, {
} from './components/charts/ChartContainer';
import DashboardFiltersModal from './components/DashboardFiltersModal';
import { DBBarChart } from './components/DBBarChart';
import DBHeatmapChart, { toHeatmapQuery } from './components/DBHeatmapChart';
import DBHeatmapChart, {
HeatmapSeriesChartConfig,
toHeatmapQuery,
} from './components/DBHeatmapChart';
import { DBPieChart } from './components/DBPieChart';
import DBSqlRowTableWithSideBar from './components/DBSqlRowTableWithSidebar';
import OnboardingModal from './components/OnboardingModal';
Expand Down Expand Up @@ -244,7 +247,7 @@ function HeatmapTile({
title: React.ReactNode;
toolbarPrefix: React.ReactNode[];
toolbarSuffix: React.ReactNode[];
queriedConfig: BuilderChartConfigWithDateRange;
queriedConfig: HeatmapSeriesChartConfig;
source: TSource | undefined;
dateRange: [Date, Date];
enabled?: boolean;
Expand All @@ -262,7 +265,12 @@ function HeatmapTile({

const eventDeltasUrl = useMemo(() => {
// Search page event deltas only supports trace sources and distribution mode
if (!source || !isTraceSource(source) || mode !== 'distribution')
if (
!source ||
!isTraceSource(source) ||
mode !== 'distribution' ||
!isBuilderChartConfig(queriedConfig)
)
return null;
const url = buildEventsSearchUrl({
source,
Expand Down Expand Up @@ -1383,7 +1391,8 @@ const Tile = ({
/>
)}
{effectiveQueriedConfig?.displayType === DisplayType.Heatmap &&
isBuilderChartConfig(effectiveQueriedConfig) && (
(isBuilderChartConfig(effectiveQueriedConfig) ||
isPromqlChartConfig(effectiveQueriedConfig)) && (
<HeatmapTile
keyPrefix={keyPrefix}
chartId={chart.id}
Expand Down
58 changes: 56 additions & 2 deletions packages/app/src/components/ChartEditor/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ const sessionSource: TSource = {
traceSourceId: 'source-trace',
};

const promqlSource: TSource = {
id: 'source-promql',
name: 'PromQL Source',
kind: SourceKind.Promql,
connection: 'conn-1',
from: { databaseName: 'db', tableName: 'prom' },
timestampValueExpression: 'Timestamp',
};

const seriesItem = {
aggFn: 'count' as const,
valueExpression: '*',
Expand Down Expand Up @@ -659,6 +668,26 @@ describe('PromQL expressions', () => {
series: [],
});

it('saves a heatmap with only its first expression queried and no heatmap mode', () => {
const result = convertFormStateToSavedChartConfig(
{
...promqlForm([{ expression: 'up' }], DisplayType.Heatmap),
legendTemplate: '{{pod}}',
heatmap: { mode: 'distribution' },
granularity: '5 minute',
},
undefined,
);
expect(result).toMatchObject({
configType: 'promql',
displayType: DisplayType.Heatmap,
promqlExpression: [{ expression: 'up' }],
legendTemplate: '{{pod}}',
granularity: '5 minute',
});
expect(result).not.toHaveProperty('heatmap');
});

it('saves the form rows as the expression list', () => {
expect(
convertFormStateToSavedChartConfig(
Expand Down Expand Up @@ -1841,6 +1870,21 @@ describe('validateChartForm', () => {
expect(errors).toContainEqual(expect.objectContaining({ path: 'series' }));
});

it('does not require a value expression on a PromQL heatmap', () => {
const errors = validateChartForm(
makeForm({
configType: 'promql',
displayType: DisplayType.Heatmap,
source: 'source-promql',
promqlExpressions: [{ expression: 'up' }],
series: [{ ...seriesItem, valueExpression: '' }],
}),
promqlSource,
jest.fn(),
);
expect(errors).toHaveLength(0);
});

it('rejects heatmap chart without a value expression', () => {
const setError = jest.fn();
const errors = validateChartForm(
Expand Down Expand Up @@ -2407,12 +2451,22 @@ describe('getAllowedSourceKinds', () => {
},
);

it('narrows to traces on heatmap tiles', () => {
it('narrows to traces on builder heatmap tiles', () => {
expect(
getAllowedSourceKinds({
configType: 'promql',
configType: 'builder',
displayType: DisplayType.Heatmap,
}),
).toEqual([SourceKind.Trace]);
});

it('offers only PromQL sources on PromQL heatmap tiles', () => {
expect(
getAllowedSourceKinds({
configType: 'promql',
displayType: DisplayType.Heatmap,
heatmapMode: 'distribution',
}),
).toEqual([SourceKind.Promql]);
});
});
23 changes: 22 additions & 1 deletion packages/app/src/components/ChartEditor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,25 @@ export const isStringSelectDisplayType = (
displayType === DisplayType.Search ||
displayType === DisplayType.EventPatterns;

export const isPromqlDisplayType = isRawSqlDisplayType;
export const isPromqlDisplayType = (
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
displayType: DisplayType | undefined,
): displayType is
| DisplayType.Table
| DisplayType.Line
| DisplayType.StackedBar
| DisplayType.StackedLine
| DisplayType.Pie
| DisplayType.Bar
| DisplayType.Number
| DisplayType.Heatmap =>
displayType === DisplayType.Table ||
displayType === DisplayType.Line ||
displayType === DisplayType.StackedBar ||
displayType === DisplayType.StackedLine ||
displayType === DisplayType.Pie ||
displayType === DisplayType.Bar ||
displayType === DisplayType.Number ||
displayType === DisplayType.Heatmap;
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

const NON_PROMQL_SOURCE_KINDS = Object.values(SourceKind).filter(
kind => kind !== SourceKind.Promql,
Expand Down Expand Up @@ -553,6 +571,8 @@ export const validateChartForm = (

const isRawSqlChart =
form.configType === 'sql' && isRawSqlDisplayType(form.displayType);
const isPromqlChart =
form.configType === 'promql' && isPromqlDisplayType(form.displayType);

// Validate connection is selected for raw SQL charts
if (isRawSqlChart && !form.connection) {
Expand Down Expand Up @@ -731,6 +751,7 @@ export const validateChartForm = (
// validated like any other builder series above.
if (
!isRawSqlChart &&
!isPromqlChart &&
form.displayType === DisplayType.Heatmap &&
getHeatmapMode(form) === 'distribution' &&
Array.isArray(form.series) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useCallback, useMemo, useState } from 'react';
import { isBuilderChartConfig } from '@hyperdx/common-utils/dist/guards';
import {
isBuilderChartConfig,
isPromqlChartConfig,
} from '@hyperdx/common-utils/dist/guards';
import {
BuilderChartConfigWithDateRange,
BuilderChartConfigWithOptTimestamp,
Expand All @@ -26,6 +29,7 @@ import DBHeatmapChart, {
buildHeatmapBucketConfig,
buildHeatmapSeriesConfig,
HEATMAP_N_BUCKETS,
HeatmapSeriesChartConfig,
resolveHeatmapGranularity,
toHeatmapQuery,
} from '@/components/DBHeatmapChart';
Expand Down Expand Up @@ -64,7 +68,7 @@ function HeatmapPreview({
config,
minGranularitySeconds,
}: {
config: BuilderChartConfigWithDateRange;
config: HeatmapSeriesChartConfig;
minGranularitySeconds: number | undefined;
}) {
return (
Expand Down Expand Up @@ -344,7 +348,8 @@ export function ChartPreviewPanel({
)}
{queryReady &&
queriedConfig != null &&
isBuilderChartConfig(queriedConfig) &&
(isBuilderChartConfig(queriedConfig) ||
isPromqlChartConfig(queriedConfig)) &&
activeTab === 'heatmap' && (
<HeatmapPreview
config={queriedConfig}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export default function EditTimeChartForm({
const configType = useWatch({ control, name: 'configType' });
const connection = useWatch({ control, name: 'connection' });
const promqlExpressions = useWatch({ control, name: 'promqlExpressions' });
const heatmapMode = getHeatmapMode({
const formHeatmapMode = getHeatmapMode({
heatmap: useWatch({ control, name: 'heatmap' }),
});

Expand All @@ -319,6 +319,8 @@ export default function EditTimeChartForm({
configType === 'sql' && isRawSqlDisplayType(displayType);
const isPromqlInput =
configType === 'promql' && isPromqlDisplayType(displayType);
// PromQL heatmaps only support series mode
const heatmapMode: HeatmapMode = isPromqlInput ? 'series' : formHeatmapMode;
Comment thread
pulpdrew marked this conversation as resolved.
Resolved

const { data: sources } = useSources();

Expand Down Expand Up @@ -779,9 +781,12 @@ export default function EditTimeChartForm({
setValue('source', firstSourceItemValue(allowedSourceItems) ?? '');

// Record that a display-type or heatmap mode change triggered a source
// change, so that auto-submit runs in the correct effect below.
// change, so that auto-submit runs in the correct effect below. A mode
// change that comes from switching config type doesn't auto-submit, like
// any other config type change.
isDisplayTypeSourceSwapPendingRef.current =
prev.displayType !== displayType || prev.heatmapMode !== heatmapMode;
prev.displayType !== displayType ||
(prev.heatmapMode !== heatmapMode && prev.configType === configType);
}, [
allowedSourceItems,
allowedSourceKinds,
Expand Down Expand Up @@ -1008,6 +1013,19 @@ export default function EditTimeChartForm({
[tableSource],
);

const configTypeOptions = useMemo(
() => [
{ label: 'Builder', value: 'builder' },
...(isRawSqlDisplayType(displayType)
? [{ label: 'SQL', value: 'sql' }]
: []),
...(IS_PROMQL_ENABLED && isPromqlDisplayType(displayType)
? [{ label: 'PromQL', value: 'promql' }]
: []),
],
[displayType],
);

return (
<div ref={setParentRef} data-testid={dataTestId}>
<ErrorBoundary>
Expand Down Expand Up @@ -1093,21 +1111,19 @@ export default function EditTimeChartForm({
placeholder="My Chart Name"
data-testid="chart-name-input"
/>
{isRawSqlDisplayType(displayType) && (
{configTypeOptions.length > 1 && (
<Controller
control={control}
name="configType"
render={({ field: { onChange, value } }) => (
<SegmentedControl
value={value ?? 'builder'}
value={
configTypeOptions.some(option => option.value === value)
? value
: 'builder'
}
onChange={onChange}
data={[
{ label: 'Builder', value: 'builder' },
{ label: 'SQL', value: 'sql' },
...(IS_PROMQL_ENABLED
? [{ label: 'PromQL', value: 'promql' }]
: []),
]}
data={configTypeOptions}
/>
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ describe('buildRenderedPromqlExpression', () => {
);
});

it("expands macros with a heatmap's finer auto granularity", () => {
expect(
buildRenderedPromqlExpression(
promqlConfig({
displayType: DisplayType.Heatmap,
granularity: 'auto',
promqlExpression: [{ expression: 'rate(up[$__interval])' }],
}),
)?.expressions?.[0].expression,
).toBe('rate(up[900s])');
});

it('expands macros with the granularity a table queries with', () => {
const seventyMinutes: [Date, Date] = [
new Date('2024-01-01T00:00:00Z'),
Expand Down
11 changes: 11 additions & 0 deletions packages/app/src/components/DBEditTimeChartForm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ import {
tryExpandConfigVariables,
} from '@/ChartUtils';
import { ChartEditorFormState } from '@/components/ChartEditor/types';
import {
buildHeatmapSeriesConfig,
resolveHeatmapGranularity,
} from '@/components/DBHeatmapChart/heatmapQueries';
import { getMinGranularitySeconds } from '@/hooks/useChartConfig';
import { getFirstTimestampValueExpression } from '@/source';
import { getMetricTableName } from '@/utils';
Expand Down Expand Up @@ -287,6 +291,13 @@ function toQueriedPromqlConfig(
const converted = convertToTimeChartConfig(config);
return isPromqlChartConfig(converted) ? converted : config;
}
if (config.displayType === DisplayType.Heatmap) {
const converted = buildHeatmapSeriesConfig(
config,
resolveHeatmapGranularity(config),
);
return isPromqlChartConfig(converted) ? converted : config;
}
return config;
}

Expand Down
Loading
Loading