mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Prometheus: Use fetch instead of deprecated datasourceRequest (#27090)
* Prometheus: replaces dataSourcRequest with fetch * Tests: fixes typings * Refactor: removes unnecessary from operator * Refactor: removes weird json() function calls * Update public/app/plugins/datasource/prometheus/metric_find_query.test.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> * Update public/app/plugins/datasource/prometheus/metric_find_query.test.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> * Update public/app/plugins/datasource/prometheus/metric_find_query.test.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> * Update public/app/plugins/datasource/prometheus/metric_find_query.test.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> * Update public/app/plugins/datasource/prometheus/metric_find_query.test.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> * Update public/app/plugins/datasource/prometheus/metric_find_query.test.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> * Update public/app/plugins/datasource/prometheus/metric_find_query.test.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> * Update public/app/plugins/datasource/prometheus/metric_find_query.test.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> * Update public/app/plugins/datasource/prometheus/metric_find_query.test.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> * Update public/app/plugins/datasource/prometheus/metric_find_query.test.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com>
This commit is contained in:
@@ -18,13 +18,13 @@ import {
|
||||
TimeRange,
|
||||
TimeSeries,
|
||||
} from '@grafana/data';
|
||||
import { forkJoin, from, merge, Observable, of } from 'rxjs';
|
||||
import { filter, map, tap } from 'rxjs/operators';
|
||||
import { forkJoin, merge, Observable, of, throwError } from 'rxjs';
|
||||
import { catchError, filter, map, tap } from 'rxjs/operators';
|
||||
|
||||
import PrometheusMetricFindQuery from './metric_find_query';
|
||||
import { ResultTransformer } from './result_transformer';
|
||||
import PrometheusLanguageProvider from './language_provider';
|
||||
import { getBackendSrv, BackendSrvRequest } from '@grafana/runtime';
|
||||
import { BackendSrvRequest, getBackendSrv } from '@grafana/runtime';
|
||||
import addLabelToQuery from './add_label_to_query';
|
||||
import { getQueryHints } from './query_hints';
|
||||
import { expandRecordingRules } from './language_utils';
|
||||
@@ -111,7 +111,7 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
}
|
||||
}
|
||||
|
||||
_request(url: string, data: Record<string, string> | null, overrides: Partial<BackendSrvRequest> = {}) {
|
||||
_request<T = any>(url: string, data: Record<string, string> | null, overrides: Partial<BackendSrvRequest> = {}) {
|
||||
const options: BackendSrvRequest = defaults(overrides, {
|
||||
url: this.url + url,
|
||||
method: this.httpMethod,
|
||||
@@ -140,12 +140,12 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
options.headers!.Authorization = this.basicAuth;
|
||||
}
|
||||
|
||||
return getBackendSrv().datasourceRequest(options);
|
||||
return getBackendSrv().fetch<T>(options);
|
||||
}
|
||||
|
||||
// Use this for tab completion features, wont publish response to other components
|
||||
metadataRequest(url: string) {
|
||||
return this._request(url, null, { method: 'GET', hideFromInspector: true });
|
||||
metadataRequest<T = any>(url: string) {
|
||||
return this._request<T>(url, null, { method: 'GET', hideFromInspector: true }).toPromise(); // toPromise until we change getTagValues, getTagKeys to Observable
|
||||
}
|
||||
|
||||
interpolateQueryExpr(value: string | string[] = [], variable: any) {
|
||||
@@ -272,8 +272,8 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
const target = activeTargets[index];
|
||||
|
||||
let observable = query.instant
|
||||
? from(this.performInstantQuery(query, end))
|
||||
: from(this.performTimeSeriesQuery(query, query.start, query.end));
|
||||
? this.performInstantQuery(query, end)
|
||||
: this.performTimeSeriesQuery(query, query.start, query.end);
|
||||
|
||||
return observable.pipe(
|
||||
// Decrease the counter here. We assume that each request returns only single value and then completes
|
||||
@@ -305,8 +305,8 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
const target = activeTargets[index];
|
||||
|
||||
let observable = query.instant
|
||||
? from(this.performInstantQuery(query, end))
|
||||
: from(this.performTimeSeriesQuery(query, query.start, query.end));
|
||||
? this.performInstantQuery(query, end)
|
||||
: this.performTimeSeriesQuery(query, query.start, query.end);
|
||||
|
||||
return observable.pipe(
|
||||
filter((response: any) => (response.cancelled ? false : true)),
|
||||
@@ -448,13 +448,15 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
}
|
||||
}
|
||||
|
||||
return this._request(url, data, { requestId: query.requestId, headers: query.headers }).catch((err: any) => {
|
||||
if (err.cancelled) {
|
||||
return err;
|
||||
}
|
||||
return this._request(url, data, { requestId: query.requestId, headers: query.headers }).pipe(
|
||||
catchError(err => {
|
||||
if (err.cancelled) {
|
||||
return of(err);
|
||||
}
|
||||
|
||||
throw this.handleErrors(err, query);
|
||||
});
|
||||
return throwError(this.handleErrors(err, query));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
performInstantQuery(query: PromQueryRequest, time: number) {
|
||||
@@ -474,13 +476,15 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
}
|
||||
}
|
||||
|
||||
return this._request(url, data, { requestId: query.requestId, headers: query.headers }).catch((err: any) => {
|
||||
if (err.cancelled) {
|
||||
return err;
|
||||
}
|
||||
return this._request(url, data, { requestId: query.requestId, headers: query.headers }).pipe(
|
||||
catchError(err => {
|
||||
if (err.cancelled) {
|
||||
return of(err);
|
||||
}
|
||||
|
||||
throw this.handleErrors(err, query);
|
||||
});
|
||||
return throwError(this.handleErrors(err, query));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
handleErrors = (err: any, target: PromQuery) => {
|
||||
@@ -582,7 +586,11 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
const query = this.createQuery(queryModel, queryOptions, start, end);
|
||||
|
||||
const self = this;
|
||||
const response: PromDataQueryResponse = await this.performTimeSeriesQuery(query, query.start, query.end);
|
||||
const response: PromDataQueryResponse = await this.performTimeSeriesQuery(
|
||||
query,
|
||||
query.start,
|
||||
query.end
|
||||
).toPromise();
|
||||
const eventList: AnnotationEvent[] = [];
|
||||
const splitKeys = tagKeys.split(',');
|
||||
|
||||
@@ -662,7 +670,7 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
async testDatasource() {
|
||||
const now = new Date().getTime();
|
||||
const query = { expr: '1+1' } as PromQueryRequest;
|
||||
const response = await this.performInstantQuery(query, now / 1000);
|
||||
const response = await this.performInstantQuery(query, now / 1000).toPromise();
|
||||
return response.data.status === 'success'
|
||||
? { status: 'success', message: 'Data source is working' }
|
||||
: { status: 'error', message: response.error };
|
||||
@@ -690,9 +698,8 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
async loadRules() {
|
||||
try {
|
||||
const res = await this.metadataRequest('/api/v1/rules');
|
||||
const body = res.data || res.json();
|
||||
const groups = res.data?.data?.groups;
|
||||
|
||||
const groups = body?.data?.groups;
|
||||
if (groups) {
|
||||
this.ruleMappings = extractRuleMappingFromGroups(groups);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user