Loki: Return false from isMetricsQuery if query is empty (#47024)
This commit is contained in:
@@ -13,7 +13,7 @@ import {
|
|||||||
toUtc,
|
toUtc,
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
import { BackendSrvRequest, FetchResponse } from '@grafana/runtime';
|
import { BackendSrvRequest, FetchResponse } from '@grafana/runtime';
|
||||||
import { LokiDatasource, RangeQueryOptions } from './datasource';
|
import { isMetricsQuery, LokiDatasource, RangeQueryOptions } from './datasource';
|
||||||
import { LokiQuery, LokiResponse, LokiResultType } from './types';
|
import { LokiQuery, LokiResponse, LokiResultType } from './types';
|
||||||
import { getQueryOptions } from 'test/helpers/getQueryOptions';
|
import { getQueryOptions } from 'test/helpers/getQueryOptions';
|
||||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
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) {
|
function assertAdHocFilters(query: string, expectedResults: string, ds: LokiDatasource) {
|
||||||
const lokiQuery: LokiQuery = { refId: 'A', expr: query };
|
const lokiQuery: LokiQuery = { refId: 'A', expr: query };
|
||||||
const result = ds.addAdHocFilters(lokiQuery.expr);
|
const result = ds.addAdHocFilters(lokiQuery.expr);
|
||||||
|
|||||||
@@ -811,6 +811,9 @@ export function lokiSpecialRegexEscape(value: any) {
|
|||||||
* Sometimes important to know that before we actually do the query.
|
* Sometimes important to know that before we actually do the query.
|
||||||
*/
|
*/
|
||||||
export function isMetricsQuery(query: string): boolean {
|
export function isMetricsQuery(query: string): boolean {
|
||||||
|
if (!query) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
const tokens = Prism.tokenize(query, syntax);
|
const tokens = Prism.tokenize(query, syntax);
|
||||||
return tokens.some((t) => {
|
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
|
// Not sure in which cases it can be string maybe if nothing matched which means it should not be a function
|
||||||
|
|||||||
Reference in New Issue
Block a user