Skip to content
Open
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
7 changes: 7 additions & 0 deletions .changeset/promql-table-multi-expression.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hyperdx/common-utils': patch
'@hyperdx/app': patch
'@hyperdx/api': patch
---

fix: query every expression of a PromQL table tile. Table tiles used to run only the first expression and drop the rest without warning; they now return one value column per expression, joined on the label set (and timestamp for range queries), and the editor offers multi-expression editing for table tiles.
2 changes: 1 addition & 1 deletion packages/api/src/mcp/prompts/dashboards/promqlContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const PROMQL_DASHBOARD_SECTION = `== PROMQL TILES ==
Sources with kind "promql" hold Prometheus metrics. Chart them with PROMQL tiles, not builder or raw SQL tiles:
{ "name": "Request rate", "config": { "configType": "promql", "displayType": "line", "sourceId": "<promql source id>", "expressions": [{ "expression": "sum by (job) (rate(http_requests_total{job=~\\"$job\\"}[$__rate_interval]))" }], "legendTemplate": "{{job}}" } }

- displayType works as for builder tiles. line, stacked_bar and stacked_line plot every expression; number, table, pie and bar query only the first and can set queryType "instant" to evaluate once at the end of the range.
- displayType works as for builder tiles. line, stacked_bar, stacked_line and table plot every expression (a table joins them into one value column per expression); number, pie and bar query only the first and can set queryType "instant" to evaluate once at the end of the range.
- legendTemplate names each series from its labels, e.g. "{{job}} / {{instance}}".
- Filter with a PROMETHEUS_LABEL dashboard filter: { "type": "PROMETHEUS_LABEL", "name": "Job", "sourceId": "<promql source id>", "label": "job", "variableName": "job" }. Tiles reference it in a matcher: {job=~"$job"} (multi-select) or {job="$job"} with maxSelections: 1. clickstack_describe_source with metricName lists a metric's label names.
- For free-form label conditions, use an ADHOC filter with sourceType "promql": { "type": "ADHOC", "name": "Labels", "sourceType": "promql", "sources": ["<promql source id>"] }. Its conditions are added as label matchers to every selector in the PromQL tiles on those sources, with no tile wiring. Operators: =, !=, =~, !~, and IN (shown as =| in the filter bar); IN conditions on the same label are ORed into one =~"a|b" matcher. Seed them via savedFilterValues as for sql ad hoc filters.
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/mcp/tools/dashboards/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1456,8 +1456,9 @@ const mcpPromqlTileSchema = mcpTileLayoutSchema.extend({
.min(1)
.max(MAX_PROMQL_EXPRESSIONS)
.describe(
'PromQL queries. Line and stacked charts plot every expression; ' +
'other display types query only the first.',
'PromQL queries. Line, stacked and table charts plot every ' +
'expression (a table joins them into one value column per ' +
'expression); number, pie and bar query only the first.',
),
granularity: mcpUnion([SQLIntervalSchema, z.literal('auto')])
.optional()
Expand Down
49 changes: 49 additions & 0 deletions packages/api/src/mcp/tools/query/__tests__/promqlTile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ describe('buildPromqlTileQueries', () => {
expect(queries.map(q => q.expression)).toEqual(['sum(up)']);
});

it('queries every expression of a table tile', () => {
const queries = buildPromqlTileQueries({
config: {
displayType: DisplayType.Table,
promqlExpression: [
{ expression: 'sum(up)', queryType: 'instant' },
{ expression: 'up', queryType: 'instant' },
],
},
dateRange,
});

expect(queries.map(q => q.expression)).toEqual(['sum(up)', 'up']);
expect(queries.map(q => q.query.kind)).toEqual(['instant', 'instant']);
});

it('expands dashboard variables', () => {
const [{ query }] = buildPromqlTileQueries({
config: {
Expand Down Expand Up @@ -288,6 +304,39 @@ describe('toPromqlTileResult', () => {
expect(result).not.toHaveProperty('seriesLabels');
});

it("joins a table tile's expressions into one value column each", () => {
const result = toPromqlTileResult(
{
displayType: DisplayType.Table,
promqlExpression: [
{ expression: 'requests', alias: 'requests', queryType: 'instant' },
{ expression: 'errors', alias: 'errors', queryType: 'instant' },
],
},
[
{
expression: 'requests',
alias: 'requests',
isBucketed: false,
result: [{ metric: { service: 'api' }, values: [[t0, 10]] }],
},
{
expression: 'errors',
alias: 'errors',
isBucketed: false,
result: [{ metric: { service: 'api' }, values: [[t0, 2]] }],
},
],
);

expect(result.data).toEqual([{ service: 'api', requests: 10, errors: 2 }]);
expect(result.meta.map(m => m.name)).toEqual([
'service',
'requests',
'errors',
]);
});

it('keys a histogram heatmap by bucket', () => {
const result = toPromqlTileResult(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default function PromqlChartEditor({
const { data: metricNames } = usePromqlMetricNames(promqlSource?.id);

const displayTypeSupportsMultiExpression =
isTimeSeriesDisplayType(displayType);
isTimeSeriesDisplayType(displayType) || displayType === DisplayType.Table;
const canAddExpression =
displayTypeSupportsMultiExpression &&
expressions.length < MAX_PROMQL_EXPRESSIONS;
Expand Down
4 changes: 2 additions & 2 deletions packages/common-utils/src/core/__tests__/promql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ describe('getQueriedPromqlSeries', () => {
{ expression: 'rate(errors[5m])' },
];

it('keeps every expression for a time series chart', () => {
it('keeps every expression for charts that plot several result sets', () => {
for (const displayType of [
DisplayType.Line,
DisplayType.StackedBar,
DisplayType.StackedLine,
DisplayType.Table,
]) {
expect(getQueriedPromqlSeries({ promqlExpression, displayType })).toEqual(
promqlExpression,
Expand All @@ -77,7 +78,6 @@ describe('getQueriedPromqlSeries', () => {
it('keeps only the first expression for other display types', () => {
for (const displayType of [
DisplayType.Number,
DisplayType.Table,
DisplayType.Pie,
DisplayType.Bar,
undefined,
Expand Down
110 changes: 104 additions & 6 deletions packages/common-utils/src/core/__tests__/promqlChart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,107 @@ describe('toPromqlTableRows', () => {
it('unions the labels across expressions', () => {
const result = toPromqlTableRows([
instantResult({ service: 'accounting' }, 1, 1),
instantResult({ pod: 'a' }, 1, 2),
{
...instantResult({ pod: 'a' }, 1, 2),
expression: 'up{pod="a"}',
},
]);

expect(result.data).toEqual([
{ pod: '', service: 'accounting', Value_1: 1, Value_2: null },
{ pod: 'a', service: '', Value_1: null, Value_2: 2 },
]);
expect(result.meta).toEqual([
{ name: 'pod', type: 'String' },
{ name: 'service', type: 'String' },
{ name: 'Value_1', type: 'Float64' },
{ name: 'Value_2', type: 'Float64' },
]);
});

it('joins expressions on the label set, one value column per expression', () => {
const result = toPromqlTableRows([
{
expression: 'max by (service) (requests)',
alias: 'requests',
isBucketed: false,
result: [
{ metric: { service: 'api' }, values: [[1, 10]] },
{ metric: { service: 'web' }, values: [[1, 20]] },
],
},
{
expression: 'max by (service) (errors)',
alias: 'errors',
isBucketed: false,
result: [
{ metric: { service: 'api' }, values: [[1, 2]] },
{ metric: { service: 'db' }, values: [[1, 3]] },
],
},
]);

expect(result.data).toEqual([
{ service: 'api', requests: 10, errors: 2 },
{ service: 'web', requests: 20, errors: null },
{ service: 'db', requests: null, errors: 3 },
]);
expect(result.meta).toEqual([
{ name: 'service', type: 'String' },
{ name: 'requests', type: 'Float64' },
{ name: 'errors', type: 'Float64' },
]);
});

it('joins range queries on the label set and timestamp', () => {
const result = toPromqlTableRows([
{
expression: 'requests',
isBucketed: true,
result: [
{
metric: { service: 'api' },
values: [
[1700000000, 1],
[1700000060, 2],
],
},
],
},
{
expression: 'errors',
isBucketed: true,
result: [
{
metric: { service: 'api' },
values: [
[1700000000, 0],
[1700000120, 4],
],
},
],
},
]);

expect(result.data).toEqual([
{ pod: '', service: 'accounting', Value: 1 },
{ pod: 'a', service: '', Value: 2 },
{
Timestamp: '2023-11-14T22:13:20.000Z',
service: 'api',
Value_1: 1,
Value_2: 0,
},
{
Timestamp: '2023-11-14T22:14:20.000Z',
service: 'api',
Value_1: 2,
Value_2: null,
},
{
Timestamp: '2023-11-14T22:15:20.000Z',
service: 'api',
Value_1: null,
Value_2: 4,
},
]);
});

Expand Down Expand Up @@ -447,19 +542,22 @@ describe('toPromqlTableRows', () => {
'__proto__',
'constructor',
'pod',
'Value',
'Value_1',
'Value_2',
]);
expect(Object.entries(result.data[0])).toEqual([
['__proto__', 'b'],
['constructor', 'a'],
['pod', ''],
['Value', 1],
['Value_1', 1],
['Value_2', null],
]);
expect(Object.entries(result.data[1])).toEqual([
['__proto__', ''],
['constructor', ''],
['pod', 'x'],
['Value', 2],
['Value_1', null],
['Value_2', 2],
]);
});
});
Expand Down
11 changes: 7 additions & 4 deletions packages/common-utils/src/core/promql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ export function getExemplarPromqlExpression(
/**
* The expressions a PromQL config actually queries. Blank rows are skipped so
* an unfinished one the editor is still holding never reaches Prometheus. Only
* time series charts plot several result sets.
* time series charts and tables plot several result sets; number, pie and bar
* tiles show one value per series and keep the first expression.
*/
export function getQueriedPromqlSeries(config: {
promqlExpression?: PromqlExpressionList;
displayType?: DisplayType;
}): PromqlSeries[] {
const series = getPromqlSeries(config);
const queried = isTimeSeriesDisplayType(config.displayType)
? series
: series.slice(0, 1);
const queried =
isTimeSeriesDisplayType(config.displayType) ||
config.displayType === DisplayType.Table
? series
: series.slice(0, 1);
return queried.filter(({ expression }) => expression.trim());
}

Expand Down
Loading
Loading