mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Loki,Prometheus: Fix of showing error message for empty query (#47379)
* Loki,Prometheus: Dont show error on empty query * Add tests
This commit is contained in:
parent
c72881a8b1
commit
caa82a124d
@ -2,6 +2,15 @@ import { buildVisualQueryFromString } from './parsing';
|
|||||||
import { LokiVisualQuery } from './types';
|
import { LokiVisualQuery } from './types';
|
||||||
|
|
||||||
describe('buildVisualQueryFromString', () => {
|
describe('buildVisualQueryFromString', () => {
|
||||||
|
it('creates no errors for empty query', () => {
|
||||||
|
expect(buildVisualQueryFromString('')).toEqual(
|
||||||
|
noErrors({
|
||||||
|
labels: [],
|
||||||
|
operations: [],
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('parses simple query with label-values', () => {
|
it('parses simple query with label-values', () => {
|
||||||
expect(buildVisualQueryFromString('{app="frontend"}')).toEqual(
|
expect(buildVisualQueryFromString('{app="frontend"}')).toEqual(
|
||||||
noErrors({
|
noErrors({
|
||||||
|
@ -20,8 +20,8 @@ interface Context {
|
|||||||
|
|
||||||
interface ParsingError {
|
interface ParsingError {
|
||||||
text: string;
|
text: string;
|
||||||
from: number;
|
from?: number;
|
||||||
to: number;
|
to?: number;
|
||||||
parentType?: string;
|
parentType?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,12 +36,25 @@ export function buildVisualQueryFromString(expr: string): Context {
|
|||||||
operations: [],
|
operations: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
const context = {
|
const context: Context = {
|
||||||
query: visQuery,
|
query: visQuery,
|
||||||
errors: [],
|
errors: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
handleExpression(replacedExpr, node, context);
|
try {
|
||||||
|
handleExpression(replacedExpr, node, context);
|
||||||
|
} catch (err) {
|
||||||
|
// Not ideal to log it here, but otherwise we would lose the stack trace.
|
||||||
|
console.error(err);
|
||||||
|
context.errors.push({
|
||||||
|
text: err.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have empty query, we want to reset errors
|
||||||
|
if (isEmptyQuery(context.query)) {
|
||||||
|
context.errors = [];
|
||||||
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -496,3 +509,10 @@ function createNotSupportedError(expr: string, node: SyntaxNode, error: string)
|
|||||||
err.text = `${error}: ${err.text}`;
|
err.text = `${error}: ${err.text}`;
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isEmptyQuery(query: LokiVisualQuery) {
|
||||||
|
if (query.labels.length === 0 && query.operations.length === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
@ -2,6 +2,15 @@ import { buildVisualQueryFromString } from './parsing';
|
|||||||
import { PromVisualQuery } from './types';
|
import { PromVisualQuery } from './types';
|
||||||
|
|
||||||
describe('buildVisualQueryFromString', () => {
|
describe('buildVisualQueryFromString', () => {
|
||||||
|
it('creates no errors for empty query', () => {
|
||||||
|
expect(buildVisualQueryFromString('')).toEqual(
|
||||||
|
noErrors({
|
||||||
|
labels: [],
|
||||||
|
operations: [],
|
||||||
|
metric: '',
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
it('parses simple query', () => {
|
it('parses simple query', () => {
|
||||||
expect(buildVisualQueryFromString('counters_logins{app="frontend"}')).toEqual(
|
expect(buildVisualQueryFromString('counters_logins{app="frontend"}')).toEqual(
|
||||||
noErrors({
|
noErrors({
|
||||||
|
@ -46,6 +46,11 @@ export function buildVisualQueryFromString(expr: string): Context {
|
|||||||
text: err.message,
|
text: err.message,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we have empty query, we want to reset errors
|
||||||
|
if (isEmptyQuery(context.query)) {
|
||||||
|
context.errors = [];
|
||||||
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -373,3 +378,10 @@ function getBinaryModifier(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isEmptyQuery(query: PromVisualQuery) {
|
||||||
|
if (query.labels.length === 0 && query.operations.length === 0 && !query.metric) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
@ -30,7 +30,7 @@ export function makeError(expr: string, node: SyntaxNode) {
|
|||||||
const variableRegex = /\$(\w+)|\[\[([\s\S]+?)(?::(\w+))?\]\]|\${(\w+)(?:\.([^:^\}]+))?(?::([^\}]+))?}/g;
|
const variableRegex = /\$(\w+)|\[\[([\s\S]+?)(?::(\w+))?\]\]|\${(\w+)(?:\.([^:^\}]+))?(?::([^\}]+))?}/g;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* As variables with $ are creating parsing errors, we first replace them with magic string that is parseable and at
|
* As variables with $ are creating parsing errors, we first replace them with magic string that is parsable and at
|
||||||
* the same time we can get the variable and it's format back from it.
|
* the same time we can get the variable and it's format back from it.
|
||||||
* @param expr
|
* @param expr
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user