123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- import { buildVisualQueryFromString } from './parsing';
- import { LokiVisualQuery } from './types';
- describe('buildVisualQueryFromString', () => {
- it('creates no errors for empty query', () => {
- expect(buildVisualQueryFromString('')).toEqual(
- noErrors({
- labels: [],
- operations: [],
- })
- );
- });
- it('parses simple query with label-values', () => {
- expect(buildVisualQueryFromString('{app="frontend"}')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [],
- })
- );
- });
- it('parses query with multiple label-values pairs', () => {
- expect(buildVisualQueryFromString('{app="frontend", instance!="1"}')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- {
- op: '!=',
- value: '1',
- label: 'instance',
- },
- ],
- operations: [],
- })
- );
- });
- it('parses query with line filter', () => {
- expect(buildVisualQueryFromString('{app="frontend"} |= "line"')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [{ id: '__line_contains', params: ['line'] }],
- })
- );
- });
- it('parses query with line filters and escaped characters', () => {
- expect(buildVisualQueryFromString('{app="frontend"} |= "\\\\line"')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [{ id: '__line_contains', params: ['\\line'] }],
- })
- );
- });
- it('returns error for query with ip matching line filter', () => {
- const context = buildVisualQueryFromString('{app="frontend"} |= ip("192.168.4.5/16")');
- expect(context.errors).toEqual([
- {
- text: 'Matching ip addresses not supported in query builder: |= ip("192.168.4.5/16")',
- from: 17,
- to: 40,
- parentType: 'LineFilters',
- },
- ]);
- });
- it('parses query with matcher label filter', () => {
- expect(buildVisualQueryFromString('{app="frontend"} | bar="baz"')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [{ id: '__label_filter', params: ['bar', '=', 'baz'] }],
- })
- );
- });
- it('parses query with number label filter', () => {
- expect(buildVisualQueryFromString('{app="frontend"} | bar >= 8')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [{ id: '__label_filter', params: ['bar', '>=', '8'] }],
- })
- );
- });
- it('parses query with no pipe errors filter', () => {
- expect(buildVisualQueryFromString('{app="frontend"} | __error__=""')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [{ id: '__label_filter_no_errors', params: [] }],
- })
- );
- });
- it('parses query with with unit label filter', () => {
- expect(buildVisualQueryFromString('{app="frontend"} | bar < 8mb')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [{ id: '__label_filter', params: ['bar', '<', '8mb'] }],
- })
- );
- });
- it('returns error for query with "and", "or", "comma" in label filter', () => {
- const context = buildVisualQueryFromString('{app="frontend"} | logfmt | level="error" and job="grafana"');
- expect(context.errors).toEqual([
- {
- text: 'Label filter with comma, "and", "or" not supported in query builder: level="error" and job="grafana"',
- from: 28,
- to: 59,
- parentType: 'PipelineStage',
- },
- ]);
- });
- it('returns error for query with ip label filter', () => {
- const context = buildVisualQueryFromString('{app="frontend"} | logfmt | address=ip("192.168.4.5/16")');
- expect(context.errors).toEqual([
- {
- text: 'IpLabelFilter not supported in query builder: address=ip("192.168.4.5/16")',
- from: 28,
- to: 56,
- parentType: 'PipelineStage',
- },
- ]);
- });
- it('parses query with with parser', () => {
- expect(buildVisualQueryFromString('{app="frontend"} | json')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [{ id: 'json', params: [] }],
- })
- );
- });
- it('returns error for query with JSON expression parser', () => {
- const context = buildVisualQueryFromString('{app="frontend"} | json label="value" ');
- expect(context.errors).toEqual([
- {
- text: 'JsonExpressionParser not supported in visual query builder: json label="value"',
- from: 19,
- to: 37,
- parentType: 'PipelineStage',
- },
- ]);
- });
- it('parses query with with simple unwrap', () => {
- expect(
- buildVisualQueryFromString('sum_over_time({app="frontend"} | logfmt | unwrap bytes_processed [1m])')
- ).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [
- { id: 'logfmt', params: [] },
- { id: 'unwrap', params: ['bytes_processed'] },
- { id: 'sum_over_time', params: ['1m'] },
- ],
- })
- );
- });
- it('parses query with with unwrap and error filter', () => {
- expect(
- buildVisualQueryFromString('sum_over_time({app="frontend"} | logfmt | unwrap duration | __error__="" [1m])')
- ).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [
- { id: 'logfmt', params: [] },
- { id: 'unwrap', params: ['duration'] },
- { id: '__label_filter_no_errors', params: [] },
- { id: 'sum_over_time', params: ['1m'] },
- ],
- })
- );
- });
- it('parses query with with unwrap and label filter', () => {
- expect(
- buildVisualQueryFromString('sum_over_time({app="frontend"} | logfmt | unwrap duration | label="value" [1m])')
- ).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [
- { id: 'logfmt', params: [] },
- { id: 'unwrap', params: ['duration'] },
- { id: '__label_filter', params: ['label', '=', 'value'] },
- { id: 'sum_over_time', params: ['1m'] },
- ],
- })
- );
- });
- it('returns error for query with unwrap and conversion operation', () => {
- const context = buildVisualQueryFromString(
- 'sum_over_time({app="frontend"} | logfmt | unwrap duration(label) [5m])'
- );
- expect(context.errors).toEqual([
- {
- text: 'Unwrap with conversion operator not supported in query builder: | unwrap duration(label)',
- from: 40,
- to: 64,
- parentType: 'LogRangeExpr',
- },
- ]);
- });
- it('parses metrics query with function', () => {
- expect(buildVisualQueryFromString('rate({app="frontend"} | json [5m])')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [
- { id: 'json', params: [] },
- { id: 'rate', params: ['5m'] },
- ],
- })
- );
- });
- it('parses metrics query with function and aggregation', () => {
- expect(buildVisualQueryFromString('sum(rate({app="frontend"} | json [5m]))')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [
- { id: 'json', params: [] },
- { id: 'rate', params: ['5m'] },
- { id: 'sum', params: [] },
- ],
- })
- );
- });
- it('parses metrics query with function and aggregation with grouping', () => {
- expect(buildVisualQueryFromString('sum by (job,name) (rate({app="frontend"} | json [5m]))')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [
- { id: 'json', params: [] },
- { id: 'rate', params: ['5m'] },
- { id: '__sum_by', params: ['job', 'name'] },
- ],
- })
- );
- });
- it('parses metrics query with function and aggregation with grouping at the end', () => {
- expect(buildVisualQueryFromString('sum(rate({app="frontend"} | json [5m])) without(job,name)')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [
- { id: 'json', params: [] },
- { id: 'rate', params: ['5m'] },
- { id: '__sum_without', params: ['job', 'name'] },
- ],
- })
- );
- });
- it('parses metrics query with function and aggregation and filters', () => {
- expect(buildVisualQueryFromString('sum(rate({app="frontend"} |~ `abc` | json | bar="baz" [5m]))')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [
- { id: '__line_matches_regex', params: ['abc'] },
- { id: 'json', params: [] },
- { id: '__label_filter', params: ['bar', '=', 'baz'] },
- { id: 'rate', params: ['5m'] },
- { id: 'sum', params: [] },
- ],
- })
- );
- });
- it('parses metrics query with vector aggregation with number', () => {
- expect(
- buildVisualQueryFromString('topk(10, sum(count_over_time({app="frontend"} | logfmt | __error__=`` [5m])))')
- ).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [
- { id: 'logfmt', params: [] },
- { id: '__label_filter_no_errors', params: [] },
- { id: 'count_over_time', params: ['5m'] },
- { id: 'sum', params: [] },
- { id: 'topk', params: [10] },
- ],
- })
- );
- });
- it('parses template variables in strings', () => {
- expect(buildVisualQueryFromString('{instance="$label_variable"}')).toEqual(
- noErrors({
- labels: [{ label: 'instance', op: '=', value: '$label_variable' }],
- operations: [],
- })
- );
- });
- it('parses metrics query with interval variables', () => {
- expect(buildVisualQueryFromString('rate({app="frontend"} [$__interval])')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [{ id: 'rate', params: ['$__interval'] }],
- })
- );
- });
- it('parses quantile queries', () => {
- expect(buildVisualQueryFromString(`quantile_over_time(0.99, {app="frontend"} [1m])`)).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [{ id: 'quantile_over_time', params: ['0.99', '1m'] }],
- })
- );
- });
- it('parses query with line format', () => {
- expect(buildVisualQueryFromString('{app="frontend"} | line_format "abc"')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [{ id: 'line_format', params: ['abc'] }],
- })
- );
- });
- it('parses query with label format', () => {
- expect(buildVisualQueryFromString('{app="frontend"} | label_format newLabel=oldLabel')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [{ id: 'label_format', params: ['newLabel', 'oldLabel'] }],
- })
- );
- });
- it('parses query with multiple label format', () => {
- expect(buildVisualQueryFromString('{app="frontend"} | label_format newLabel=oldLabel, bar="baz"')).toEqual(
- noErrors({
- labels: [
- {
- op: '=',
- value: 'frontend',
- label: 'app',
- },
- ],
- operations: [
- { id: 'label_format', params: ['newLabel', 'oldLabel'] },
- { id: 'label_format', params: ['bar', 'baz'] },
- ],
- })
- );
- });
- it('parses binary query', () => {
- expect(buildVisualQueryFromString('rate({project="bar"}[5m]) / rate({project="foo"}[5m])')).toEqual(
- noErrors({
- labels: [{ op: '=', value: 'bar', label: 'project' }],
- operations: [{ id: 'rate', params: ['5m'] }],
- binaryQueries: [
- {
- operator: '/',
- query: {
- labels: [{ op: '=', value: 'foo', label: 'project' }],
- operations: [{ id: 'rate', params: ['5m'] }],
- },
- },
- ],
- })
- );
- });
- it('parses binary scalar query', () => {
- expect(buildVisualQueryFromString('rate({project="bar"}[5m]) / 2')).toEqual(
- noErrors({
- labels: [{ op: '=', value: 'bar', label: 'project' }],
- operations: [
- { id: 'rate', params: ['5m'] },
- { id: '__divide_by', params: [2] },
- ],
- })
- );
- });
- it('parses chained binary query', () => {
- expect(
- buildVisualQueryFromString('rate({project="bar"}[5m]) * 2 / rate({project="foo"}[5m]) + rate({app="test"}[1m])')
- ).toEqual(
- noErrors({
- labels: [{ op: '=', value: 'bar', label: 'project' }],
- operations: [
- { id: 'rate', params: ['5m'] },
- { id: '__multiply_by', params: [2] },
- ],
- binaryQueries: [
- {
- operator: '/',
- query: {
- labels: [{ op: '=', value: 'foo', label: 'project' }],
- operations: [{ id: 'rate', params: ['5m'] }],
- binaryQueries: [
- {
- operator: '+',
- query: {
- labels: [{ op: '=', value: 'test', label: 'app' }],
- operations: [{ id: 'rate', params: ['1m'] }],
- },
- },
- ],
- },
- },
- ],
- })
- );
- });
- });
- function noErrors(query: LokiVisualQuery) {
- return {
- errors: [],
- query,
- };
- }
|