From bd320b75ee4842d71dc1fc916aac2fa92b3a3fc1 Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Mon, 7 Aug 2023 16:22:02 +0300 Subject: [PATCH] [v10.0.x] Prometheus: Fix fetching label values when datasource has no labels match api support (#72984) 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 (cherry picked from commit 29906847e1d19f6d201559dca298f5de90d47422) Co-authored-by: ismail simsek --- .../prometheus/language_provider.test.ts | 21 +++++++++++++++++++ .../prometheus/language_provider.ts | 3 ++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/datasource/prometheus/language_provider.test.ts b/public/app/plugins/datasource/prometheus/language_provider.test.ts index b751647be22..406d2db26ac 100644 --- a/public/app/plugins/datasource/prometheus/language_provider.test.ts +++ b/public/app/plugins/datasource/prometheus/language_provider.test.ts @@ -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', () => { diff --git a/public/app/plugins/datasource/prometheus/language_provider.ts b/public/app/plugins/datasource/prometheus/language_provider.ts index b104c0a1df6..665758806c4 100644 --- a/public/app/plugins/datasource/prometheus/language_provider.ts +++ b/public/app/plugins/datasource/prometheus/language_provider.ts @@ -567,10 +567,11 @@ export default class PromQlLanguageProvider extends LanguageProvider { */ fetchSeriesValuesWithMatch = async (name: string, match?: string): Promise => { 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