From cfdb9db775932e90a2f8e898531490a059d154b5 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Tue, 23 Mar 2021 09:17:55 +0100 Subject: [PATCH] Logs: If log message missing, use empty string (#32080) * Fix error when message in undefined and add test * Fix typo --- public/app/core/logs_model.test.ts | 50 ++++++++++++++++++++++++++++++ public/app/core/logs_model.ts | 3 +- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/public/app/core/logs_model.test.ts b/public/app/core/logs_model.test.ts index 89c6cf027c1..5f0f02c2b80 100644 --- a/public/app/core/logs_model.test.ts +++ b/public/app/core/logs_model.test.ts @@ -678,6 +678,56 @@ describe('logSeriesToLogsModel', () => { }, ]); }); + + it('should return empty string if message field is undefined', () => { + const logSeries: DataFrame[] = [ + toDataFrame({ + fields: [ + { + name: 'ts', + type: FieldType.time, + values: ['1970-01-01T00:00:01Z', '1970-02-01T00:00:01Z', '1970-03-01T00:00:01Z'], + }, + { + name: 'line', + type: FieldType.string, + values: ['WARN boooo 0', undefined, 'WARN boooo 2'], + labels: { + foo: 'bar', + level: 'dbug', + }, + }, + { + name: 'id', + type: FieldType.string, + values: ['0', '1', '2'], + }, + ], + refId: 'A', + meta: {}, + }), + ]; + + const logsModel = dataFrameToLogsModel(logSeries, 0, 'utc'); + expect(logsModel.rows).toHaveLength(3); + expect(logsModel.rows).toMatchObject([ + { + entry: 'WARN boooo 0', + labels: { foo: 'bar' }, + logLevel: LogLevel.debug, + }, + { + entry: '', + labels: { foo: 'bar' }, + logLevel: LogLevel.debug, + }, + { + entry: 'WARN boooo 2', + labels: { foo: 'bar' }, + logLevel: LogLevel.debug, + }, + ]); + }); }); describe('getSeriesProperties()', () => { diff --git a/public/app/core/logs_model.ts b/public/app/core/logs_model.ts index 3df00d2cdf1..96dced712f4 100644 --- a/public/app/core/logs_model.ts +++ b/public/app/core/logs_model.ts @@ -356,7 +356,8 @@ export function logSeriesToLogsModel(logSeries: DataFrame[]): LogsModel | undefi const tsNs = timeNanosecondField ? timeNanosecondField.values.get(j) : undefined; const timeEpochNs = tsNs ? tsNs : time.valueOf() + '000000'; - const messageValue: unknown = stringField.values.get(j); + // In edge cases, this can be undefined. If undefined, we want to replace it with empty string. + const messageValue: unknown = stringField.values.get(j) ?? ''; // This should be string but sometimes isn't (eg elastic) because the dataFrame is not strongly typed. const message: string = typeof messageValue === 'string' ? messageValue : JSON.stringify(messageValue);