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 { ...@@ -170,7 +170,9 @@ export class BackendSrv {
return this.$http(options) return this.$http(options)
.then(response => { .then(response => {
appEvents.emit('ds-request-response', response); if (!options.silent) {
appEvents.emit('ds-request-response', response);
}
return response; return response;
}) })
.catch(err => { .catch(err => {
...@@ -201,8 +203,9 @@ export class BackendSrv { ...@@ -201,8 +203,9 @@ export class BackendSrv {
if (err.data && !err.data.message && _.isString(err.data.error)) { if (err.data && !err.data.message && _.isString(err.data.error)) {
err.data.message = err.data.error; err.data.message = err.data.error;
} }
if (!options.silent) {
appEvents.emit('ds-request-error', err); appEvents.emit('ds-request-error', err);
}
throw err; throw err;
}) })
.finally(() => { .finally(() => {
......
...@@ -5,6 +5,7 @@ import kbn from 'app/core/utils/kbn'; ...@@ -5,6 +5,7 @@ import kbn from 'app/core/utils/kbn';
import * as dateMath from 'app/core/utils/datemath'; import * as dateMath from 'app/core/utils/datemath';
import PrometheusMetricFindQuery from './metric_find_query'; import PrometheusMetricFindQuery from './metric_find_query';
import { ResultTransformer } from './result_transformer'; import { ResultTransformer } from './result_transformer';
import { BackendSrv } from 'app/core/services/backend_srv';
export function prometheusRegularEscape(value) { export function prometheusRegularEscape(value) {
return value.replace(/'/g, "\\\\'"); return value.replace(/'/g, "\\\\'");
...@@ -29,7 +30,7 @@ export class PrometheusDatasource { ...@@ -29,7 +30,7 @@ export class PrometheusDatasource {
resultTransformer: ResultTransformer; resultTransformer: ResultTransformer;
/** @ngInject */ /** @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.type = 'prometheus';
this.editorSrc = 'app/features/prometheus/partials/query.editor.html'; this.editorSrc = 'app/features/prometheus/partials/query.editor.html';
this.name = instanceSettings.name; this.name = instanceSettings.name;
...@@ -43,13 +44,13 @@ export class PrometheusDatasource { ...@@ -43,13 +44,13 @@ export class PrometheusDatasource {
this.resultTransformer = new ResultTransformer(templateSrv); this.resultTransformer = new ResultTransformer(templateSrv);
} }
_request(method, url, data?, requestId?) { _request(url, data?, options?: any) {
var options: any = { var options: any = {
url: this.url + url, url: this.url + url,
method: method, method: this.httpMethod,
requestId: requestId, ...options,
}; };
if (method === 'GET') { if (options.method === 'GET') {
if (!_.isEmpty(data)) { if (!_.isEmpty(data)) {
options.url = options.url =
options.url + options.url +
...@@ -81,6 +82,11 @@ export class PrometheusDatasource { ...@@ -81,6 +82,11 @@ export class PrometheusDatasource {
return this.backendSrv.datasourceRequest(options); 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) { interpolateQueryExpr(value, variable, defaultFormatFn) {
// if no multi or include all do not regexEscape // if no multi or include all do not regexEscape
if (!variable.multi && !variable.includeAll) { if (!variable.multi && !variable.includeAll) {
...@@ -206,7 +212,7 @@ export class PrometheusDatasource { ...@@ -206,7 +212,7 @@ export class PrometheusDatasource {
end: end, end: end,
step: query.step, step: query.step,
}; };
return this._request(this.httpMethod, url, data, query.requestId); return this._request(url, data, { requestId: query.requestId });
} }
performInstantQuery(query, time) { performInstantQuery(query, time) {
...@@ -215,7 +221,7 @@ export class PrometheusDatasource { ...@@ -215,7 +221,7 @@ export class PrometheusDatasource {
query: query.expr, query: query.expr,
time: time, time: time,
}; };
return this._request(this.httpMethod, url, data, query.requestId); return this._request(url, data, { requestId: query.requestId });
} }
performSuggestQuery(query, cache = false) { performSuggestQuery(query, cache = false) {
...@@ -229,7 +235,7 @@ export class PrometheusDatasource { ...@@ -229,7 +235,7 @@ export class PrometheusDatasource {
); );
} }
return this._request('GET', url).then(result => { return this.metadataRequest(url).then(result => {
this.metricsNameCache = { this.metricsNameCache = {
data: result.data.data, data: result.data.data,
expire: Date.now() + 60 * 1000, expire: Date.now() + 60 * 1000,
......
...@@ -46,7 +46,7 @@ export default class PrometheusMetricFindQuery { ...@@ -46,7 +46,7 @@ export default class PrometheusMetricFindQuery {
// return label values globally // return label values globally
url = '/api/v1/label/' + label + '/values'; 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 _.map(result.data.data, function(value) {
return { text: value }; return { text: value };
}); });
...@@ -56,7 +56,7 @@ export default class PrometheusMetricFindQuery { ...@@ -56,7 +56,7 @@ export default class PrometheusMetricFindQuery {
var end = this.datasource.getPrometheusTime(this.range.to, true); var end = this.datasource.getPrometheusTime(this.range.to, true);
url = '/api/v1/series?match[]=' + encodeURIComponent(metric) + '&start=' + start + '&end=' + end; 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) { var _labels = _.map(result.data.data, function(metric) {
return metric[label] || ''; return metric[label] || '';
}).filter(function(label) { }).filter(function(label) {
...@@ -76,7 +76,7 @@ export default class PrometheusMetricFindQuery { ...@@ -76,7 +76,7 @@ export default class PrometheusMetricFindQuery {
metricNameQuery(metricFilterPattern) { metricNameQuery(metricFilterPattern) {
var url = '/api/v1/label/__name__/values'; 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) return _.chain(result.data.data)
.filter(function(metricName) { .filter(function(metricName) {
var r = new RegExp(metricFilterPattern); var r = new RegExp(metricFilterPattern);
...@@ -120,7 +120,7 @@ export default class PrometheusMetricFindQuery { ...@@ -120,7 +120,7 @@ export default class PrometheusMetricFindQuery {
var url = '/api/v1/series?match[]=' + encodeURIComponent(query) + '&start=' + start + '&end=' + end; var url = '/api/v1/series?match[]=' + encodeURIComponent(query) + '&start=' + start + '&end=' + end;
var self = this; 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 _.map(result.data.data, function(metric) {
return { return {
text: self.datasource.getOriginalMetricName(metric), text: self.datasource.getOriginalMetricName(metric),
......
...@@ -14,6 +14,7 @@ describe('PrometheusDatasource', () => { ...@@ -14,6 +14,7 @@ describe('PrometheusDatasource', () => {
}; };
ctx.backendSrvMock = {}; ctx.backendSrvMock = {};
ctx.templateSrvMock = { ctx.templateSrvMock = {
replace: a => a, replace: a => a,
}; };
...@@ -23,6 +24,25 @@ describe('PrometheusDatasource', () => { ...@@ -23,6 +24,25 @@ describe('PrometheusDatasource', () => {
ctx.ds = new PrometheusDatasource(instanceSettings, q, ctx.backendSrvMock, ctx.templateSrvMock, ctx.timeSrvMock); 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', () => { describe('When converting prometheus histogram to heatmap format', () => {
beforeEach(() => { beforeEach(() => {
ctx.query = { 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