From 565430f2975a5a8d06bf92bcd1c809f8c934dc42 Mon Sep 17 00:00:00 2001 From: Darren Janeczek <38694490+darrenjaneczek@users.noreply.github.com> Date: Thu, 21 Jul 2022 09:45:02 -0400 Subject: [PATCH] Prometheus: Don't show errors from unsuccessful API checks like rules or exemplar checks (#52193) The query inspector was experiencing some error pop-ups due to errors encountered on metrics datasources configured with multiple tenants. e.g., team-a|team-b This fix suppresses those errors so as to not mislead the user. --- .../datasource/prometheus/datasource.tsx | 23 +++++++++++++------ .../prometheus/language_provider.ts | 9 +++++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/datasource.tsx b/public/app/plugins/datasource/prometheus/datasource.tsx index c6a3f089e90..98b72c1bd46 100644 --- a/public/app/plugins/datasource/prometheus/datasource.tsx +++ b/public/app/plugins/datasource/prometheus/datasource.tsx @@ -213,7 +213,7 @@ export class PrometheusDatasource } // Use this for tab completion features, wont publish response to other components - async metadataRequest(url: string, params = {}) { + async metadataRequest(url: string, params = {}, options?: Partial) { // If URL includes endpoint that supports POST and GET method, try to use configured method. This might fail as POST is supported only in v2.10+. if (GET_AND_POST_METADATA_ENDPOINTS.some((endpoint) => url.includes(endpoint))) { try { @@ -222,6 +222,7 @@ export class PrometheusDatasource method: this.httpMethod, hideFromInspector: true, showErrorAlert: false, + ...options, }) ); } catch (err) { @@ -238,6 +239,7 @@ export class PrometheusDatasource this._request(`/api/datasources/${this.id}/resources${url}`, params, { method: 'GET', hideFromInspector: true, + ...options, }) ); // toPromise until we change getTagValues, getTagKeys to Observable } @@ -1003,7 +1005,7 @@ export class PrometheusDatasource async loadRules() { try { - const res = await this.metadataRequest('/api/v1/rules'); + const res = await this.metadataRequest('/api/v1/rules', {}, { showErrorAlert: false }); const groups = res.data?.data?.groups; if (groups) { @@ -1017,11 +1019,18 @@ export class PrometheusDatasource async areExemplarsAvailable() { try { - const res = await this.getResource('/api/v1/query_exemplars', { - query: 'test', - start: dateTime().subtract(30, 'minutes').valueOf(), - end: dateTime().valueOf(), - }); + const res = await this.metadataRequest( + '/api/v1/query_exemplars', + { + query: 'test', + start: dateTime().subtract(30, 'minutes').valueOf().toString(), + end: dateTime().valueOf().toString(), + }, + { + // Avoid alerting the user if this test fails + showErrorAlert: false, + } + ); if (res.data.status === 'success') { return true; } diff --git a/public/app/plugins/datasource/prometheus/language_provider.ts b/public/app/plugins/datasource/prometheus/language_provider.ts index 66bf546a016..e88827e7cf7 100644 --- a/public/app/plugins/datasource/prometheus/language_provider.ts +++ b/public/app/plugins/datasource/prometheus/language_provider.ts @@ -11,6 +11,7 @@ import { HistoryItem, LanguageProvider, } from '@grafana/data'; +import { BackendSrvRequest } from '@grafana/runtime'; import { CompletionItem, CompletionItemGroup, SearchFunctionType, TypeaheadInput, TypeaheadOutput } from '@grafana/ui'; import { PrometheusDatasource } from './datasource'; @@ -120,9 +121,9 @@ export default class PromQlLanguageProvider extends LanguageProvider { return PromqlSyntax; } - request = async (url: string, defaultValue: any, params = {}): Promise => { + request = async (url: string, defaultValue: any, params = {}, options?: Partial): Promise => { try { - const res = await this.datasource.metadataRequest(url, params); + const res = await this.datasource.metadataRequest(url, params, options); return res.data.data; } catch (error) { console.error(error); @@ -145,7 +146,9 @@ export default class PromQlLanguageProvider extends LanguageProvider { }; async loadMetricsMetadata() { - this.metricsMetadata = fixSummariesMetadata(await this.request('/api/v1/metadata', {})); + this.metricsMetadata = fixSummariesMetadata( + await this.request('/api/v1/metadata', {}, {}, { showErrorAlert: false }) + ); } getLabelKeys(): string[] {