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:
Hugo Häggmark
2020-08-20 21:03:59 -07:00
committed by GitHub
parent 0fb7ee05d1
commit a73e6f728d
5 changed files with 149 additions and 136 deletions

View File

@@ -1,8 +1,12 @@
import 'whatwg-fetch'; // fetch polyfill needed backendSrv
import { of } from 'rxjs';
import { DataSourceInstanceSettings, toUtc } from '@grafana/data';
import { PrometheusDatasource } from './datasource';
import PrometheusMetricFindQuery from './metric_find_query';
import { DataSourceInstanceSettings, toUtc } from '@grafana/data';
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
import { PromOptions } from './types';
import { FetchResponse } from '@grafana/runtime';
jest.mock('app/features/templating/template_srv', () => {
return {
@@ -16,7 +20,7 @@ jest.mock('@grafana/runtime', () => ({
getBackendSrv: () => backendSrv,
}));
const datasourceRequestMock = jest.spyOn(backendSrv, 'datasourceRequest');
const fetchMock = jest.spyOn(backendSrv, 'fetch');
const instanceSettings = ({
url: 'proxied',
@@ -54,7 +58,7 @@ describe('PrometheusMetricFindQuery', () => {
});
const setupMetricFindQuery = (data: any) => {
datasourceRequestMock.mockImplementation(() => Promise.resolve({ status: 'success', data: data.response }));
fetchMock.mockImplementation(() => of(({ status: 'success', data: data.response } as unknown) as FetchResponse));
return new PrometheusMetricFindQuery(ds, data.query);
};
@@ -69,8 +73,8 @@ describe('PrometheusMetricFindQuery', () => {
const results = await query.process();
expect(results).toHaveLength(3);
expect(datasourceRequestMock).toHaveBeenCalledTimes(1);
expect(datasourceRequestMock).toHaveBeenCalledWith({
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: 'proxied/api/v1/labels',
hideFromInspector: true,
@@ -88,8 +92,8 @@ describe('PrometheusMetricFindQuery', () => {
const results = await query.process();
expect(results).toHaveLength(3);
expect(datasourceRequestMock).toHaveBeenCalledTimes(1);
expect(datasourceRequestMock).toHaveBeenCalledWith({
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: 'proxied/api/v1/label/resource/values',
hideFromInspector: true,
@@ -111,8 +115,8 @@ describe('PrometheusMetricFindQuery', () => {
const results = await query.process();
expect(results).toHaveLength(3);
expect(datasourceRequestMock).toHaveBeenCalledTimes(1);
expect(datasourceRequestMock).toHaveBeenCalledWith({
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `proxied/api/v1/series?match${encodeURIComponent(
'[]'
@@ -136,8 +140,8 @@ describe('PrometheusMetricFindQuery', () => {
const results = await query.process();
expect(results).toHaveLength(3);
expect(datasourceRequestMock).toHaveBeenCalledTimes(1);
expect(datasourceRequestMock).toHaveBeenCalledWith({
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url:
'proxied/api/v1/series?match%5B%5D=metric%7Blabel1%3D%22foo%22%2C+label2%3D%22bar%22%2C+label3%3D%22baz%22%7D&start=1524650400&end=1524654000',
@@ -162,8 +166,8 @@ describe('PrometheusMetricFindQuery', () => {
expect(results).toHaveLength(2);
expect(results[0].text).toBe('value1');
expect(results[1].text).toBe('value2');
expect(datasourceRequestMock).toHaveBeenCalledTimes(1);
expect(datasourceRequestMock).toHaveBeenCalledWith({
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `proxied/api/v1/series?match${encodeURIComponent(
'[]'
@@ -183,8 +187,8 @@ describe('PrometheusMetricFindQuery', () => {
const results = await query.process();
expect(results).toHaveLength(3);
expect(datasourceRequestMock).toHaveBeenCalledTimes(1);
expect(datasourceRequestMock).toHaveBeenCalledWith({
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: 'proxied/api/v1/label/__name__/values',
hideFromInspector: true,
@@ -211,8 +215,8 @@ describe('PrometheusMetricFindQuery', () => {
expect(results).toHaveLength(1);
expect(results[0].text).toBe('metric{job="testjob"} 3846 1443454528000');
expect(datasourceRequestMock).toHaveBeenCalledTimes(1);
expect(datasourceRequestMock).toHaveBeenCalledWith({
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `proxied/api/v1/query?query=metric&time=${raw.to.unix()}`,
requestId: undefined,
@@ -237,8 +241,8 @@ describe('PrometheusMetricFindQuery', () => {
expect(results[0].text).toBe('up{instance="127.0.0.1:1234",job="job1"}');
expect(results[1].text).toBe('up{instance="127.0.0.1:5678",job="job1"}');
expect(results[2].text).toBe('up{instance="127.0.0.1:9102",job="job1"}');
expect(datasourceRequestMock).toHaveBeenCalledTimes(1);
expect(datasourceRequestMock).toHaveBeenCalledWith({
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `proxied/api/v1/series?match${encodeURIComponent('[]')}=${encodeURIComponent(
'up{job="job1"}'