diff --git a/.betterer.results b/.betterer.results index 3171ea37f0c..53e63d02c22 100644 --- a/.betterer.results +++ b/.betterer.results @@ -3805,6 +3805,9 @@ exports[`better eslint`] = { "public/app/features/logs/components/LogLabelStats.tsx:5381": [ [0, 0, 0, "No untranslated strings. Wrap text with ", "0"] ], + "public/app/features/logs/logsFrame.ts:5381": [ + [0, 0, 0, "Do not use any type assertions.", "0"] + ], "public/app/features/logs/utils.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] ], diff --git a/public/app/features/logs/logsFrame.ts b/public/app/features/logs/logsFrame.ts index c8e00f96319..5a9938b6e2b 100644 --- a/public/app/features/logs/logsFrame.ts +++ b/public/app/features/logs/logsFrame.ts @@ -35,14 +35,32 @@ const DATAPLANE_SEVERITY_NAME = 'severity'; const DATAPLANE_ID_NAME = 'id'; const DATAPLANE_LABELS_NAME = 'labels'; +// NOTE: this is a hot fn, we need to avoid allocating new objects here export function logFrameLabelsToLabels(logFrameLabels: LogFrameLabels): Labels { - const result: Labels = {}; + let needsSerialization = false; - Object.entries(logFrameLabels).forEach(([k, v]) => { - result[k] = typeof v === 'string' ? v : JSON.stringify(v); - }); + for (const k in logFrameLabels) { + const v = logFrameLabels[k]; - return result; + if (typeof v !== 'string') { + needsSerialization = true; + break; + } + } + + if (needsSerialization) { + let labels: Labels = {}; + + for (const k in logFrameLabels) { + const v = logFrameLabels[k]; + labels[k] = typeof v === 'string' ? v : JSON.stringify(v); + } + + return labels; + } + + // @ts-ignore + return logFrameLabels as Labels; } export function parseDataplaneLogsFrame(frame: DataFrame): LogsFrame | null {