Commit c99d6bd5 by David Committed by GitHub

Merge pull request #11678 from grafana/davkal/11673-silent-response

Add silent option to backend requests
parents f2b5b6f2 707700ac
......@@ -170,7 +170,9 @@ export class BackendSrv {
return this.$http(options)
.then(response => {
if (!options.silent) {
appEvents.emit('ds-request-response', response);
}
return response;
})
.catch(err => {
......@@ -201,8 +203,9 @@ export class BackendSrv {
if (err.data && !err.data.message && _.isString(err.data.error)) {
err.data.message = err.data.error;
}
if (!options.silent) {
appEvents.emit('ds-request-error', err);
}
throw err;
})
.finally(() => {
......
......@@ -5,6 +5,7 @@ import kbn from 'app/core/utils/kbn';
import * as dateMath from 'app/core/utils/datemath';
import PrometheusMetricFindQuery from './metric_find_query';
import { ResultTransformer } from './result_transformer';
import { BackendSrv } from 'app/core/services/backend_srv';
export function prometheusRegularEscape(value) {
return value.replace(/'/g, "\\\\'");
......@@ -29,7 +30,7 @@ export class PrometheusDatasource {
resultTransformer: ResultTransformer;
/** @ngInject */
constructor(instanceSettings, private $q, private backendSrv, private templateSrv, private timeSrv) {
constructor(instanceSettings, private $q, private backendSrv: BackendSrv, private templateSrv, private timeSrv) {
this.type = 'prometheus';
this.editorSrc = 'app/features/prometheus/partials/query.editor.html';
this.name = instanceSettings.name;
......@@ -43,13 +44,13 @@ export class PrometheusDatasource {
this.resultTransformer = new ResultTransformer(templateSrv);
}
_request(method, url, data?, requestId?) {
_request(url, data?, options?: any) {
var options: any = {
url: this.url + url,
method: method,
requestId: requestId,
method: this.httpMethod,
...options,
};
if (method === 'GET') {
if (options.method === 'GET') {
if (!_.isEmpty(data)) {
options.url =
options.url +
......@@ -81,6 +82,11 @@ export class PrometheusDatasource {
return this.backendSrv.datasourceRequest(options);
}
// Use this for tab completion features, wont publish response to other components
metadataRequest(url) {
return this._request(url, null, { method: 'GET', silent: true });
}
interpolateQueryExpr(value, variable, defaultFormatFn) {
// if no multi or include all do not regexEscape
if (!variable.multi && !variable.includeAll) {
......@@ -206,7 +212,7 @@ export class PrometheusDatasource {
end: end,
step: query.step,
};
return this._request(this.httpMethod, url, data, query.requestId);
return this._request(url, data, { requestId: query.requestId });
}
performInstantQuery(query, time) {
......@@ -215,7 +221,7 @@ export class PrometheusDatasource {
query: query.expr,
time: time,
};
return this._request(this.httpMethod, url, data, query.requestId);
return this._request(url, data, { requestId: query.requestId });
}
performSuggestQuery(query, cache = false) {
......@@ -229,7 +235,7 @@ export class PrometheusDatasource {
);
}
return this._request('GET', url).then(result => {
return this.metadataRequest(url).then(result => {
this.metricsNameCache = {
data: result.data.data,
expire: Date.now() + 60 * 1000,
......
......@@ -46,7 +46,7 @@ export default class PrometheusMetricFindQuery {
// return label values globally
url = '/api/v1/label/' + label + '/values';
return this.datasource._request('GET', url).then(function(result) {
return this.datasource.metadataRequest(url).then(function(result) {
return _.map(result.data.data, function(value) {
return { text: value };
});
......@@ -56,7 +56,7 @@ export default class PrometheusMetricFindQuery {
var end = this.datasource.getPrometheusTime(this.range.to, true);
url = '/api/v1/series?match[]=' + encodeURIComponent(metric) + '&start=' + start + '&end=' + end;
return this.datasource._request('GET', url).then(function(result) {
return this.datasource.metadataRequest(url).then(function(result) {
var _labels = _.map(result.data.data, function(metric) {
return metric[label] || '';
}).filter(function(label) {
......@@ -76,7 +76,7 @@ export default class PrometheusMetricFindQuery {
metricNameQuery(metricFilterPattern) {
var url = '/api/v1/label/__name__/values';
return this.datasource._request('GET', url).then(function(result) {
return this.datasource.metadataRequest(url).then(function(result) {
return _.chain(result.data.data)
.filter(function(metricName) {
var r = new RegExp(metricFilterPattern);
......@@ -120,7 +120,7 @@ export default class PrometheusMetricFindQuery {
var url = '/api/v1/series?match[]=' + encodeURIComponent(query) + '&start=' + start + '&end=' + end;
var self = this;
return this.datasource._request('GET', url).then(function(result) {
return this.datasource.metadataRequest(url).then(function(result) {
return _.map(result.data.data, function(metric) {
return {
text: self.datasource.getOriginalMetricName(metric),
......
......@@ -14,6 +14,7 @@ describe('PrometheusDatasource', () => {
};
ctx.backendSrvMock = {};
ctx.templateSrvMock = {
replace: a => a,
};
......@@ -23,6 +24,25 @@ describe('PrometheusDatasource', () => {
ctx.ds = new PrometheusDatasource(instanceSettings, q, ctx.backendSrvMock, ctx.templateSrvMock, ctx.timeSrvMock);
});
describe('Datasource metadata requests', () => {
it('should perform a GET request with the default config', () => {
ctx.backendSrvMock.datasourceRequest = jest.fn();
ctx.ds.metadataRequest('/foo');
expect(ctx.backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
expect(ctx.backendSrvMock.datasourceRequest.mock.calls[0][0].method).toBe('GET');
});
it('should still perform a GET request with the DS HTTP method set to POST', () => {
ctx.backendSrvMock.datasourceRequest = jest.fn();
const postSettings = _.cloneDeep(instanceSettings);
postSettings.jsonData.httpMethod = 'POST';
const ds = new PrometheusDatasource(postSettings, q, ctx.backendSrvMock, ctx.templateSrvMock, ctx.timeSrvMock);
ds.metadataRequest('/foo');
expect(ctx.backendSrvMock.datasourceRequest.mock.calls.length).toBe(1);
expect(ctx.backendSrvMock.datasourceRequest.mock.calls[0][0].method).toBe('GET');
});
});
describe('When converting prometheus histogram to heatmap format', () => {
beforeEach(() => {
ctx.query = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment