feat(loki-monaco-editor): use query hints implementation to parse expression

This commit is contained in:
Matias Chomicki 2022-09-21 16:47:10 +02:00
parent 39f81f77b2
commit 2719fa4454
2 changed files with 14 additions and 78 deletions

View File

@ -1,7 +1,6 @@
import { chain, difference } from 'lodash'; import { chain, difference } from 'lodash';
import LRU from 'lru-cache'; import LRU from 'lru-cache';
import Prism, { Grammar } from 'prismjs'; import Prism, { Grammar } from 'prismjs';
import { lastValueFrom } from 'rxjs';
import { import {
dateTime, dateTime,
@ -9,9 +8,7 @@ import {
LanguageProvider, LanguageProvider,
HistoryItem, HistoryItem,
AbstractQuery, AbstractQuery,
CoreApp,
DataFrame, DataFrame,
DataQueryRequest,
FieldType, FieldType,
getParser, getParser,
LogsParsers, LogsParsers,
@ -25,6 +22,7 @@ import {
} from 'app/plugins/datasource/prometheus/language_utils'; } from 'app/plugins/datasource/prometheus/language_utils';
import { LokiDatasource } from './datasource'; import { LokiDatasource } from './datasource';
import { extractLogParserFromDataFrame } from './responseUtils';
import syntax, { FUNCTIONS, PIPE_PARSERS, PIPE_OPERATORS } from './syntax'; import syntax, { FUNCTIONS, PIPE_PARSERS, PIPE_OPERATORS } from './syntax';
import { LokiQuery, LokiQueryType } from './types'; import { LokiQuery, LokiQueryType } from './types';
@ -75,48 +73,6 @@ export function addHistoryMetadata(item: CompletionItem, history: LokiHistoryIte
}; };
} }
function extractLogInfo(frame: DataFrame): { extractedLabelKeys: string[]; hasLogfmt: boolean; hasJSON: boolean } {
const lineField = frame.fields.find((field) => field.type === FieldType.string);
if (lineField == null) {
return { extractedLabelKeys: [], hasJSON: false, hasLogfmt: false };
}
const texts: string[] = lineField.values.toArray();
let hasJSON = false;
let hasLogfmt = false;
const keys = new Set<string>();
texts.forEach((text) => {
const parser = getParser(text);
if (parser === LogsParsers.JSON) {
// FIXME: we javascript-parse here, we should call Loki probably
parser
.getFields(text)
.map((field) => parser.getLabelFromField(field))
.forEach((key) => {
keys.add(key);
});
hasJSON = true;
}
if (parser === LogsParsers.logfmt) {
// FIXME: we javascript-parse here, we should call Loki probably
parser
.getFields(text)
.map((field) => parser.getLabelFromField(field))
.forEach((key) => {
keys.add(key);
});
hasLogfmt = true;
}
});
const extractedLabelKeys = Array.from(keys);
extractedLabelKeys.sort();
return { extractedLabelKeys, hasLogfmt, hasJSON };
}
export default class LokiLanguageProvider extends LanguageProvider { export default class LokiLanguageProvider extends LanguageProvider {
labelKeys: string[]; labelKeys: string[];
labelFetchTs: number; labelFetchTs: number;
@ -518,36 +474,16 @@ export default class LokiLanguageProvider extends LanguageProvider {
} }
async getLogInfo(selector: string): Promise<{ extractedLabelKeys: string[]; hasJSON: boolean; hasLogfmt: boolean }> { async getLogInfo(selector: string): Promise<{ extractedLabelKeys: string[]; hasJSON: boolean; hasLogfmt: boolean }> {
const request: DataQueryRequest<LokiQuery> = { let series = [];
requestId: 'LOKI_LOG_INFO', try {
interval: '1s', series = await this.datasource.getDataSamples({ expr: selector, refId: 'data-samples' });
intervalMs: 1000, } catch (e) {
range: this.datasource.getTimeRange(),
scopedVars: {},
timezone: 'UTC', // FIXME
app: CoreApp.Explore, // FIXME
startTime: 0, // FIXME
targets: [
{
refId: 'A',
expr: selector,
maxLines: 5, // FIXME
},
],
};
return lastValueFrom(this.datasource.query(request)).then(
(response) => {
const frame: DataFrame | undefined = response.data[0];
if (frame == null) {
return { extractedLabelKeys: [], hasJSON: false, hasLogfmt: false }; return { extractedLabelKeys: [], hasJSON: false, hasLogfmt: false };
} }
return extractLogInfo(frame);
}, const { hasLogfmt, hasJSON } = extractLogParserFromDataFrame(series[0]);
(error) => {
// TODO: better error handling // TODO: figure out extractedLabelKeys
console.error(error); return { extractedLabelKeys: [], hasJSON, hasLogfmt };
return { extractedLabelKeys: [], hasJSON: false, hasLogfmt: false };
}
);
} }
} }

View File

@ -42,8 +42,8 @@ export class CompletionDataProvider {
return data[labelName] ?? []; return data[labelName] ?? [];
} }
getLogInfo(labels: Label[]) { async getLogInfo(labels: Label[]) {
return this.languageProvider.getLogInfo(this.buildSelector(labels)); return await this.languageProvider.getLogInfo(this.buildSelector(labels));
} }
getSeriesLabels(labels: Label[]) { getSeriesLabels(labels: Label[]) {