diff --git a/public/app/features/logs/components/logParser.test.ts b/public/app/features/logs/components/logParser.test.ts index 474f8cbe630..01141cb5a3a 100644 --- a/public/app/features/logs/components/logParser.test.ts +++ b/public/app/features/logs/components/logParser.test.ts @@ -12,6 +12,7 @@ describe('logParser', () => { dataFrame: new MutableDataFrame({ refId: 'A', fields: [ + testLineField, testStringField, { name: 'labels', @@ -34,6 +35,7 @@ describe('logParser', () => { dataFrame: new MutableDataFrame({ refId: 'A', fields: [ + testLineField, testStringField, { name: 'labels', @@ -49,12 +51,42 @@ describe('logParser', () => { expect(fields.find((field) => field.keys[0] === 'labels')).not.toBe(undefined); }); + it('should not filter out field with labels name and other type and datalinks', () => { + const logRow = createLogRow({ + entryFieldIndex: 10, + dataFrame: new MutableDataFrame({ + refId: 'A', + fields: [ + testLineField, + testStringField, + { + name: 'labels', + type: FieldType.other, + config: { + links: [ + { + title: 'test1', + url: 'url1', + }, + ], + }, + values: [{ place: 'luna', source: 'data' }], + }, + ], + }), + }); + const fields = getAllFields(logRow); + expect(fields.length).toBe(2); + expect(fields.find((field) => field.keys[0] === 'labels')).not.toBe(undefined); + }); + it('should filter out field with id name', () => { const logRow = createLogRow({ entryFieldIndex: 10, dataFrame: new MutableDataFrame({ refId: 'A', fields: [ + testLineField, testStringField, { name: 'id', @@ -110,7 +142,7 @@ describe('logParser', () => { entryFieldIndex: 10, dataFrame: new MutableDataFrame({ refId: 'A', - fields: [{ ...testStringField }], + fields: [testLineField, { ...testStringField }], }), }); @@ -182,6 +214,13 @@ describe('logParser', () => { }); }); +const testLineField = { + name: 'body', + type: FieldType.string, + config: {}, + values: ['line1'], +}; + const testStringField = { name: 'test_field_string', type: FieldType.string, diff --git a/public/app/features/logs/components/logParser.ts b/public/app/features/logs/components/logParser.ts index 5950bb14c2e..088c34b670f 100644 --- a/public/app/features/logs/components/logParser.ts +++ b/public/app/features/logs/components/logParser.ts @@ -80,6 +80,23 @@ export const getDataframeFields = memoizeOne( ); function shouldRemoveField(field: Field, index: number, row: LogRowModel) { + // hidden field, remove + if (field.config.custom?.hidden) { + return true; + } + + // field with data-links, keep + if ((field.config.links ?? []).length > 0) { + return false; + } + + // field that has empty value (we want to keep 0 or empty string) + if (field.values[row.rowIndex] == null) { + return true; + } + + // the remaining checks use knowledge of how we parse logs-dataframes + // Remove field if it is: // "labels" field that is in Loki used to store all labels if (field.name === 'labels' && field.type === FieldType.other) { @@ -97,13 +114,12 @@ function shouldRemoveField(field: Field, index: number, row: LogRowModel) { ) { return true; } - // hidden field - if (field.config.custom?.hidden) { - return true; - } - // field that has empty value (we want to keep 0 or empty string) - if (field.values[row.rowIndex] == null) { + + // first string-field is the log-line + const firstStringFieldIndex = row.dataFrame.fields.findIndex((f) => f.type === FieldType.string); + if (firstStringFieldIndex === index) { return true; } + return false; } diff --git a/public/app/features/logs/utils.test.ts b/public/app/features/logs/utils.test.ts index 164994cb6ae..34fb4e2b9f5 100644 --- a/public/app/features/logs/utils.test.ts +++ b/public/app/features/logs/utils.test.ts @@ -221,9 +221,19 @@ describe('checkLogsError()', () => { describe('logRowsToReadableJson', () => { const testRow: LogRowModel = { - rowIndex: 1, + rowIndex: 0, entryFieldIndex: 0, - dataFrame: new MutableDataFrame(), + dataFrame: { + length: 1, + fields: [ + { + name: 'body', + type: FieldType.string, + config: {}, + values: ['test entry'], + }, + ], + }, entry: 'test entry', hasAnsi: false, hasUnescapedContent: false, @@ -239,8 +249,23 @@ describe('logRowsToReadableJson', () => { timeUtc: '', uid: '2', }; - const testDf = new MutableDataFrame(); - testDf.addField({ name: 'foo2', values: ['bar2'] }); + const testDf: DataFrame = { + length: 1, + fields: [ + { + name: 'body', + type: FieldType.string, + config: {}, + values: ['test entry'], + }, + { + name: 'foo2', + type: FieldType.string, + config: {}, + values: ['bar2'], + }, + ], + }; const testRow2: LogRowModel = { rowIndex: 0, entryFieldIndex: -1,