Skip to content

Commit 5eb60be

Browse files
committed
fix: drop cached PromQL lookups when a source is updated
PromQL lookups are now keyed by source ID alone, so editing a source's table left the old table's values cached. Also drop the banner comment in the prometheus router.
1 parent e1c5b9f commit 5eb60be

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎packages/api/src/routers/api/prometheus.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ function getParams(req: express.Request): Record<string, string> {
130130
};
131131
}
132132

133-
// --------------------------
134-
// Source resolution
135-
// --------------------------
136-
137133
type PromqlTarget = {
138134
connection: NonNullable<Awaited<ReturnType<typeof getConnectionById>>>;
139135
databaseName: string;

‎packages/app/src/__tests__/source.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
useChartNumberFormats,
1919
useSingleSeriesNumberFormat,
2020
useSources,
21+
useUpdateSource,
2122
} from '@/source';
2223

2324
jest.mock('../api', () => ({ hdxServer: jest.fn() }));
@@ -31,7 +32,7 @@ jest.mock('@tanstack/react-query', () => ({
3132
useQueryClient: jest.fn(),
3233
}));
3334

34-
import { useQuery } from '@tanstack/react-query';
35+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
3536

3637
import { hdxServer } from '@/api';
3738

@@ -231,6 +232,30 @@ describe('getTraceDurationNumberFormat', () => {
231232
});
232233
});
233234

235+
describe('useUpdateSource', () => {
236+
it("drops the updated source's cached PromQL lookups", () => {
237+
const invalidateQueries = jest.fn();
238+
// @ts-expect-error return value type
239+
jest.mocked(useQueryClient).mockReturnValue({ invalidateQueries });
240+
let onSuccess: ((data: unknown, vars: unknown) => void) | undefined;
241+
// @ts-expect-error param type
242+
jest.mocked(useMutation).mockImplementation(options => {
243+
// @ts-expect-error return value type
244+
onSuccess = options.onSuccess;
245+
});
246+
247+
useUpdateSource();
248+
onSuccess?.(undefined, { source: { id: 'source-promql' } });
249+
250+
expect(invalidateQueries.mock.calls.map(([arg]) => arg.queryKey)).toEqual([
251+
['sources'],
252+
['promql-metric-names', 'source-promql'],
253+
['promql-label-names', 'source-promql'],
254+
['dashboard-filter-promql-label-values', 'source-promql'],
255+
]);
256+
});
257+
});
258+
234259
describe('useSources validation notifications', () => {
235260
const mockUseQuery = useQuery as jest.Mock;
236261
const mockHdxServer = hdxServer as jest.Mock;

‎packages/app/src/source.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ export function useSource({
205205
});
206206
}
207207

208+
// PromQL lookups are cached by source ID alone, so a source pointed at another
209+
// table would keep serving the old table's values until they go stale.
210+
const PROMQL_SOURCE_QUERY_KEYS = [
211+
'promql-metric-names',
212+
'promql-label-names',
213+
'dashboard-filter-promql-label-values',
214+
];
215+
208216
export function useUpdateSource() {
209217
const queryClient = useQueryClient();
210218

@@ -219,8 +227,11 @@ export function useUpdateSource() {
219227
json: source,
220228
});
221229
},
222-
onSuccess: () => {
230+
onSuccess: (_data, { source }) => {
223231
queryClient.invalidateQueries({ queryKey: ['sources'] });
232+
for (const key of PROMQL_SOURCE_QUERY_KEYS) {
233+
queryClient.invalidateQueries({ queryKey: [key, source.id] });
234+
}
224235
},
225236
});
226237
}

0 commit comments

Comments
 (0)