Loki: Add maxLines option to getParserAndLabelKeys (#77460)

* Loki: Add logsCount option to getParserAndLabelKeys

* Rename logsCount to maxLines
This commit is contained in:
Ivana Huckova 2023-11-01 17:22:33 +01:00 committed by GitHub
parent 5d5f8dfc52
commit a38c9d4f44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import { AbstractLabelOperator, DataFrame } from '@grafana/data'; import { AbstractLabelOperator, DataFrame } from '@grafana/data';
import LanguageProvider from './LanguageProvider'; import LanguageProvider from './LanguageProvider';
import { LokiDatasource } from './datasource'; import { DEFAULT_MAX_LINES_SAMPLE, LokiDatasource } from './datasource';
import { createLokiDatasource, createMetadataRequest } from './mocks'; import { createLokiDatasource, createMetadataRequest } from './mocks';
import { import {
extractLogParserFromDataFrame, extractLogParserFromDataFrame,
@ -275,6 +275,40 @@ describe('Query imports', () => {
}); });
expect(extractLogParserFromDataFrameMock).not.toHaveBeenCalled(); expect(extractLogParserFromDataFrameMock).not.toHaveBeenCalled();
}); });
it('calls dataSample with correct default maxLines', async () => {
jest.spyOn(datasource, 'getDataSamples').mockResolvedValue([]);
expect(await languageProvider.getParserAndLabelKeys('{place="luna"}')).toEqual({
extractedLabelKeys: [],
unwrapLabelKeys: [],
hasJSON: false,
hasLogfmt: false,
hasPack: false,
});
expect(datasource.getDataSamples).toHaveBeenCalledWith({
expr: '{place="luna"}',
maxLines: DEFAULT_MAX_LINES_SAMPLE,
refId: 'data-samples',
});
});
it('calls dataSample with correctly set sampleSize', async () => {
jest.spyOn(datasource, 'getDataSamples').mockResolvedValue([]);
expect(await languageProvider.getParserAndLabelKeys('{place="luna"}', { maxLines: 5 })).toEqual({
extractedLabelKeys: [],
unwrapLabelKeys: [],
hasJSON: false,
hasLogfmt: false,
hasPack: false,
});
expect(datasource.getDataSamples).toHaveBeenCalledWith({
expr: '{place="luna"}',
maxLines: 5,
refId: 'data-samples',
});
});
}); });
}); });

View File

@ -4,7 +4,7 @@ import Prism from 'prismjs';
import { LanguageProvider, AbstractQuery, KeyValue } from '@grafana/data'; import { LanguageProvider, AbstractQuery, KeyValue } from '@grafana/data';
import { extractLabelMatchers, processLabels, toPromLikeExpr } from 'app/plugins/datasource/prometheus/language_utils'; import { extractLabelMatchers, processLabels, toPromLikeExpr } from 'app/plugins/datasource/prometheus/language_utils';
import { LokiDatasource } from './datasource'; import { DEFAULT_MAX_LINES_SAMPLE, LokiDatasource } from './datasource';
import { import {
extractLabelKeysFromDataFrame, extractLabelKeysFromDataFrame,
extractLogParserFromDataFrame, extractLogParserFromDataFrame,
@ -251,11 +251,21 @@ export default class LokiLanguageProvider extends LanguageProvider {
* - `unwrapLabelKeys`: An array of label keys that can be used for unwrapping log data. * - `unwrapLabelKeys`: An array of label keys that can be used for unwrapping log data.
* *
* @param streamSelector - The selector for the log stream you want to analyze. * @param streamSelector - The selector for the log stream you want to analyze.
* @param {Object} [options] - Optional parameters.
* @param {number} [options.maxLines] - The number of log lines requested when determining parsers and label keys.
* Smaller maxLines is recommended for improved query performance. The default count is 10.
* @returns A promise containing an object with parser and label key information. * @returns A promise containing an object with parser and label key information.
* @throws An error if the fetch operation fails. * @throws An error if the fetch operation fails.
*/ */
async getParserAndLabelKeys(streamSelector: string): Promise<ParserAndLabelKeysResult> { async getParserAndLabelKeys(
const series = await this.datasource.getDataSamples({ expr: streamSelector, refId: 'data-samples' }); streamSelector: string,
options?: { maxLines?: number }
): Promise<ParserAndLabelKeysResult> {
const series = await this.datasource.getDataSamples({
expr: streamSelector,
refId: 'data-samples',
maxLines: options?.maxLines || DEFAULT_MAX_LINES_SAMPLE,
});
if (!series.length) { if (!series.length) {
return { extractedLabelKeys: [], unwrapLabelKeys: [], hasJSON: false, hasLogfmt: false, hasPack: false }; return { extractedLabelKeys: [], unwrapLabelKeys: [], hasJSON: false, hasLogfmt: false, hasPack: false };

View File

@ -99,6 +99,7 @@ import { LokiVariableSupport } from './variables';
export type RangeQueryOptions = DataQueryRequest<LokiQuery> | AnnotationQueryRequest<LokiQuery>; export type RangeQueryOptions = DataQueryRequest<LokiQuery> | AnnotationQueryRequest<LokiQuery>;
export const DEFAULT_MAX_LINES = 1000; export const DEFAULT_MAX_LINES = 1000;
export const DEFAULT_MAX_LINES_SAMPLE = 10;
export const LOKI_ENDPOINT = '/loki/api/v1'; export const LOKI_ENDPOINT = '/loki/api/v1';
export const REF_ID_DATA_SAMPLES = 'loki-data-samples'; export const REF_ID_DATA_SAMPLES = 'loki-data-samples';
export const REF_ID_STARTER_ANNOTATION = 'annotation-'; export const REF_ID_STARTER_ANNOTATION = 'annotation-';
@ -761,8 +762,7 @@ export class LokiDatasource
expr: query.expr, expr: query.expr,
queryType: LokiQueryType.Range, queryType: LokiQueryType.Range,
refId: REF_ID_DATA_SAMPLES, refId: REF_ID_DATA_SAMPLES,
// For samples we limit the request to 10 lines, so queries are small and fast maxLines: query.maxLines || DEFAULT_MAX_LINES_SAMPLE,
maxLines: 10,
supportingQueryType: SupportingQueryType.DataSample, supportingQueryType: SupportingQueryType.DataSample,
}; };

View File

@ -138,10 +138,16 @@ try {
* - `unwrapLabelKeys`: An array of label keys that can be used for unwrapping log data. * - `unwrapLabelKeys`: An array of label keys that can be used for unwrapping log data.
* *
* @param streamSelector - The selector for the log stream you want to analyze. * @param streamSelector - The selector for the log stream you want to analyze.
* @param {Object} [options] - Optional parameters.
* @param {number} [options.maxLines] - The number of log lines requested when determining parsers and label keys.
* Smaller maxLines is recommended for improved query performance. The default count is 10.
* @returns A promise containing an object with parser and label key information. * @returns A promise containing an object with parser and label key information.
* @throws An error if the fetch operation fails. * @throws An error if the fetch operation fails.
*/ */
async function getParserAndLabelKeys(streamSelector: string): Promise<{ async function getParserAndLabelKeys(
streamSelector: string,
options?: { maxLines?: number }
): Promise<{
extractedLabelKeys: string[]; extractedLabelKeys: string[];
hasJSON: boolean; hasJSON: boolean;
hasLogfmt: boolean; hasLogfmt: boolean;
@ -154,7 +160,7 @@ async function getParserAndLabelKeys(streamSelector: string): Promise<{
*/ */
const streamSelector = '{job="grafana"}'; const streamSelector = '{job="grafana"}';
try { try {
const parserAndLabelKeys = await getParserAndLabelKeys(streamSelector); const parserAndLabelKeys = await getParserAndLabelKeys(streamSelector, { maxLines: 5 });
console.log(parserAndLabelKeys); console.log(parserAndLabelKeys);
} catch (error) { } catch (error) {
console.error(`Error fetching parser and label keys: ${error.message}`); console.error(`Error fetching parser and label keys: ${error.message}`);