Commit 36896f50 by Lukas Siatka Committed by GitHub

Explore/Loki: Removes old query syntax support for regex filter (#24281)

* Chore: removed regex from LokiQuery

* Chore: removes parseQuery from Loki datasource

* Chore: removes regex param from format query in lokiStreamsToDataframes in Loki result transformer

* Chore: updates formatQuery and removes parseQuery from Loki query utils

* Chore: removes highlighter test with unsupported syntax from Loki query utils
parent 2b028e38
......@@ -11,7 +11,7 @@ import { DatasourceRequestOptions } from 'app/core/services/backend_srv';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { safeStringifyValue, convertToWebSocketUrl } from 'app/core/utils/explore';
import { lokiResultsToTableModel, processRangeQueryResponse, lokiStreamResultToDataFrame } from './result_transformer';
import { parseQuery, getHighlighterExpressionsFromQuery } from './query_utils';
import { getHighlighterExpressionsFromQuery } from './query_utils';
// Types
import {
......@@ -136,7 +136,7 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
): Observable<DataQueryResponse> => {
const timeNs = this.getTime(options.range.to, true);
const query = {
query: parseQuery(target.expr).query,
query: target.expr,
time: `${timeNs + (1e9 - (timeNs % 1e9))}`,
limit: Math.min(options.maxDataPoints || Infinity, this.maxLines),
};
......@@ -158,7 +158,7 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
};
createRangeQuery(target: LokiQuery, options: RangeQueryOptions): LokiRangeQueryRequest {
const { query } = parseQuery(target.expr);
const query = target.expr;
let range: { start?: number; end?: number; step?: number } = {};
if (options.range) {
const startNs = this.getTime(options.range.from, false);
......@@ -236,7 +236,7 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
};
createLiveTarget(target: LokiQuery, options: { maxDataPoints?: number }): LokiLiveTarget {
const { query } = parseQuery(target.expr);
const query = target.expr;
const baseUrl = this.instanceSettings.url;
const params = serializeParams({ query });
......
import { parseQuery, getHighlighterExpressionsFromQuery } from './query_utils';
import { LokiExpression } from './types';
describe('parseQuery', () => {
it('returns empty for empty string', () => {
expect(parseQuery('')).toEqual({
query: '',
regexp: '',
} as LokiExpression);
});
it('returns regexp for strings without query', () => {
expect(parseQuery('test')).toEqual({
query: 'test',
regexp: '',
} as LokiExpression);
});
it('returns query for strings without regexp', () => {
expect(parseQuery('{foo="bar"}')).toEqual({
query: '{foo="bar"}',
regexp: '',
} as LokiExpression);
});
it('returns query for strings with query and search string', () => {
expect(parseQuery('x {foo="bar"}')).toEqual({
query: '{foo="bar"}',
regexp: 'x',
} as LokiExpression);
});
it('returns query for strings with query and regexp', () => {
expect(parseQuery('{foo="bar"} x|y')).toEqual({
query: '{foo="bar"}',
regexp: 'x|y',
} as LokiExpression);
});
it('returns query for selector with two labels', () => {
expect(parseQuery('{foo="bar", baz="42"}')).toEqual({
query: '{foo="bar", baz="42"}',
regexp: '',
} as LokiExpression);
});
it('returns query and regexp with quantifiers', () => {
expect(parseQuery('{foo="bar"} \\.java:[0-9]{1,5}')).toEqual({
query: '{foo="bar"}',
regexp: '\\.java:[0-9]{1,5}',
} as LokiExpression);
expect(parseQuery('\\.java:[0-9]{1,5} {foo="bar"}')).toEqual({
query: '{foo="bar"}',
regexp: '\\.java:[0-9]{1,5}',
} as LokiExpression);
});
it('returns query with filter operands as is', () => {
expect(parseQuery('{foo="bar"} |= "x|y"')).toEqual({
query: '{foo="bar"} |= "x|y"',
regexp: '',
} as LokiExpression);
expect(parseQuery('{foo="bar"} |~ "42"')).toEqual({
query: '{foo="bar"} |~ "42"',
regexp: '',
} as LokiExpression);
});
});
import { getHighlighterExpressionsFromQuery } from './query_utils';
describe('getHighlighterExpressionsFromQuery', () => {
it('returns no expressions for empty query', () => {
expect(getHighlighterExpressionsFromQuery('')).toEqual([]);
});
it('returns a single expressions for legacy query', () => {
expect(getHighlighterExpressionsFromQuery('{} x')).toEqual(['x']);
expect(getHighlighterExpressionsFromQuery('{foo="bar"} x')).toEqual(['x']);
});
it('returns an expression for query with filter', () => {
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x"')).toEqual(['x']);
});
......
import { LokiExpression } from './types';
import escapeRegExp from 'lodash/escapeRegExp';
const selectorRegexp = /(?:^|\s){[^{]*}/g;
export function parseQuery(input: string): LokiExpression {
input = input || '';
const match = input.match(selectorRegexp);
let query = input;
let regexp = '';
if (match) {
// Regexp result is ignored on the server side
regexp = input.replace(selectorRegexp, '').trim();
// Keep old-style regexp, otherwise take whole query
if (regexp && regexp.search(/\|=|\|~|!=|!~/) === -1) {
query = match[0].trim();
} else {
regexp = '';
}
}
return { regexp, query };
}
export function formatQuery(selector: string, search: string): string {
return `${selector || ''} ${search || ''}`.trim();
export function formatQuery(selector: string): string {
return `${selector || ''}`.trim();
}
/**
......@@ -31,11 +9,6 @@ export function formatQuery(selector: string, search: string): string {
* E.g., `{} |= foo |=bar != baz` returns `['foo', 'bar']`.
*/
export function getHighlighterExpressionsFromQuery(input: string): string[] {
const parsed = parseQuery(input);
// Legacy syntax
if (parsed.regexp) {
return [parsed.regexp];
}
let expression = input;
const results = [];
// Consume filter expression from left to right
......
......@@ -291,7 +291,7 @@ function lokiStatsToMetaStat(stats: LokiStats): QueryResultMetaStat[] {
export function lokiStreamsToDataframes(
response: LokiStreamResponse,
target: { refId: string; expr?: string; regexp?: string },
target: { refId: string; expr?: string },
limit: number,
config: LokiOptions,
reverse = false
......@@ -309,7 +309,7 @@ export function lokiStreamsToDataframes(
...dataFrame,
refId: target.refId,
meta: {
searchWords: getHighlighterExpressionsFromQuery(formatQuery(target.expr, target.regexp)),
searchWords: getHighlighterExpressionsFromQuery(formatQuery(target.expr)),
limit,
stats,
custom,
......
......@@ -26,7 +26,6 @@ export interface LokiQuery extends DataQuery {
expr: string;
liveStreaming?: boolean;
query?: string;
regexp?: string;
format?: string;
reverse?: boolean;
legendFormat?: string;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment