From 9521b0d2d25b8145a76bd06cc21aeec5f0873247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=9B=BD=E5=BF=A0?= <249032432@qq.com> Date: Fri, 3 Mar 2023 17:09:34 +0800 Subject: [PATCH] Explore: Fix vector(5) syntax error in loki log explore (#63994) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [fix] loki log explore : fix vector(5) err * changelog * changelog fmt * rollback change log * fix test and pretty fmt * Update package.json update lezer-logql depedency to 0.1.2 * Update package.json fix conflict ➤ YN0000: │ "@grafana/monaco-logql@npm:^0.0.6": ➤ YN0000: │ @@ -21988,9 +21987,9 @@ ➤ YN0000: │ "@grafana/experimental": 1.1.0 ➤ YN0000: │ "@grafana/faro-core": 1.0.0-beta2 ➤ YN0000: │ "@grafana/faro-web-sdk": 1.0.0-beta2 ➤ YN0000: │ "@grafana/google-sdk": 0.0.4 ➤ YN0028: │ - "@grafana/lezer-logql": 0.1.1 ➤ YN0028: │ + "@grafana/lezer-logql": 0.1.2 ➤ YN0000: │ "@grafana/monaco-logql": ^0.0.6 ➤ YN0000: │ "@grafana/runtime": "workspace:*" ➤ YN0000: │ "@grafana/scenes": ^0.0.14 ➤ YN0000: │ "@grafana/schema": "workspace:*" * Upgrade lezer-logql * fix logql test --------- Co-authored-by: Matias Chomicki --- package.json | 2 +- .../completions.test.ts | 2 +- .../monaco-completion-provider/completions.ts | 20 +++++++++++++++++-- .../loki/querybuilder/parsing.test.ts | 4 ++-- .../plugins/datasource/loki/syntax.test.ts | 1 + public/app/plugins/datasource/loki/syntax.ts | 11 +++++++++- yarn.lock | 10 +++++----- 7 files changed, 38 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index e3492699898..828da4c2a5f 100644 --- a/package.json +++ b/package.json @@ -264,7 +264,7 @@ "@grafana/faro-core": "1.0.0-beta2", "@grafana/faro-web-sdk": "1.0.0-beta2", "@grafana/google-sdk": "0.0.4", - "@grafana/lezer-logql": "0.1.1", + "@grafana/lezer-logql": "0.1.2", "@grafana/monaco-logql": "^0.0.6", "@grafana/runtime": "workspace:*", "@grafana/scenes": "^0.0.14", diff --git a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.test.ts b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.test.ts index d914996f2f4..2793607f2d5 100644 --- a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.test.ts +++ b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.test.ts @@ -206,7 +206,7 @@ describe('getCompletions', () => { const situation = { type } as Situation; const completions = await getCompletions(situation, completionProvider); - expect(completions).toHaveLength(24); + expect(completions).toHaveLength(25); }); test('Returns completion options when the situation is IN_RANGE', async () => { diff --git a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.ts b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.ts index 003118cc5ab..cb06080b4c9 100644 --- a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.ts +++ b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.ts @@ -1,7 +1,7 @@ import { escapeLabelValueInExactSelector } from '../../../languageUtils'; import { explainOperator } from '../../../querybuilder/operations'; import { LokiOperationId } from '../../../querybuilder/types'; -import { AGGREGATION_OPERATORS, RANGE_VEC_FUNCTIONS } from '../../../syntax'; +import { AGGREGATION_OPERATORS, RANGE_VEC_FUNCTIONS, BUILT_IN_FUNCTIONS } from '../../../syntax'; import { CompletionDataProvider } from './CompletionDataProvider'; import { NeverCaseError } from './NeverCaseError'; @@ -58,6 +58,16 @@ const FUNCTION_COMPLETIONS: Completion[] = RANGE_VEC_FUNCTIONS.map((f) => ({ documentation: f.documentation, })); +const BUILT_IN_FUNCTIONS_COMPLETIONS: Completion[] = BUILT_IN_FUNCTIONS.map((f) => ({ + type: 'FUNCTION', + label: f.label, + insertText: `${f.insertText ?? ''}($0)`, + isSnippet: true, + triggerOnInsert: true, + detail: f.detail, + documentation: f.documentation, +})); + const DURATION_COMPLETIONS: Completion[] = ['$__interval', '$__range', '1m', '5m', '10m', '30m', '1h', '1d'].map( (text) => ({ type: 'DURATION', @@ -293,7 +303,13 @@ export async function getCompletions( case 'EMPTY': case 'AT_ROOT': const historyCompletions = await getAllHistoryCompletions(dataProvider); - return [...historyCompletions, ...LOG_COMPLETIONS, ...AGGREGATION_COMPLETIONS, ...FUNCTION_COMPLETIONS]; + return [ + ...historyCompletions, + ...LOG_COMPLETIONS, + ...AGGREGATION_COMPLETIONS, + ...BUILT_IN_FUNCTIONS_COMPLETIONS, + ...FUNCTION_COMPLETIONS, + ]; case 'IN_RANGE': return DURATION_COMPLETIONS; case 'IN_GROUPING': diff --git a/public/app/plugins/datasource/loki/querybuilder/parsing.test.ts b/public/app/plugins/datasource/loki/querybuilder/parsing.test.ts index 52e88b8af2c..cb34da275cb 100644 --- a/public/app/plugins/datasource/loki/querybuilder/parsing.test.ts +++ b/public/app/plugins/datasource/loki/querybuilder/parsing.test.ts @@ -141,7 +141,7 @@ describe('buildVisualQueryFromString', () => { }); it('parses query with with unit label filter', () => { - expect(buildVisualQueryFromString('{app="frontend"} | bar < 8mb')).toEqual( + expect(buildVisualQueryFromString('{app="frontend"} | bar < 8m')).toEqual( noErrors({ labels: [ { @@ -150,7 +150,7 @@ describe('buildVisualQueryFromString', () => { label: 'app', }, ], - operations: [{ id: LokiOperationId.LabelFilter, params: ['bar', '<', '8mb'] }], + operations: [{ id: LokiOperationId.LabelFilter, params: ['bar', '<', '8m'] }], }) ); }); diff --git a/public/app/plugins/datasource/loki/syntax.test.ts b/public/app/plugins/datasource/loki/syntax.test.ts index a000eb990b0..04176ac7abf 100644 --- a/public/app/plugins/datasource/loki/syntax.test.ts +++ b/public/app/plugins/datasource/loki/syntax.test.ts @@ -33,6 +33,7 @@ describe('Loki syntax', () => { expect(Prism.highlight('avg_over_time({key="value"}[5m])', syntax, 'loki')).toContain( 'avg_over_time' ); + expect(Prism.highlight('vector(5)', syntax, 'loki')).toContain('vector'); }); it('should highlight operators in Loki query correctly', () => { expect(Prism.highlight('{key="value"} |= "test"', syntax, 'loki')).toContain( diff --git a/public/app/plugins/datasource/loki/syntax.ts b/public/app/plugins/datasource/loki/syntax.ts index 430cdc4c077..9790da1ac19 100644 --- a/public/app/plugins/datasource/loki/syntax.ts +++ b/public/app/plugins/datasource/loki/syntax.ts @@ -181,7 +181,16 @@ export const RANGE_VEC_FUNCTIONS = [ }, ]; -export const FUNCTIONS = [...AGGREGATION_OPERATORS, ...RANGE_VEC_FUNCTIONS]; +export const BUILT_IN_FUNCTIONS = [ + { + insertText: 'vector', + label: 'vector', + detail: 'vector(scalar)', + documentation: 'Returns the scalar as a vector with no labels.', + }, +]; + +export const FUNCTIONS = [...AGGREGATION_OPERATORS, ...RANGE_VEC_FUNCTIONS, ...BUILT_IN_FUNCTIONS]; export const LOKI_KEYWORDS = [...FUNCTIONS, ...PIPE_OPERATORS, ...PIPE_PARSERS].map((keyword) => keyword.label); export const lokiGrammar: Grammar = { diff --git a/yarn.lock b/yarn.lock index 67a347ed41b..3b8317962fa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5070,12 +5070,12 @@ __metadata: languageName: node linkType: hard -"@grafana/lezer-logql@npm:0.1.1": - version: 0.1.1 - resolution: "@grafana/lezer-logql@npm:0.1.1" +"@grafana/lezer-logql@npm:0.1.2": + version: 0.1.2 + resolution: "@grafana/lezer-logql@npm:0.1.2" peerDependencies: "@lezer/lr": ^1.0.0 - checksum: b0d71e5670c54d92fbc4bd92fe72cfb27469cc9797d5595b284f9d2b86782584cb1a9b8bac51af07648b807a9287e7159dcdd0e1fc3e32a2e79d52b3a7700e4b + checksum: 4b62fda9c2c7e29c48f7485df19573b96bfd34539529283980f24e9931f81b44c86b1e655bae5f50730d9d7b37bae87e6f7f7773dafaf4f6acef90b2c2cb776f languageName: node linkType: hard @@ -21995,7 +21995,7 @@ __metadata: "@grafana/faro-core": 1.0.0-beta2 "@grafana/faro-web-sdk": 1.0.0-beta2 "@grafana/google-sdk": 0.0.4 - "@grafana/lezer-logql": 0.1.1 + "@grafana/lezer-logql": 0.1.2 "@grafana/monaco-logql": ^0.0.6 "@grafana/runtime": "workspace:*" "@grafana/scenes": ^0.0.14