[v10.0.x] Logs: Do not insert log-line into log-fields in json download (#70954)

Logs: Do not insert log-line into log-fields in json download (#70901)

* logs: do not insert log-line into log-fields in json download

* fixed test after merge

(cherry picked from commit 2084cc9955)
This commit is contained in:
Gábor Farkas
2023-06-30 13:53:28 +02:00
committed by GitHub
parent 0e092ba6a1
commit e486eaa3b2
3 changed files with 91 additions and 11 deletions
@@ -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,
@@ -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;
}
+29 -4
View File
@@ -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,