Commit 16107f37 by Alexandru Bumbacea Committed by GitHub

* prometheus fix variables fetching when customQueryParameters used #28907 (#28949)

parent b904e0c6
......@@ -90,7 +90,7 @@ export const PromSettings = (props: Props) => {
<FormField
label="Custom query parameters"
labelWidth={14}
tooltip="Add Custom parameters to Prometheus or Thanos queries."
tooltip="Add Custom parameters to all Prometheus or Thanos queries."
inputEl={
<Input
className="width-25"
......
......@@ -59,7 +59,9 @@ describe('PrometheusDatasource', () => {
directUrl: 'direct',
user: 'test',
password: 'mupp',
jsonData: {} as any,
jsonData: {
customQueryParameters: '',
} as any,
} as unknown) as DataSourceInstanceSettings<PromOptions>;
beforeEach(() => {
......@@ -141,6 +143,30 @@ describe('PrometheusDatasource', () => {
});
});
describe('When using customQueryParams', () => {
const promDs = new PrometheusDatasource(
{ ...instanceSettings, jsonData: { customQueryParameters: 'customQuery=123' } as any },
templateSrvStub as any,
timeSrvStub as any
);
it('added to metadata request', () => {
promDs.metadataRequest('/foo');
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0].url).toBe('proxied/foo?customQuery=123');
});
it('added to query', () => {
promDs.query({
range: { from: time({ seconds: 63 }), to: time({ seconds: 183 }) },
targets: [{ expr: 'test{job="testjob"}', format: 'time_series' }],
interval: '60s',
} as any);
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0].url).toBe(
'proxied/api/v1/query_range?query=test%7Bjob%3D%22testjob%22%7D&start=60&end=180&step=60&customQuery=123'
);
});
});
describe('When using adhoc filters', () => {
const DEFAULT_QUERY_EXPRESSION = 'metric{job="foo"} - metric';
const target = { expr: DEFAULT_QUERY_EXPRESSION };
......
......@@ -111,7 +111,7 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
if (data && Object.keys(data).length) {
options.url =
options.url +
'?' +
(options.url.search(/\?/) >= 0 ? '&' : '?') +
Object.entries(data)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&');
......@@ -134,7 +134,13 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
// Use this for tab completion features, wont publish response to other components
metadataRequest<T = any>(url: string) {
return this._request<T>(url, null, { method: 'GET', hideFromInspector: true }).toPromise(); // toPromise until we change getTagValues, getTagKeys to Observable
const data: any = {};
for (const [key, value] of this.customQueryParameters) {
if (data[key] == null) {
data[key] = value;
}
}
return this._request<T>(url, data, { method: 'GET', hideFromInspector: true }).toPromise(); // toPromise until we change getTagValues, getTagKeys to Observable
}
interpolateQueryExpr(value: string | string[] = [], variable: any) {
......
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