Commit 8e9181e7 by Andrej Ocenas Committed by GitHub

Loki: Run instant query only when doing metric query (#28325)

* Run instant query only when doing metric query

* Update public/app/plugins/datasource/loki/datasource.ts

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
parent 2087ff60
...@@ -36,33 +36,33 @@ const timeSrvStub = { ...@@ -36,33 +36,33 @@ const timeSrvStub = {
}), }),
}; };
const testResponse: FetchResponse<LokiResponse> = {
data: {
data: {
resultType: LokiResultType.Stream,
result: [
{
stream: {},
values: [['1573646419522934000', 'hello']],
},
],
},
status: 'success',
},
ok: true,
headers: ({} as unknown) as Headers,
redirected: false,
status: 200,
statusText: 'Success',
type: 'default',
url: '',
config: ({} as unknown) as BackendSrvRequest,
};
describe('LokiDatasource', () => { describe('LokiDatasource', () => {
let fetchStream: Subject<FetchResponse>; let fetchStream: Subject<FetchResponse>;
const fetchMock = jest.spyOn(backendSrv, 'fetch'); const fetchMock = jest.spyOn(backendSrv, 'fetch');
const testResponse: FetchResponse<LokiResponse> = {
data: {
data: {
resultType: LokiResultType.Stream,
result: [
{
stream: {},
values: [['1573646419522934000', 'hello']],
},
],
},
status: 'success',
},
ok: true,
headers: ({} as unknown) as Headers,
redirected: false,
status: 200,
statusText: 'Success',
type: 'default',
url: '',
config: ({} as unknown) as BackendSrvRequest,
};
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks(); jest.clearAllMocks();
fetchStream = new Subject<FetchResponse>(); fetchStream = new Subject<FetchResponse>();
...@@ -178,48 +178,42 @@ describe('LokiDatasource', () => { ...@@ -178,48 +178,42 @@ describe('LokiDatasource', () => {
}); });
describe('when querying', () => { describe('when querying', () => {
it('should run range and instant query in Explore', done => { function setup(expr: string, app: CoreApp) {
const ds = createLokiDSForTests(); const ds = createLokiDSForTests();
const options = getQueryOptions<LokiQuery>({ const options = getQueryOptions<LokiQuery>({
targets: [{ expr: '{job="grafana"}', refId: 'B' }], targets: [{ expr, refId: 'B' }],
app: CoreApp.Explore, app,
}); });
ds.runInstantQuery = jest.fn(() => of({ data: [] })); ds.runInstantQuery = jest.fn(() => of({ data: [] }));
ds.runRangeQuery = jest.fn(() => of({ data: [] })); ds.runRangeQuery = jest.fn(() => of({ data: [] }));
return { ds, options };
observableTester().subscribeAndExpectOnComplete<DataQueryResponse>({ }
observable: ds.query(options),
expect: () => { it('should run range and instant query in Explore if running metric query', async () => {
expect(ds.runInstantQuery).toBeCalled(); const { ds, options } = setup('rate({job="grafana"}[10m])', CoreApp.Explore);
expect(ds.runRangeQuery).toBeCalled(); await ds.query(options).toPromise();
}, expect(ds.runInstantQuery).toBeCalled();
done, expect(ds.runRangeQuery).toBeCalled();
});
}); });
it('should run only range query in Dashboard', done => { it('should run only range query in Explore if running logs query', async () => {
const ds = createLokiDSForTests(); const { ds, options } = setup('{job="grafana"}', CoreApp.Explore);
const options = getQueryOptions<LokiQuery>({ await ds.query(options).toPromise();
targets: [{ expr: '{job="grafana"}', refId: 'B' }], expect(ds.runInstantQuery).not.toBeCalled();
}); expect(ds.runRangeQuery).toBeCalled();
});
ds.runInstantQuery = jest.fn(() => of({ data: [] }));
ds.runRangeQuery = jest.fn(() => of({ data: [] }));
observableTester().subscribeAndExpectOnComplete<DataQueryResponse>({ it('should run only range query in Dashboard', async () => {
observable: ds.query(options), const { ds, options } = setup('rate({job="grafana"}[10m])', CoreApp.Dashboard);
expect: () => { await ds.query(options).toPromise();
expect(ds.runRangeQuery).toBeCalled(); expect(ds.runInstantQuery).not.toBeCalled();
}, expect(ds.runRangeQuery).toBeCalled();
done,
});
}); });
it('should return series data for both queries in Explore', done => { it('should return series data for both queries in Explore if metrics query', done => {
const ds = createLokiDSForTests(); const ds = createLokiDSForTests();
const options = getQueryOptions<LokiQuery>({ const options = getQueryOptions<LokiQuery>({
targets: [{ expr: '{job="grafana"} |= "foo"', refId: 'B' }], targets: [{ expr: 'rate({job="grafana"} |= "foo" [10m])', refId: 'B' }],
app: CoreApp.Explore, app: CoreApp.Explore,
}); });
......
...@@ -99,7 +99,9 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> { ...@@ -99,7 +99,9 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
})); }));
for (const target of filteredTargets) { for (const target of filteredTargets) {
if (options.app === CoreApp.Explore) { // In explore we want to show result of metrics instant query in a table under the graph panel to mimic behaviour of prometheus.
// We don't want to do that in dashboards though as user would have to pick the correct data frame.
if (options.app === CoreApp.Explore && isMetricsQuery(target.expr)) {
subQueries.push(this.runInstantQuery(target, options, filteredTargets.length)); subQueries.push(this.runInstantQuery(target, options, filteredTargets.length));
} }
subQueries.push(this.runRangeQuery(target, options, filteredTargets.length)); subQueries.push(this.runRangeQuery(target, options, filteredTargets.length));
......
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