diff --git a/public/app/plugins/datasource/loki/datasource.test.ts b/public/app/plugins/datasource/loki/datasource.test.ts index 22c632775dd..9365364b4cc 100644 --- a/public/app/plugins/datasource/loki/datasource.test.ts +++ b/public/app/plugins/datasource/loki/datasource.test.ts @@ -1089,8 +1089,30 @@ describe('LokiDatasource', () => { }); describe('getDataSamples', () => { - it('hide request from inspector', () => { - const ds = createLokiDatasource(templateSrvStub); + let ds: LokiDatasource; + beforeEach(() => { + ds = createLokiDatasource(templateSrvStub); + }); + it('ignores invalid queries', () => { + const spy = jest.spyOn(ds, 'query'); + ds.getDataSamples({ expr: 'not a query', refId: 'A' }); + expect(spy).not.toHaveBeenCalled(); + }); + it('ignores metric queries', () => { + const spy = jest.spyOn(ds, 'query'); + ds.getDataSamples({ expr: 'count_over_time({a="b"}[1m])', refId: 'A' }); + expect(spy).not.toHaveBeenCalled(); + }); + it('uses the current interval in the request', () => { + const spy = jest.spyOn(ds, 'query').mockImplementation(() => of({} as DataQueryResponse)); + ds.getDataSamples({ expr: '{job="bar"}', refId: 'A' }); + expect(spy).toHaveBeenCalledWith( + expect.objectContaining({ + range: ds.getTimeRange(), + }) + ); + }); + it('hides the request from the inspector', () => { const spy = jest.spyOn(ds, 'query').mockImplementation(() => of({} as DataQueryResponse)); ds.getDataSamples({ expr: '{job="bar"}', refId: 'A' }); expect(spy).toHaveBeenCalledWith( diff --git a/public/app/plugins/datasource/loki/datasource.ts b/public/app/plugins/datasource/loki/datasource.ts index 67697ee6f34..c7041a08a17 100644 --- a/public/app/plugins/datasource/loki/datasource.ts +++ b/public/app/plugins/datasource/loki/datasource.ts @@ -22,7 +22,6 @@ import { DateTime, FieldCache, FieldType, - getDefaultTimeRange, Labels, LoadingState, LogLevel, @@ -548,11 +547,11 @@ export class LokiDatasource expr: query.expr, queryType: LokiQueryType.Range, refId: REF_ID_DATA_SAMPLES, + // For samples we limit the request to 10 lines, so queries are small and fast maxLines: 10, }; - // For samples, we use defaultTimeRange (now-6h/now) and limit od 10 lines so queries are small and fast - const timeRange = getDefaultTimeRange(); + const timeRange = this.getTimeRange(); const request = makeRequest(lokiLogsQuery, timeRange, CoreApp.Unknown, REF_ID_DATA_SAMPLES, true); return await lastValueFrom(this.query(request).pipe(switchMap((res) => of(res.data)))); }