mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Loki: Fix log context when no label types are present (#87587)
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user