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
6 changes: 6 additions & 0 deletions .changeset/promql-macros.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperdx/common-utils': minor
'@hyperdx/app': minor
---

feat: Support macros in PromQL charts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default function PromqlExpressionEditor({
onSubmit={() => onSubmit()}
placeholder="rate(http_requests_total{service='api'}[5m])"
metricNames={metricNames}
enableMacros
/>
</Box>
{isIgnored ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ describe('ChartPreviewPanel', () => {
await userEvent.hover(wrapper!);

expect(
await screen.findByText(/Variables could not be expanded/),
await screen.findByText(/Expression could not be expanded/),
).toBeInTheDocument();
expect(
screen.queryByTestId('chart-promql-preview'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ describe('buildRenderedPromqlExpression', () => {
promqlExpression?: PromqlExpressionList;
displayType?: DisplayType;
variables?: ChartVariable[];
granularity?: string;
} = {},
): ChartConfigWithDateRange => ({
configType: 'promql',
Expand Down Expand Up @@ -302,6 +303,47 @@ describe('buildRenderedPromqlExpression', () => {
).toEqual(['up{service=~"api"}', 'errors{service=~"api"}']);
});

it('expands macros with the granularity the chart queries with', () => {
const expression = 'rate(up[$__rate_interval]) / $__interval / $__range';
const render = (displayType: DisplayType, granularity?: string) =>
buildRenderedPromqlExpression(
promqlConfig({
displayType,
granularity,
promqlExpression: [{ expression }],
}),
)?.expressions?.[0].expression;

expect(render(DisplayType.Line, '5 minute')).toBe(
'rate(up[315s]) / 300s / 86400s',
);
// `auto` resolves to the chart's 80-bucket granularity (30 minutes for a
// day), not promqlStep's own fallback.
expect(render(DisplayType.Line, 'auto')).toBe(
'rate(up[1815s]) / 1800s / 86400s',
);
expect(render(DisplayType.Number, 'auto')).toBe(
'rate(up[1815s]) / 1800s / 86400s',
);
});

it('expands macros with the granularity a table queries with', () => {
const seventyMinutes: [Date, Date] = [
new Date('2024-01-01T00:00:00Z'),
new Date('2024-01-01T01:10:00Z'),
];
expect(
buildRenderedPromqlExpression({
...promqlConfig({
displayType: DisplayType.Table,
granularity: 'auto',
promqlExpression: [{ expression: 'rate(up[$__interval])' }],
}),
dateRange: seventyMinutes,
})?.expressions?.[0].expression,
).toBe('rate(up[60s])');
});

it('reports a substitution failure instead of an expression', () => {
const result = buildRenderedPromqlExpression(
promqlConfig({
Expand All @@ -310,7 +352,7 @@ describe('buildRenderedPromqlExpression', () => {
}),
);
expect(result?.expressions).toBeUndefined();
expect(result?.error).toMatch(/Variables could not be expanded/);
expect(result?.error).toMatch(/Expression could not be expanded/);
});
});

Expand Down
42 changes: 34 additions & 8 deletions packages/app/src/components/DBEditTimeChartForm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
TableConnectionChoice,
} from '@hyperdx/common-utils/dist/core/metadata';
import { getQueriedPromqlSeries } from '@hyperdx/common-utils/dist/core/promql';
import { isTimeSeriesDisplayType } from '@hyperdx/common-utils/dist/core/utils';
import {
configConsumesBroadcastFilters,
getBlockingRequiredFilterNames,
Expand All @@ -14,29 +15,31 @@ import {
isRawSqlChartConfig,
isRawSqlSavedChartConfig,
} from '@hyperdx/common-utils/dist/guards';
import { substitutePromqlChartConfigTemplates } from '@hyperdx/common-utils/dist/macros';
import {
BuilderChartConfigWithDateRange,
ChartAlertBaseSchema,
ChartConfigWithDateRange,
ChartConfigWithOptTimestamp,
ChartVariable,
DashboardFilter,
DateRange,
DisplayType,
Filter,
PromqlChartConfig,
SavedChartConfig,
SelectList,
SourceKind,
TSource,
validateAlertScheduleOffsetMinutes,
} from '@hyperdx/common-utils/dist/types';
import {
filterReferencedVariables,
substitutePromqlChartConfigVariables,
} from '@hyperdx/common-utils/dist/variables';
import { filterReferencedVariables } from '@hyperdx/common-utils/dist/variables';

import {
convertToCategoricalChartConfig,
convertToNumberChartConfig,
convertToPromqlNumberChartConfig,
convertToPromqlTableChartConfig,
convertToTableChartConfig,
convertToTimeChartConfig,
tryExpandConfigVariables,
Expand Down Expand Up @@ -263,7 +266,28 @@ export type RenderedPromqlExpression =
| { expressions: RenderedPromqlEntry[]; error?: never }
| { expressions?: never; error: string };

/** The expressions a PromQL tile is queried with, with variables substituted. */
/**
* The config a PromQL chart of this display type actually queries with. Macros
* depend on the resolved granularity and date range, so the preview must
* resolve them the same way the chart does.
*/
function toQueriedPromqlConfig(
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
config: PromqlChartConfig & DateRange,
): PromqlChartConfig & DateRange {
if (config.displayType === DisplayType.Number) {
return convertToPromqlNumberChartConfig(config, { withReducer: false });
}
if (config.displayType === DisplayType.Table) {
return convertToPromqlTableChartConfig(config);
}
if (isTimeSeriesDisplayType(config.displayType)) {
const converted = convertToTimeChartConfig(config);
return isPromqlChartConfig(converted) ? converted : config;
}
return config;
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}

/** The expressions a PromQL tile is queried with, with macros and variables substituted. */
export function buildRenderedPromqlExpression(
queriedConfig: ChartConfigWithDateRange | undefined,
): RenderedPromqlExpression | undefined {
Expand All @@ -272,7 +296,9 @@ export function buildRenderedPromqlExpression(
}

try {
const substituted = substitutePromqlChartConfigVariables(queriedConfig);
const substituted = substitutePromqlChartConfigTemplates(
toQueriedPromqlConfig(queriedConfig),
);
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
return {
expressions: getQueriedPromqlSeries(substituted).map((series, index) => ({
id: String(index),
Expand All @@ -287,8 +313,8 @@ export function buildRenderedPromqlExpression(
return {
error:
e instanceof Error
? `Variables could not be expanded: ${e.message}`
: 'Variables could not be expanded.',
? `Expression could not be expanded: ${e.message}`
: 'Expression could not be expanded.',
};
}
}
Expand Down
37 changes: 26 additions & 11 deletions packages/app/src/components/PromQLEditor/PromQLEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ import {
createCodeMirrorStyleTheme,
DEFAULT_CODE_MIRROR_BASIC_SETUP,
} from '@/components/SQLEditor/utils';
import { usePromqlVariableCompletions } from '@/components/SQLEditor/variableCompletions';
import {
PROMQL_MACRO_COMPLETIONS,
usePromqlVariableCompletions,
} from '@/components/SQLEditor/variableCompletions';
import {
useVariableValidation,
VariableIssueIndicator,
variableValidationState,
} from '@/components/SQLEditor/variableValidation';

import { promqlTemplateExtension } from './templateMaskedLanguage';
import { createVariableCompletionSource } from './variableCompletionSource';

import styles from '@/components/SQLEditor/SQLInlineEditor.module.scss';
Expand All @@ -47,6 +51,8 @@ type PromQLEditorProps = {
* modal or other scroll container, which would otherwise clip the popup.
*/
parentRef?: HTMLElement | null;
/** Suggest `$__interval` and the other chart macros. */
enableMacros?: boolean;
};

const MAX_EDITOR_HEIGHT = '150px';
Expand Down Expand Up @@ -113,30 +119,37 @@ export default function PromQLEditor({
onSubmit,
metricNames,
parentRef,
enableMacros,
}: PromQLEditorProps) {
const { colorScheme } = useMantineColorScheme();
const ref = useRef<ReactCodeMirrorRef>(null);
const compartmentRef = useRef<Compartment>(new Compartment());
const [isFocused, setIsFocused] = useState(false);
const variableCompletions = usePromqlVariableCompletions();
const templateCompletions = useMemo(
() =>
enableMacros
? [...PROMQL_MACRO_COMPLETIONS, ...variableCompletions]
: variableCompletions,
[enableMacros, variableCompletions],
);
const variableIssues = useVariableValidation(value, { language: 'promql' });

const updateAutocomplete = useCallback(
(viewRef: EditorView) => {
const override: CompletionSource[] = [];

if (variableCompletions.length > 0) {
override.push(createVariableCompletionSource(variableCompletions));
if (templateCompletions.length > 0) {
override.push(createVariableCompletionSource(templateCompletions));
}

if (metricNames?.length) {
override.push(debounceAndPruneAutocompleteResults(metricNames));
}

// PromQL's own function/keyword completion, re-registered explicitly.
// `PromQLExtension.asExtension()` registers it through
// `language.data.of({ autocomplete })`, which an `override` array
// replaces outright — so without this it is silently lost.
// PromQL's own function/keyword completion. `promqlTemplateExtension`
// installs the language without it, and an `override` array would
// replace a `language.data` registration outright anyway.
override.push(context => promqlExtension.getComplete().promQL(context));

viewRef.dispatch({
Expand All @@ -145,7 +158,7 @@ export default function PromQLEditor({
),
});
},
[metricNames, variableCompletions],
[metricNames, templateCompletions],
);

useEffect(() => {
Expand All @@ -160,8 +173,10 @@ export default function PromQLEditor({
createCodeMirrorStyleTheme(MAX_EDITOR_HEIGHT),
EditorView.lineWrapping,

// PromQL syntax highlighting
promqlExtension.asExtension(),
// PromQL syntax highlighting and linting, variable- and macro-aware
promqlTemplateExtension(promqlExtension, {
enableMacros: !!enableMacros,
}),

// Autocomplete sources (via compartment for hot-swapping)
// eslint-disable-next-line react-hooks/refs
Expand Down Expand Up @@ -190,7 +205,7 @@ export default function PromQLEditor({
},
]),
],
[onSubmit, parentRef],
[onSubmit, parentRef, enableMacros],
);

const onClickCodeMirror = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
findMaskedTemplateRanges,
maskPromqlTemplates,
templateMaskedPromqlLanguage,
} from '@/components/PromQLEditor/templateMaskedLanguage';

const errorSpans = (doc: string, enableMacros = true) => {
const spans: string[] = [];
templateMaskedPromqlLanguage({ enableMacros })
.parser.parse(doc)
.iterate({
enter: node => {
if (node.type.isError) spans.push(doc.slice(node.from, node.to));
},
});
return spans;
};

describe('findMaskedTemplateRanges', () => {
it('covers each macro and reference exactly', () => {
const expression = 'topk($k, rate(up[$__rate_interval]))';
expect(
findMaskedTemplateRanges(expression, { enableMacros: true }).map(
({ from, to }) => expression.slice(from, to),
),
).toEqual(['$k', '$__rate_interval']);
});

it('leaves references inside label strings alone', () => {
expect(
findMaskedTemplateRanges('up{service=~"$service"}', {
enableMacros: true,
}),
).toEqual([]);
});
});

describe('maskPromqlTemplates', () => {
it('keeps the expression length', () => {
const expression = 'rate(up{a="$a"}[$__interval]) * $k';
const masked = maskPromqlTemplates(expression, { enableMacros: true });
expect(masked).toBe('rate(up{a="$a"}[11111111111]) * aa');
expect(masked).toHaveLength(expression.length);
});

it('masks a variable in a label-name position as an identifier', () => {
expect(
maskPromqlTemplates('sum by (${service:csv}) (up)', {
enableMacros: true,
}),
).toBe('sum by (aaaaaaaaaaaaaa) (up)');
});
});

describe('templateMaskedPromqlLanguage', () => {
it('parses macros in range and offset positions', () => {
expect(
errorSpans(
'sum(rate(up[$__range])) + avg_over_time(up[$__interval] offset $__rate_interval)',
),
).toEqual([]);
});

it('parses variables in metric and label name positions', () => {
expect(errorSpans('sum by (${service:csv}) (up)')).toEqual([]);
expect(errorSpans('sum by (job, $label) ($metric{${key}="x"})')).toEqual(
[],
);
expect(errorSpans('a / on(${keys:csv}) group_left($extra) b')).toEqual([]);
});

it('rejects a variable in a duration position', () => {
expect(errorSpans('rate(up[$window])')).not.toEqual([]);
});

it('keeps node offsets on the original text', () => {
const doc = 'rate(up[$__range])';
const spans: string[] = [];
templateMaskedPromqlLanguage({ enableMacros: true })
.parser.parse(doc)
.iterate({
enter: node => {
if (node.name === 'NumberDurationLiteralInDurationContext') {
spans.push(doc.slice(node.from, node.to));
}
},
});
expect(spans).toEqual(['$__range']);
});

it('rejects macros when macros are disabled', () => {
expect(errorSpans('rate(up[$__interval])', false)).not.toEqual([]);
});

it('parses a macro given arguments', () => {
expect(errorSpans('rate(up[$__interval(1)])')).toEqual([]);
});

it('still rejects errors elsewhere in the expression', () => {
expect(errorSpans('rate(up[$__interval]) +')).not.toEqual([]);
});
});
Loading
Loading