Repository navigation
fix: Validate PromQL tile sources and keep PromQL tiles on MCP saves #3414
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@hyperdx/api': patch | ||
| --- | ||
|
|
||
| fix: Reject external API PromQL tiles that reference a non-PromQL source, and keep PromQL tiles when an MCP agent updates a dashboard | ||
|
|
||
| `/api/v2/dashboards` now returns a 400 when a PromQL tile's `sourceId` is not a PromQL source. MCP `clickstack_save_dashboard` updates no longer delete the PromQL tiles that MCP hides from agents. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { DisplayType } from '@hyperdx/common-utils/dist/types'; | ||
| import mongoose from 'mongoose'; | ||
|
|
||
| import { callTool, getFirstText } from '@/mcp/__tests__/mcpTestUtils'; | ||
| import Dashboard from '@/models/dashboard'; | ||
|
|
||
| import { setupDashboardTests } from './setup'; | ||
|
|
||
| // MCP cannot edit PromQL tiles until HDX-5302, so it hides them on read and | ||
| // keeps them on save. Remove with that work. | ||
| describe('MCP Dashboard Tools - PromQL tiles', () => { | ||
| const ctx = setupDashboardTests(); | ||
|
|
||
| const promqlTile = { | ||
| id: 'promql-tile', | ||
| x: 0, | ||
| y: 0, | ||
| w: 6, | ||
| h: 3, | ||
| config: { | ||
| configType: 'promql', | ||
| name: 'PromQL', | ||
| displayType: DisplayType.Line, | ||
| source: new mongoose.Types.ObjectId().toString(), | ||
| promqlExpression: [{ expression: 'up' }], | ||
| }, | ||
| }; | ||
|
|
||
| const markdownTile = { | ||
| id: 'markdown-tile', | ||
| x: 6, | ||
| y: 0, | ||
| w: 6, | ||
| h: 3, | ||
| config: { | ||
| name: 'Notes', | ||
| displayType: DisplayType.Markdown, | ||
| markdown: 'Notes', | ||
| source: '', | ||
| where: '', | ||
| select: [], | ||
| }, | ||
| }; | ||
|
|
||
| const createDashboard = () => | ||
| new Dashboard({ | ||
| name: 'PromQL dashboard', | ||
| tiles: [promqlTile, markdownTile], | ||
| team: ctx.team._id, | ||
| }).save(); | ||
|
|
||
| it('hides PromQL tiles from clickstack_get_dashboard', async () => { | ||
| const dashboard = await createDashboard(); | ||
|
|
||
| const result = await callTool(ctx.client!, 'clickstack_get_dashboard', { | ||
| id: dashboard._id.toString(), | ||
| }); | ||
|
|
||
| expect(result.isError).toBeFalsy(); | ||
| const output = JSON.parse(getFirstText(result)); | ||
| expect(output.tiles.map((t: { id: string }) => t.id)).toEqual([ | ||
| 'markdown-tile', | ||
| ]); | ||
| }); | ||
|
|
||
| it('keeps PromQL tiles when clickstack_save_dashboard updates the dashboard', async () => { | ||
| const dashboard = await createDashboard(); | ||
|
|
||
| const result = await callTool(ctx.client!, 'clickstack_save_dashboard', { | ||
| id: dashboard._id.toString(), | ||
| name: 'PromQL dashboard', | ||
| tiles: [ | ||
| { | ||
| id: 'markdown-tile', | ||
| name: 'Updated notes', | ||
| config: { displayType: 'markdown', markdown: 'Updated' }, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(result.isError).toBeFalsy(); | ||
| const stored = await Dashboard.findById(dashboard._id).lean(); | ||
| expect(stored?.tiles.map(t => t.id)).toEqual([ | ||
| 'markdown-tile', | ||
| 'promql-tile', | ||
|
karl-power marked this conversation as resolved.
Resolved
|
||
| ]); | ||
| expect(stored?.tiles[1].config).toEqual(promqlTile.config); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1359,19 +1359,21 @@ function getHeatmapTilesWithIncompatibleSources( | |
|
|
||
| /** | ||
| * Returns an error message string if any of the referenced source IDs is not | ||
| * a valid PromQL source, or null if all of them are. | ||
| * a valid PromQL source, or null if all of them are. `subject` names what | ||
| * referenced them, e.g. "PromQL tiles". | ||
| */ | ||
| function getPromqlLabelFilterSourceError( | ||
| function getNonPromqlSourceError( | ||
| sources: SourceForValidation[], | ||
| referencedSourceIds: string[], | ||
| subject: string, | ||
| ): string | null { | ||
| const sourceById = new Map(sources.map(s => [s._id.toString(), s])); | ||
| const invalid = [...new Set(referencedSourceIds)].filter(id => { | ||
| const source = sourceById.get(id); | ||
| return source === undefined || !isPromqlSource(source); | ||
| }); | ||
| if (invalid.length === 0) return null; | ||
| return `PROMETHEUS_LABEL filters require a PromQL source. The following source IDs are not PromQL sources: ${invalid.join(', ')}`; | ||
| return `${subject} require a PromQL source. The following source IDs are not PromQL sources: ${invalid.join(', ')}`; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1504,6 +1506,33 @@ function filterChangedHeatmapTiles( | |
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the source IDs of PromQL tiles to check against the PromQL | ||
| * source-kind gate. With `existingTiles` (update paths), a tile that was already | ||
| * a PromQL tile on the same source is skipped, mirroring | ||
| * `filterChangedHeatmapTiles`: a source whose kind changed after the tile was | ||
| * saved must not block unrelated dashboard edits. | ||
| */ | ||
| function getChangedPromqlTileSourceIds( | ||
| tiles: ExternalDashboardTileWithId[], | ||
| existingTiles: DashboardDocument['tiles'] | undefined, | ||
| ): string[] { | ||
| const existingPromqlSources = new Map( | ||
| (existingTiles ?? []).flatMap(tile => | ||
| isPromqlSavedChartConfig(tile.config) | ||
| ? [[tile.id, tile.config.source]] | ||
| : [], | ||
| ), | ||
| ); | ||
| return tiles.flatMap(tile => | ||
| isConfigTile(tile) && | ||
| isPromqlExternalTileConfig(tile.config) && | ||
| existingPromqlSources.get(tile.id) !== tile.config.sourceId | ||
| ? [tile.config.sourceId] | ||
| : [], | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns source IDs referenced by onClick search link-outs (mode=id, | ||
| * type=search) whose source kind is not log or trace. The /search destination | ||
|
|
@@ -1711,7 +1740,7 @@ export type TileValidationContext = { | |
|
|
||
| /** | ||
| * Run the full suite of tile validation checks (sources, connections, | ||
| * heatmap source-kind, onClick targets, container/tab refs). Returns | ||
| * heatmap and PromQL source-kind, onClick targets, container/tab refs). Returns | ||
| * `null` when all checks pass, or an error message string on failure. | ||
| * | ||
| * Consolidates the ~95-line validation block that was previously | ||
|
|
@@ -1789,11 +1818,20 @@ export async function validateDashboardTiles( | |
| return `Tiles with formulas require a Metric, Log, or Trace source. The following source IDs are not formula-capable: ${formulaIncompatibleSources.join(', ')}`; | ||
| } | ||
|
|
||
| // Validate that PROMETHEUS_LABEL filters reference PromQL sources | ||
| // PromQL tiles and PROMETHEUS_LABEL filters query through their source's | ||
| // connection and TimeSeries table, which only a PromQL source carries. | ||
| const promqlTileError = getNonPromqlSourceError( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 minor — PromQL source check re-validates every PromQL tile on update, so one tile with a stale source blocks unrelated PUTs Unlike the heatmap and formula checks just above, which only re-check new or changed tiles on update ( Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond |
||
| sources, | ||
| getChangedPromqlTileSourceIds(tiles, existingTiles), | ||
| 'PromQL tiles', | ||
| ); | ||
|
karl-power marked this conversation as resolved.
Resolved
|
||
| if (promqlTileError != null) return promqlTileError; | ||
|
|
||
| const promqlLabelFilters = (filters ?? []).filter(isPrometheusLabelFilter); | ||
| const promqlLabelFilterError = getPromqlLabelFilterSourceError( | ||
| const promqlLabelFilterError = getNonPromqlSourceError( | ||
| sources, | ||
| promqlLabelFilters.map(f => f.sourceId), | ||
| 'PROMETHEUS_LABEL filters', | ||
| ); | ||
| if (promqlLabelFilterError != null) return promqlLabelFilterError; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 minor — No test for a request tile that reuses a hidden PromQL tile's id
The new merge in
saveDashboard.ts:329-336has a second branch: a request tile whose id matches a hidden PromQL tile replaces it instead of being added next to it. The PR description promises this, but no test sends a tile with idpromql-tile. If the!requestTileIds.has(tile.id)filter breaks, the stored dashboard ends up with two tiles sharing one id. Add a case that saves{ id: 'promql-tile', ... }and asserts exactly onepromql-tilewith the new config.Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond