diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index 575b74b2dc5..85c04b7c9fa 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -59,6 +59,15 @@ export class PrometheusDatasource implements DataSourceApi { 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 { }).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 { } 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 { 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 { 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 { 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) { diff --git a/public/app/plugins/datasource/prometheus/specs/datasource.test.ts b/public/app/plugins/datasource/prometheus/specs/datasource.test.ts index 5c747eae74f..05808464115 100644 --- a/public/app/plugins/datasource/prometheus/specs/datasource.test.ts +++ b/public/app/plugins/datasource/prometheus/specs/datasource.test.ts @@ -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); + }); + }); });