Loki: Fix log context when no label types are present (#87587)

This commit is contained in:
Sven Grossmann
2024-05-10 11:52:41 +02:00
committed by GitHub
parent 8213e8a2a6
commit 1c8a77a43d
2 changed files with 69 additions and 1 deletions

View File

@@ -49,6 +49,22 @@ const defaultLogRow = {
timeEpochMs: new Date().getTime(),
} as unknown as LogRowModel;
const frameWithoutTypes = {
rowIndex: 0,
dataFrame: createDataFrame({
fields: [
{
name: 'ts',
type: FieldType.time,
values: [0],
},
],
}),
labels: { bar: 'baz', foo: 'uniqueParsedLabel', xyz: 'abc' },
uid: '1',
timeEpochMs: new Date().getTime(),
} as unknown as LogRowModel;
describe('LogContextProvider', () => {
let logContextProvider: LogContextProvider;
beforeEach(() => {
@@ -493,6 +509,23 @@ describe('LogContextProvider', () => {
expect(result.preservedFiltersApplied).toBe(true);
});
it('should correctly apply preserved labels with no types', async () => {
window.localStorage.setItem(
LOKI_LOG_CONTEXT_PRESERVED_LABELS,
JSON.stringify({
removedLabels: ['bar'],
selectedExtractedLabels: ['foo'],
})
);
const result = await logContextProvider.getInitContextFilters(frameWithoutTypes, queryWithParser);
expect(result.contextFilters).toEqual([
{ enabled: false, nonIndexed: false, label: 'bar', value: 'baz' }, // disabled real label
{ enabled: true, nonIndexed: false, label: 'foo', value: 'uniqueParsedLabel' }, // enabled parsed label
{ enabled: true, nonIndexed: false, label: 'xyz', value: 'abc' },
]);
expect(result.preservedFiltersApplied).toBe(true);
});
it('should use contextFilters from row labels if all real labels are disabled', async () => {
window.localStorage.setItem(
LOKI_LOG_CONTEXT_PRESERVED_LABELS,
@@ -510,6 +543,23 @@ describe('LogContextProvider', () => {
expect(result.preservedFiltersApplied).toBe(false);
});
it('should use contextFilters from row labels if all real labels are disabled with no types', async () => {
window.localStorage.setItem(
LOKI_LOG_CONTEXT_PRESERVED_LABELS,
JSON.stringify({
removedLabels: ['bar', 'xyz'],
selectedExtractedLabels: ['foo'],
})
);
const result = await logContextProvider.getInitContextFilters(frameWithoutTypes, queryWithParser);
expect(result.contextFilters).toEqual([
{ enabled: false, nonIndexed: false, label: 'bar', value: 'baz' }, // enabled real label
{ enabled: true, nonIndexed: false, label: 'foo', value: 'uniqueParsedLabel' },
{ enabled: false, nonIndexed: false, label: 'xyz', value: 'abc' }, // enabled real label
]);
expect(result.preservedFiltersApplied).toBe(true);
});
it('should not introduce new labels as context filters', async () => {
window.localStorage.setItem(
LOKI_LOG_CONTEXT_PRESERVED_LABELS,
@@ -526,6 +576,23 @@ describe('LogContextProvider', () => {
]);
expect(result.preservedFiltersApplied).toBe(true);
});
it('should not introduce new labels as context filters with no label types', async () => {
window.localStorage.setItem(
LOKI_LOG_CONTEXT_PRESERVED_LABELS,
JSON.stringify({
removedLabels: ['bar'],
selectedExtractedLabels: ['foo', 'new'],
})
);
const result = await logContextProvider.getInitContextFilters(frameWithoutTypes, queryWithParser);
expect(result.contextFilters).toEqual([
{ enabled: false, nonIndexed: false, label: 'bar', value: 'baz' },
{ enabled: true, nonIndexed: false, label: 'foo', value: 'uniqueParsedLabel' },
{ enabled: true, nonIndexed: false, label: 'xyz', value: 'abc' },
]);
expect(result.preservedFiltersApplied).toBe(true);
});
});
});

View File

@@ -338,11 +338,12 @@ export class LogContextProvider {
const contextFilters: ContextFilter[] = [];
Object.entries(rowLabels).forEach(([label, value]) => {
const labelType = getLabelTypeFromFrame(label, row.dataFrame, row.rowIndex);
const filter: ContextFilter = {
label,
value: value,
enabled: allLabels.includes(label),
nonIndexed: getLabelTypeFromFrame(label, row.dataFrame, row.rowIndex) !== LabelType.Indexed,
nonIndexed: labelType !== null && labelType !== LabelType.Indexed,
};
contextFilters.push(filter);