mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
Loki: Fix query error for step parameter (#20607)
* Loki: Fix query error for step parameter - Loki does not fully support float steps like prometheus and returns a query error - this change makes sure that the step parameter is rounded to an integer * Added test and comments
This commit is contained in:
parent
cd07c3e652
commit
c53ed5bbac
@ -1,7 +1,7 @@
|
||||
import LokiDatasource from './datasource';
|
||||
import LokiDatasource, { RangeQueryOptions } from './datasource';
|
||||
import { LokiQuery, LokiResultType, LokiResponse, LokiLegacyStreamResponse } from './types';
|
||||
import { getQueryOptions } from 'test/helpers/getQueryOptions';
|
||||
import { AnnotationQueryRequest, DataSourceApi, DataFrame, dateTime } from '@grafana/data';
|
||||
import { AnnotationQueryRequest, DataSourceApi, DataFrame, dateTime, TimeRange } from '@grafana/data';
|
||||
import { BackendSrv } from 'app/core/services/backend_srv';
|
||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
||||
import { CustomVariable } from 'app/features/templating/custom_variable';
|
||||
@ -302,6 +302,23 @@ describe('LokiDatasource', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('when creating a range query', () => {
|
||||
const ds = new LokiDatasource(instanceSettings, backendSrv, templateSrvMock);
|
||||
const query: LokiQuery = { expr: 'foo', refId: 'bar' };
|
||||
|
||||
// Loki v1 API has an issue with float step parameters, can be removed when API is fixed
|
||||
it('should produce an integer step parameter', () => {
|
||||
const range: TimeRange = {
|
||||
from: dateTime(0),
|
||||
to: dateTime(1e9 + 1),
|
||||
raw: { from: '0', to: '1000000001' },
|
||||
};
|
||||
// Odd timerange/interval combination that would lead to a float step
|
||||
const options: RangeQueryOptions = { range, intervalMs: 2000 };
|
||||
expect(Number.isInteger(ds.createRangeQuery(query, options).step)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('annotationQuery', () => {
|
||||
it('should transform the loki data to annotation response', async () => {
|
||||
const ds = new LokiDatasource(instanceSettings, backendSrv, templateSrvMock);
|
||||
|
@ -55,7 +55,7 @@ import { ExploreMode } from 'app/types';
|
||||
import { LegacyTarget, LiveStreams } from './live_streams';
|
||||
import LanguageProvider from './language_provider';
|
||||
|
||||
type RangeQueryOptions = Pick<DataQueryRequest<LokiQuery>, 'range' | 'intervalMs' | 'maxDataPoints' | 'reverse'>;
|
||||
export type RangeQueryOptions = Pick<DataQueryRequest<LokiQuery>, 'range' | 'intervalMs' | 'maxDataPoints' | 'reverse'>;
|
||||
export const DEFAULT_MAX_LINES = 1000;
|
||||
const LEGACY_QUERY_ENDPOINT = '/api/prom/query';
|
||||
const RANGE_QUERY_ENDPOINT = '/loki/api/v1/query_range';
|
||||
@ -267,7 +267,7 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
|
||||
const startNs = this.getTime(options.range.from, false);
|
||||
const endNs = this.getTime(options.range.to, true);
|
||||
const rangeMs = Math.ceil((endNs - startNs) / 1e6);
|
||||
const step = this.adjustInterval(options.intervalMs, rangeMs) / 1000;
|
||||
const step = Math.ceil(this.adjustInterval(options.intervalMs, rangeMs) / 1000);
|
||||
const alignedTimes = {
|
||||
start: startNs - (startNs % 1e9),
|
||||
end: endNs + (1e9 - (endNs % 1e9)),
|
||||
|
Loading…
Reference in New Issue
Block a user