mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Loki: Fix processing of all lines labels in getParserAndLabelKeys (#81483)
* Loki: Fix processing of all lines labels in getParserAndLabelKeys * Refactor * Update comment
This commit is contained in:
parent
85a745ca9d
commit
1671b77546
@ -66,6 +66,34 @@ const frameWithTypes: DataFrame = {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const frameWithMultipleLabels: DataFrame = {
|
||||||
|
length: 1,
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'Time',
|
||||||
|
config: {},
|
||||||
|
type: FieldType.time,
|
||||||
|
values: [1, 2, 3],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'labels',
|
||||||
|
config: {},
|
||||||
|
type: FieldType.other,
|
||||||
|
values: [
|
||||||
|
{ level: 'info', foo: 'bar' },
|
||||||
|
{ level: 'info', foo: 'baz', new: 'yes' },
|
||||||
|
{ level: 'error', foo: 'baz' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Line',
|
||||||
|
config: {},
|
||||||
|
type: FieldType.string,
|
||||||
|
values: ['line1', 'line2', 'line3'],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
describe('dataFrameHasParsingError', () => {
|
describe('dataFrameHasParsingError', () => {
|
||||||
it('handles frame with parsing error', () => {
|
it('handles frame with parsing error', () => {
|
||||||
const input = cloneDeep(frame);
|
const input = cloneDeep(frame);
|
||||||
@ -138,6 +166,11 @@ describe('extractLabelKeysFromDataFrame', () => {
|
|||||||
expect(extractLabelKeysFromDataFrame(input)).toEqual(['level']);
|
expect(extractLabelKeysFromDataFrame(input)).toEqual(['level']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('extracts label keys from all logs', () => {
|
||||||
|
const input = cloneDeep(frameWithMultipleLabels);
|
||||||
|
expect(extractLabelKeysFromDataFrame(input)).toEqual(['level', 'foo', 'new']);
|
||||||
|
});
|
||||||
|
|
||||||
it('extracts indexed label keys', () => {
|
it('extracts indexed label keys', () => {
|
||||||
const input = cloneDeep(frameWithTypes);
|
const input = cloneDeep(frameWithTypes);
|
||||||
expect(extractLabelKeysFromDataFrame(input)).toEqual(['level']);
|
expect(extractLabelKeysFromDataFrame(input)).toEqual(['level']);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { DataFrame, FieldType, isValidGoDuration, Labels } from '@grafana/data';
|
import { DataFrame, FieldType, isValidGoDuration, Labels } from '@grafana/data';
|
||||||
|
|
||||||
import { isBytesString } from './languageUtils';
|
import { isBytesString, processLabels } from './languageUtils';
|
||||||
import { isLogLineJSON, isLogLineLogfmt, isLogLinePacked } from './lineParser';
|
import { isLogLineJSON, isLogLineLogfmt, isLogLinePacked } from './lineParser';
|
||||||
import { LabelType } from './types';
|
import { LabelType } from './types';
|
||||||
|
|
||||||
@ -54,19 +54,26 @@ export function extractLabelKeysFromDataFrame(frame: DataFrame, type: LabelType
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// if there are no label types, only return indexed labels if requested
|
// if there are no label types and type is LabelType.Indexed return all label keys
|
||||||
if (!labelTypeArray?.length) {
|
if (!labelTypeArray?.length) {
|
||||||
if (type === LabelType.Indexed) {
|
if (type === LabelType.Indexed) {
|
||||||
return Object.keys(labelsArray[0]);
|
const { keys: labelKeys } = processLabels(labelsArray);
|
||||||
|
return labelKeys;
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const labelTypes = labelTypeArray[0];
|
// If we have label types, we can return only label keys that match type
|
||||||
|
let labelsSet = new Set<string>();
|
||||||
|
for (let i = 0; i < labelsArray.length; i++) {
|
||||||
|
const labels = labelsArray[i];
|
||||||
|
const labelsType = labelTypeArray[i];
|
||||||
|
|
||||||
const allLabelKeys = Object.keys(labelsArray[0]).filter((k) => labelTypes[k] === type);
|
const allLabelKeys = Object.keys(labels).filter((key) => labelsType[key] === type);
|
||||||
|
labelsSet = new Set([...labelsSet, ...allLabelKeys]);
|
||||||
|
}
|
||||||
|
|
||||||
return allLabelKeys;
|
return Array.from(labelsSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractUnwrapLabelKeysFromDataFrame(frame: DataFrame): string[] {
|
export function extractUnwrapLabelKeysFromDataFrame(frame: DataFrame): string[] {
|
||||||
|
Loading…
Reference in New Issue
Block a user