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

fix: Resolve PromQL queries through the source instead of a caller-supplied connection and table

The `/api/v1/prometheus/*` routes now take `sourceId` instead of `connectionId`, and read the connection and TimeSeries table from that PromQL source. Caller-supplied `database`/`table` params are ignored for ClickHouse and no longer forwarded to external Prometheus endpoints. Saved PromQL chart configs now require `source` and no longer persist `connection`, so provisioned dashboards with PromQL tiles must set `source`.
10 changes: 9 additions & 1 deletion packages/api/src/mcp/tools/query/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
isTimeSeriesDisplayType,
splitAndTrimWithBracket,
} from '@hyperdx/common-utils/dist/core/utils';
import { isBuilderSavedChartConfig } from '@hyperdx/common-utils/dist/guards';
import {
isBuilderSavedChartConfig,
isPromqlSavedChartConfig,
} from '@hyperdx/common-utils/dist/guards';
import { UnknownVariableError } from '@hyperdx/common-utils/dist/macroErrors';
import type {
ChartConfigWithDateRange,
Expand Down Expand Up @@ -618,6 +621,11 @@ export async function runConfigTile(
}
}

// PromQL is evaluated by the Prometheus API, not the SQL renderer.
if (isPromqlSavedChartConfig(savedConfig)) {
Comment thread
karl-power marked this conversation as resolved.
Resolved
Comment thread
karl-power marked this conversation as resolved.
Resolved
return mcpUserError('PromQL tiles cannot be queried through MCP yet.');
}

// Raw SQL tile — hydrate source fields for macro support ($__sourceTable, $__filters)
let sourceFields: {
from?: { databaseName: string; tableName: string };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { buildLogComment } from '@hyperdx/common-utils/dist/clickhouse';
import { Metadata } from '@hyperdx/common-utils/dist/core/metadata';
import { SourceKind } from '@hyperdx/common-utils/dist/types';

import * as config from '@/config';
import {
Expand All @@ -11,6 +12,7 @@ import {
seedTimeSeriesTagsTable,
} from '@/fixtures';
import Connection from '@/models/connection';
import { Source } from '@/models/source';

declare global {
// Set by jest.setup.ts before it stubs `fetch`.
Expand Down Expand Up @@ -118,14 +120,18 @@ describe('request query attribution', () => {
username: config.CLICKHOUSE_USER,
password: config.CLICKHOUSE_PASSWORD,
});
const source = await Source.create({
kind: SourceKind.Promql,
team: team._id,
connection: conn._id,
name: 'PromQL',
from: { databaseName: DEFAULT_DATABASE, tableName: TABLE },
timestampValueExpression: 'timestamp',
});

await agent
.get('/v1/prometheus/labels')
.query({
connectionId: conn._id.toString(),
database: DEFAULT_DATABASE,
table: TABLE,
})
.query({ sourceId: source._id.toString() })
.expect(200);

const logged = await findRouteQueries();
Expand Down Expand Up @@ -153,6 +159,14 @@ describe('request query attribution', () => {
username: config.CLICKHOUSE_USER,
password: config.CLICKHOUSE_PASSWORD,
});
const source = await Source.create({
kind: SourceKind.Promql,
team: team._id,
connection: conn._id,
name: 'PromQL',
from: { databaseName: DEFAULT_DATABASE, tableName: TABLE },
timestampValueExpression: 'timestamp',
});
const tile = `tile-${Date.now()}`;

await agent
Expand All @@ -170,9 +184,7 @@ describe('request query attribution', () => {
.query({
query: 'attribution_metric',
time: '1700003600',
connectionId: conn._id.toString(),
database: DEFAULT_DATABASE,
table: TABLE,
sourceId: source._id.toString(),
})
.expect(200);

Expand Down Expand Up @@ -202,6 +214,14 @@ describe('request query attribution', () => {
username: config.CLICKHOUSE_USER,
password: config.CLICKHOUSE_PASSWORD,
});
const source = await Source.create({
kind: SourceKind.Promql,
team: team._id,
connection: conn._id,
name: 'PromQL',
from: { databaseName: DEFAULT_DATABASE, tableName: TABLE },
timestampValueExpression: 'timestamp',
});
const tile = `tile-${Date.now()}`;

await agent
Expand All @@ -213,9 +233,7 @@ describe('request query attribution', () => {
.query({
query: 'attribution_metric',
time: '1700003600',
connectionId: conn._id.toString(),
database: DEFAULT_DATABASE,
table: TABLE,
sourceId: source._id.toString(),
})
.expect(200);

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/routers/api/__tests__/iac.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('iac router', () => {
config: {
configType: 'promql',
promqlExpression: 'up',
connection: randomMongoId(),
source: randomMongoId(),
displayType: 'line',
},
},
Expand Down
Loading
Loading