Prometheus: Fix fetching label values when datasource has no labels match api support (#72960)

* interpolate match string

* provide unit test

* add the third parameter back to fix the unit test
This commit is contained in:
ismail simsek 2023-08-07 16:13:08 +03:00 committed by GitHub
parent 6ac3348021
commit 29906847e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -274,6 +274,27 @@ describe('Language completion provider', () => {
undefined
);
});
it('should call old series endpoint and should use match[] parameter and interpolate the template variables', () => {
const languageProvider = new LanguageProvider({
...defaultDatasource,
interpolateString: (string: string) => string.replace(/\$/, 'interpolated-'),
} as PrometheusDatasource);
const getSeriesValues = languageProvider.getSeriesValues;
const requestSpy = jest.spyOn(languageProvider, 'request');
getSeriesValues('job', '{instance="$instance", job="grafana"}');
expect(requestSpy).toHaveBeenCalled();
expect(requestSpy).toHaveBeenCalledWith(
'/api/v1/series',
[],
{
end: toPrometheusTimeString,
'match[]': '{instance="interpolated-instance", job="grafana"}',
start: fromPrometheusTimeString,
},
undefined
);
});
});
describe('fetchSeries', () => {

View File

@ -567,10 +567,11 @@ export default class PromQlLanguageProvider extends LanguageProvider {
*/
fetchSeriesValuesWithMatch = async (name: string, match?: string): Promise<string[]> => {
const interpolatedName = name ? this.datasource.interpolateString(name) : null;
const interpolatedMatch = match ? this.datasource.interpolateString(match) : null;
const range = this.datasource.getAdjustedInterval();
const urlParams = {
...range,
...(match && { 'match[]': match }),
...(interpolatedMatch && { 'match[]': interpolatedMatch }),
};
// @todo clean up prometheusResourceBrowserCache feature flag