Prometheus: Run Explore both queries trough backend
This commit is contained in:
@@ -2110,6 +2110,54 @@ describe('prepareTargets', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('prepareOptionsV2', () => {
|
||||
it('creates instant target', () => {
|
||||
const query: PromQuery = { refId: 'A', expr: 'go_goroutines', instant: true, format: 'table' };
|
||||
const instanceSettings = ({ jsonData: {} } as unknown) as DataSourceInstanceSettings<PromOptions>;
|
||||
const ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any);
|
||||
const instantTarget = ds.createTargetV2(query, 'instant');
|
||||
expect(instantTarget.refId).toEqual('A');
|
||||
expect(instantTarget.range).toEqual(false);
|
||||
expect(instantTarget.instant).toEqual(true);
|
||||
expect(instantTarget.format).toEqual('table');
|
||||
});
|
||||
|
||||
it('creates range target', () => {
|
||||
const query: PromQuery = { refId: 'A', expr: 'go_goroutines', range: true, format: 'time_series' };
|
||||
const instanceSettings = ({ jsonData: {} } as unknown) as DataSourceInstanceSettings<PromOptions>;
|
||||
const ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any);
|
||||
const rangeTarget = ds.createTargetV2(query, 'range');
|
||||
expect(rangeTarget.refId).toEqual('A');
|
||||
expect(rangeTarget.range).toEqual(true);
|
||||
expect(rangeTarget.instant).toEqual(false);
|
||||
expect(rangeTarget.format).toEqual('time_series');
|
||||
});
|
||||
|
||||
it('creates both targets', () => {
|
||||
const query: PromQuery = { refId: 'A', expr: 'go_goroutines', range: true, instant: true };
|
||||
const instanceSettings = ({ jsonData: {} } as unknown) as DataSourceInstanceSettings<PromOptions>;
|
||||
const ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any);
|
||||
const instantTarget = ds.createTargetV2(query, 'instant', 'table', true);
|
||||
const rangeTarget = ds.createTargetV2(query, 'range', 'time_series', true);
|
||||
expect(instantTarget.refId).toEqual('A_instant');
|
||||
expect(rangeTarget.refId).toEqual('A');
|
||||
expect(instantTarget.range).toEqual(false);
|
||||
expect(rangeTarget.range).toEqual(true);
|
||||
expect(instantTarget.instant).toEqual(true);
|
||||
expect(rangeTarget.instant).toEqual(false);
|
||||
expect(instantTarget.format).toEqual('table');
|
||||
expect(rangeTarget.format).toEqual('time_series');
|
||||
});
|
||||
|
||||
it('overrides the format', () => {
|
||||
const query: PromQuery = { refId: 'A', expr: 'go_goroutines', range: true, format: 'time_series' };
|
||||
const instanceSettings = ({ jsonData: {} } as unknown) as DataSourceInstanceSettings<PromOptions>;
|
||||
const ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any);
|
||||
const rangeTarget = ds.createTargetV2(query, 'range', 'table');
|
||||
expect(rangeTarget.format).toEqual('table');
|
||||
});
|
||||
});
|
||||
|
||||
describe('modifyQuery', () => {
|
||||
describe('when called with ADD_FILTER', () => {
|
||||
describe('and query has no labels', () => {
|
||||
|
||||
@@ -289,19 +289,41 @@ export class PrometheusDatasource extends DataSourceWithBackend<PromQuery, PromO
|
||||
};
|
||||
};
|
||||
|
||||
createTargetV2 = (
|
||||
target: PromQuery,
|
||||
type: 'instant' | 'range',
|
||||
format?: 'table' | 'time_series',
|
||||
multipleQueryTypes?: boolean
|
||||
): PromQuery => {
|
||||
const refId = multipleQueryTypes && type !== 'range' ? `${target.refId}_${type}` : target.refId;
|
||||
const resultFormat = format ?? target.format;
|
||||
|
||||
return {
|
||||
...target,
|
||||
range: type === 'range' ? true : false,
|
||||
instant: type === 'instant' ? true : false,
|
||||
utcOffsetSec: this.timeSrv.timeRange().to.utcOffset() * 60,
|
||||
format: resultFormat,
|
||||
refId,
|
||||
};
|
||||
};
|
||||
|
||||
prepareOptionsV2 = (options: DataQueryRequest<PromQuery>) => {
|
||||
const targets = options.targets.map((target) => {
|
||||
const targets: PromQuery[] = [];
|
||||
|
||||
options.targets.forEach((target) => {
|
||||
//This is currently only preparing options for Explore queries where we know the format of data we want to receive
|
||||
if (target.instant) {
|
||||
return { ...target, instant: true, range: false, format: 'table' };
|
||||
if (target.instant && target.range) {
|
||||
const targetInstant = this.createTargetV2(target, 'instant', 'table', true);
|
||||
const targetRange = this.createTargetV2(target, 'range', 'time_series', true);
|
||||
targets.push(targetInstant, targetRange);
|
||||
} else if (target.instant) {
|
||||
const targetInstant = this.createTargetV2(target, 'instant', 'table', false);
|
||||
targets.push(targetInstant);
|
||||
} else {
|
||||
const targetRange = this.createTargetV2(target, 'range', 'time_series', false);
|
||||
targets.push(targetRange);
|
||||
}
|
||||
return {
|
||||
...target,
|
||||
instant: false,
|
||||
range: true,
|
||||
format: 'time_series',
|
||||
utcOffsetSec: this.timeSrv.timeRange().to.utcOffset() * 60,
|
||||
};
|
||||
});
|
||||
|
||||
return { ...options, targets };
|
||||
@@ -310,11 +332,7 @@ export class PrometheusDatasource extends DataSourceWithBackend<PromQuery, PromO
|
||||
query(options: DataQueryRequest<PromQuery>): Observable<DataQueryResponse> {
|
||||
// WIP - currently we want to run trough backend only if all queries are explore + range/instant queries
|
||||
const shouldRunBackendQuery =
|
||||
this.access === 'proxy' &&
|
||||
options.app === CoreApp.Explore &&
|
||||
!options.targets.some((query) => query.exemplar) &&
|
||||
// When running both queries, run through proxy
|
||||
!options.targets.some((query) => query.instant && query.range);
|
||||
this.access === 'proxy' && options.app === CoreApp.Explore && !options.targets.some((query) => query.exemplar);
|
||||
|
||||
if (shouldRunBackendQuery) {
|
||||
const newOptions = this.prepareOptionsV2(options);
|
||||
|
||||
Reference in New Issue
Block a user