Prometheus: Fix running of health check query based on access mode (#42189) (#42213)

* Prometheus: Use this.query for health check

* Update alertMessage in e2e tests

(cherry picked from commit 1b4f6b19cb)

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
This commit is contained in:
Grot (@grafanabot) 2021-11-24 07:04:44 -05:00 committed by GitHub
parent 0baba05ed5
commit df6223024f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 6 deletions

View File

@ -4,7 +4,7 @@ const dataSourceName = 'PromExemplar';
const addDataSource = () => { const addDataSource = () => {
e2e.flows.addDataSource({ e2e.flows.addDataSource({
type: 'Prometheus', type: 'Prometheus',
expectedAlertMessage: 'Bad Gateway', expectedAlertMessage: 'Error reading Prometheus',
name: dataSourceName, name: dataSourceName,
form: () => { form: () => {
e2e.components.DataSource.Prometheus.configPage.exemplarsAddButton().click(); e2e.components.DataSource.Prometheus.configPage.exemplarsAddButton().click();

View File

@ -16,6 +16,7 @@ import {
ScopedVars, ScopedVars,
TimeRange, TimeRange,
DataFrame, DataFrame,
dateTime,
} from '@grafana/data'; } from '@grafana/data';
import { import {
BackendSrvRequest, BackendSrvRequest,
@ -789,11 +790,33 @@ export class PrometheusDatasource extends DataSourceWithBackend<PromQuery, PromO
async testDatasource() { async testDatasource() {
const now = new Date().getTime(); const now = new Date().getTime();
const query = { expr: '1+1' } as PromQueryRequest; const request: DataQueryRequest<PromQuery> = {
const response = await lastValueFrom(this.performInstantQuery(query, now / 1000)); targets: [{ refId: 'test', expr: '1+1', instant: true }],
return response.data.status === 'success' requestId: `${this.id}-health`,
? { status: 'success', message: 'Data source is working' } scopedVars: {},
: { status: 'error', message: response.data.error }; dashboardId: 0,
panelId: 0,
interval: '1m',
intervalMs: 60000,
maxDataPoints: 1,
range: {
from: dateTime(now - 1000),
to: dateTime(now),
},
} as DataQueryRequest<PromQuery>;
return lastValueFrom(this.query(request))
.then((res: DataQueryResponse) => {
if (!res || !res.data || res.state !== LoadingState.Done) {
return { status: 'error', message: `Error reading Prometheus: ${res?.error?.message}` };
} else {
return { status: 'success', message: 'Data source is working' };
}
})
.catch((err: any) => {
console.error('Prometheus Error', err);
return { status: 'error', message: err.message };
});
} }
interpolateVariablesInQueries(queries: PromQuery[], scopedVars: ScopedVars): PromQuery[] { interpolateVariablesInQueries(queries: PromQuery[], scopedVars: ScopedVars): PromQuery[] {