Loki Logs sample: use the selected time range in sample request (#65126)

Logs sample: use the selected time range in sample request

There are parts of the query, such as label values, that could only be present during the current selected time interval. If we request using a time range other than the selected one, we are not providing builder suggestions based on the same data that the user is seeing.
This commit is contained in:
Matias Chomicki 2023-03-22 17:56:40 +01:00 committed by GitHub
parent ae0f9fc44f
commit 9874a2d48b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 5 deletions

View File

@ -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(

View File

@ -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))));
}