Add tracing headers for prometheus datasource (#16724)

Prometheus: Adds tracing headers for Prometheus datasource
This commit is contained in:
Stanislav Putrya 2019-04-30 13:28:35 +02:00 committed by Carl Bergquist
parent 0433af6385
commit 76ab0aa059
2 changed files with 37 additions and 8 deletions

View File

@ -59,6 +59,15 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
return query.expr;
}
_addTracingHeaders(httpOptions: any, options: any) {
httpOptions.headers = options.headers || {};
const proxyMode = !this.url.match(/^http/);
if (proxyMode) {
httpOptions.headers['X-Dashboard-Id'] = options.dashboardId;
httpOptions.headers['X-Panel-Id'] = options.panelId;
}
}
_request(url, data?, options?: any) {
options = _.defaults(options || {}, {
url: this.url + url,
@ -75,9 +84,7 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
}).join('&');
}
} else {
options.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
};
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
options.transformRequest = data => {
return $.param(data);
};
@ -89,9 +96,7 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
}
if (this.basicAuth) {
options.headers = {
Authorization: this.basicAuth,
};
options.headers.Authorization = this.basicAuth;
}
return this.backendSrv.datasourceRequest(options);
@ -233,6 +238,7 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
const adjusted = alignRange(start, end, query.step);
query.start = adjusted.start;
query.end = adjusted.end;
this._addTracingHeaders(query, options);
return query;
}
@ -261,7 +267,7 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
if (this.queryTimeout) {
data['timeout'] = this.queryTimeout;
}
return this._request(url, data, { requestId: query.requestId });
return this._request(url, data, { requestId: query.requestId, headers: query.headers });
}
performInstantQuery(query, time) {
@ -273,7 +279,7 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
if (this.queryTimeout) {
data['timeout'] = this.queryTimeout;
}
return this._request(url, data, { requestId: query.requestId });
return this._request(url, data, { requestId: query.requestId, headers: query.headers });
}
performSuggestQuery(query, cache = false) {

View File

@ -1232,4 +1232,27 @@ describe('PrometheusDatasource for POST', () => {
expect(results.data[0].target).toBe('test{job="testjob"}');
});
});
describe('When querying prometheus via check headers X-Dashboard-Id and X-Panel-Id', () => {
const options = { dashboardId: 1, panelId: 2 };
const httpOptions = {
headers: {},
};
it('with proxy access tracing headers should be added', () => {
ctx.ds = new PrometheusDatasource(instanceSettings, q, backendSrv as any, templateSrv, timeSrv);
ctx.ds._addTracingHeaders(httpOptions, options);
expect(httpOptions.headers['X-Dashboard-Id']).toBe(1);
expect(httpOptions.headers['X-Panel-Id']).toBe(2);
});
it('with direct access tracing headers should not be added', () => {
instanceSettings.url = 'http://127.0.0.1:8000';
ctx.ds = new PrometheusDatasource(instanceSettings, q, backendSrv as any, templateSrv, timeSrv);
ctx.ds._addTracingHeaders(httpOptions, options);
expect(httpOptions.headers['X-Dashboard-Id']).toBe(undefined);
expect(httpOptions.headers['X-Panel-Id']).toBe(undefined);
});
});
});