Loki: Return false from isMetricsQuery if query is empty (#47024)

This commit is contained in:
Andrej Ocenas
2022-03-29 18:18:43 +02:00
committed by GitHub
parent 6c86f26881
commit 10d8ccc8ff
2 changed files with 21 additions and 1 deletions

View File

@@ -13,7 +13,7 @@ import {
toUtc,
} from '@grafana/data';
import { BackendSrvRequest, FetchResponse } from '@grafana/runtime';
import { LokiDatasource, RangeQueryOptions } from './datasource';
import { isMetricsQuery, LokiDatasource, RangeQueryOptions } from './datasource';
import { LokiQuery, LokiResponse, LokiResultType } from './types';
import { getQueryOptions } from 'test/helpers/getQueryOptions';
import { TemplateSrv } from 'app/features/templating/template_srv';
@@ -1029,6 +1029,23 @@ describe('LokiDatasource', () => {
});
});
describe('isMetricsQuery', () => {
it('should return true for metrics query', () => {
const query = 'rate({label=value}[1m])';
expect(isMetricsQuery(query)).toBeTruthy();
});
it('should return false for logs query', () => {
const query = '{label=value}';
expect(isMetricsQuery(query)).toBeFalsy();
});
it('should not blow up on empty query', () => {
const query = '';
expect(isMetricsQuery(query)).toBeFalsy();
});
});
function assertAdHocFilters(query: string, expectedResults: string, ds: LokiDatasource) {
const lokiQuery: LokiQuery = { refId: 'A', expr: query };
const result = ds.addAdHocFilters(lokiQuery.expr);

View File

@@ -811,6 +811,9 @@ export function lokiSpecialRegexEscape(value: any) {
* Sometimes important to know that before we actually do the query.
*/
export function isMetricsQuery(query: string): boolean {
if (!query) {
return false;
}
const tokens = Prism.tokenize(query, syntax);
return tokens.some((t) => {
// Not sure in which cases it can be string maybe if nothing matched which means it should not be a function