mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Add tracing headers for prometheus datasource (#16724)
Prometheus: Adds tracing headers for Prometheus datasource
This commit is contained in:
parent
0433af6385
commit
76ab0aa059
@ -59,6 +59,15 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
|
|||||||
return query.expr;
|
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) {
|
_request(url, data?, options?: any) {
|
||||||
options = _.defaults(options || {}, {
|
options = _.defaults(options || {}, {
|
||||||
url: this.url + url,
|
url: this.url + url,
|
||||||
@ -75,9 +84,7 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
|
|||||||
}).join('&');
|
}).join('&');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
options.headers = {
|
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
|
||||||
};
|
|
||||||
options.transformRequest = data => {
|
options.transformRequest = data => {
|
||||||
return $.param(data);
|
return $.param(data);
|
||||||
};
|
};
|
||||||
@ -89,9 +96,7 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.basicAuth) {
|
if (this.basicAuth) {
|
||||||
options.headers = {
|
options.headers.Authorization = this.basicAuth;
|
||||||
Authorization: this.basicAuth,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.backendSrv.datasourceRequest(options);
|
return this.backendSrv.datasourceRequest(options);
|
||||||
@ -233,6 +238,7 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
|
|||||||
const adjusted = alignRange(start, end, query.step);
|
const adjusted = alignRange(start, end, query.step);
|
||||||
query.start = adjusted.start;
|
query.start = adjusted.start;
|
||||||
query.end = adjusted.end;
|
query.end = adjusted.end;
|
||||||
|
this._addTracingHeaders(query, options);
|
||||||
|
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
@ -261,7 +267,7 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
|
|||||||
if (this.queryTimeout) {
|
if (this.queryTimeout) {
|
||||||
data['timeout'] = 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) {
|
performInstantQuery(query, time) {
|
||||||
@ -273,7 +279,7 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
|
|||||||
if (this.queryTimeout) {
|
if (this.queryTimeout) {
|
||||||
data['timeout'] = 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) {
|
performSuggestQuery(query, cache = false) {
|
||||||
|
@ -1232,4 +1232,27 @@ describe('PrometheusDatasource for POST', () => {
|
|||||||
expect(results.data[0].target).toBe('test{job="testjob"}');
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user