LogParser: safely stringify field value (#71873)

* LogParser: safely stringify field value

* LogParser: move null check to the top
This commit is contained in:
Matias Chomicki
2023-07-18 18:39:19 +02:00
committed by GitHub
parent 3381ecaebc
commit 7ef357aa32
@@ -1,6 +1,7 @@
import memoizeOne from 'memoize-one';
import { DataFrame, Field, FieldType, LinkModel, LogRowModel } from '@grafana/data';
import { safeStringifyValue } from 'app/core/utils/explore';
import { ExploreFieldLinkModel } from 'app/features/explore/utils/links';
export type FieldDef = {
@@ -69,9 +70,14 @@ export const getDataframeFields = memoizeOne(
.filter((field, index) => !shouldRemoveField(field, index, row))
.map((field) => {
const links = getFieldLinks ? getFieldLinks(field, row.rowIndex, row.dataFrame) : [];
const fieldVal = field.values[row.rowIndex];
const outputVal =
typeof fieldVal === 'string' || typeof fieldVal === 'number'
? fieldVal.toString()
: safeStringifyValue(fieldVal);
return {
keys: [field.name],
values: [field.values[row.rowIndex].toString()],
values: [outputVal],
links: links,
fieldIndex: field.index,
};
@@ -80,6 +86,11 @@ export const getDataframeFields = memoizeOne(
);
function shouldRemoveField(field: Field, index: number, row: LogRowModel) {
// field that has empty value (we want to keep 0 or empty string)
if (field.values[row.rowIndex] == null) {
return true;
}
// hidden field, remove
if (field.config.custom?.hidden) {
return true;
@@ -90,11 +101,6 @@ function shouldRemoveField(field: Field, index: number, row: LogRowModel) {
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: