Skip to content

Commit

Permalink
logs: log-details: handle dataplane-compliant dataframes (grafana#71935)
Browse files Browse the repository at this point in the history
* logs: log-details: handle dataplane-compliant dataframes

* lint fix, removed unused import
  • Loading branch information
gabor authored Jul 26, 2023
1 parent 2dea069 commit 0da1993
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 109 deletions.
1 change: 0 additions & 1 deletion public/app/features/explore/Logs/Logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,6 @@ class UnthemedLogs extends PureComponent<Props, State> {
<div className={styles.logRows} data-testid="logRowsTable">
{/* Width should be full width minus logsnavigation and padding */}
<LogsTable
rows={logRows}
logsSortOrder={this.state.logsSortOrder}
range={this.props.range}
splitOpen={this.props.splitOpen}
Expand Down
34 changes: 1 addition & 33 deletions public/app/features/explore/Logs/LogsTable.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { render, screen, waitFor } from '@testing-library/react';
import React, { ComponentProps } from 'react';

import {
FieldType,
LogLevel,
LogRowModel,
LogsSortOrder,
MutableDataFrame,
standardTransformersRegistry,
toUtc,
} from '@grafana/data';
import { FieldType, LogRowModel, LogsSortOrder, standardTransformersRegistry, toUtc } from '@grafana/data';
import { organizeFieldsTransformer } from '@grafana/data/src/transformations/transformers/organize';
import { config } from '@grafana/runtime';
import { extractFieldsTransformer } from 'app/features/transformers/extractFields/extractFields';
Expand Down Expand Up @@ -68,7 +60,6 @@ describe('LogsTable', () => {
};
return (
<LogsTable
rows={[makeLog({})]}
logsSortOrder={LogsSortOrder.Descending}
splitOpen={() => undefined}
timeZone={'utc'}
Expand Down Expand Up @@ -140,26 +131,3 @@ describe('LogsTable', () => {
});
});
});

const makeLog = (overrides: Partial<LogRowModel>): LogRowModel => {
const uid = overrides.uid || '1';
const entry = `log message ${uid}`;
return {
uid,
entryFieldIndex: 0,
rowIndex: 0,
dataFrame: new MutableDataFrame(),
logLevel: LogLevel.debug,
entry,
hasAnsi: false,
hasUnescapedContent: false,
labels: {},
raw: entry,
timeFromNow: '',
timeEpochMs: 1,
timeEpochNs: '1000000',
timeLocal: '',
timeUtc: '',
...overrides,
};
};
29 changes: 13 additions & 16 deletions public/app/features/explore/Logs/LogsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
applyFieldOverrides,
DataFrame,
Field,
LogRowModel,
LogsSortOrder,
sortDataFrame,
SplitOpen,
Expand All @@ -16,7 +15,7 @@ import {
} from '@grafana/data';
import { config } from '@grafana/runtime';
import { Table } from '@grafana/ui';
import { shouldRemoveField } from 'app/features/logs/components/logParser';
import { separateVisibleFields } from 'app/features/logs/components/logParser';
import { parseLogsFrame } from 'app/features/logs/logsFrame';

import { getFieldLinksForExplore } from '../utils/links';
Expand All @@ -28,7 +27,6 @@ interface Props {
splitOpen: SplitOpen;
range: TimeRange;
logsSortOrder: LogsSortOrder;
rows: LogRowModel[];
}

const getTableHeight = memoizeOne((dataFrames: DataFrame[] | undefined) => {
Expand All @@ -40,7 +38,7 @@ const getTableHeight = memoizeOne((dataFrames: DataFrame[] | undefined) => {
});

export const LogsTable: React.FunctionComponent<Props> = (props) => {
const { timeZone, splitOpen, range, logsSortOrder, width, logsFrames, rows } = props;
const { timeZone, splitOpen, range, logsSortOrder, width, logsFrames } = props;

const [tableFrame, setTableFrame] = useState<DataFrame | undefined>(undefined);

Expand Down Expand Up @@ -129,18 +127,17 @@ export const LogsTable: React.FunctionComponent<Props> = (props) => {
});

// remove fields that should not be displayed
dataFrame.fields.forEach((field: Field, index: number) => {
const row = rows[0]; // we just take the first row as the relevant row
if (shouldRemoveField(field, index, row, false, false)) {
transformations.push({
id: 'organize',
options: {
excludeByName: {
[field.name]: true,
},

const hiddenFields = separateVisibleFields(dataFrame, { keepBody: true, keepTimestamp: true }).hidden;
hiddenFields.forEach((field: Field, index: number) => {
transformations.push({
id: 'organize',
options: {
excludeByName: {
[field.name]: true,
},
});
}
},
});
});
if (transformations.length > 0) {
const [transformedDataFrame] = await lastValueFrom(transformDataFrame(transformations, [dataFrame]));
Expand All @@ -150,7 +147,7 @@ export const LogsTable: React.FunctionComponent<Props> = (props) => {
}
};
prepare();
}, [prepareTableFrame, logsFrames, logsSortOrder, rows]);
}, [prepareTableFrame, logsFrames, logsSortOrder]);

if (!tableFrame) {
return null;
Expand Down
207 changes: 206 additions & 1 deletion public/app/features/logs/components/logParser.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldType, MutableDataFrame } from '@grafana/data';
import { DataFrameType, Field, FieldType, LogRowModel, MutableDataFrame } from '@grafana/data';
import { ExploreFieldLinkModel } from 'app/features/explore/utils/links';

import { createLogRow } from './__mocks__/logRow';
Expand Down Expand Up @@ -199,6 +199,211 @@ describe('logParser', () => {
expect(fields.length).toBe(1);
expect(fields.find((field) => field.keys[0] === testStringField.name)).not.toBe(undefined);
});

describe('dataplane frames', () => {
const makeLogRow = (fields: Field[], entryFieldIndex: number): LogRowModel =>
createLogRow({
entryFieldIndex,
rowIndex: 0,
dataFrame: {
refId: 'A',
fields,
length: fields[0]?.values.length,
meta: {
type: DataFrameType.LogLines,
},
},
});

const expectHasField = (defs: FieldDef[], name: string): void => {
expect(defs.find((field) => field.keys[0] === name)).not.toBe(undefined);
};

it('should filter out fields with data links that have a nullish value', () => {
const createScenario = (value: unknown) =>
makeLogRow(
[
testTimeField,
testLineField,
{
name: 'link',
type: FieldType.string,
config: {
links: [
{
title: 'link1',
url: 'https://example.com',
},
],
},
values: [value],
},
],
1
);

expect(getAllFields(createScenario(null))).toHaveLength(0);
expect(getAllFields(createScenario(undefined))).toHaveLength(0);
expect(getAllFields(createScenario(''))).toHaveLength(1);
expect(getAllFields(createScenario('test'))).toHaveLength(1);
// technically this is a field-type-string, but i will add more
// falsy-values, just to be sure
expect(getAllFields(createScenario(false))).toHaveLength(1);
expect(getAllFields(createScenario(NaN))).toHaveLength(1);
expect(getAllFields(createScenario(0))).toHaveLength(1);
expect(getAllFields(createScenario(-0))).toHaveLength(1);
});

it('should filter out system-fields without data-links, but should keep severity', () => {
const row = makeLogRow(
[
testTimeField,
testLineField,
{
config: {},
name: 'id',
type: FieldType.string,
values: ['id1'],
},
{
config: {},
name: 'attributes',
type: FieldType.other,
values: [{ a: 1, b: 2 }],
},
{
config: {},
name: 'severity',
type: FieldType.string,
values: ['info'],
},
testStringField,
],
1
);

const output = getAllFields(row);

expect(output).toHaveLength(2);
expectHasField(output, 'test_field_string');
expectHasField(output, 'severity');
});

it('should keep system fields with data-links', () => {
const links = [
{
title: 'link1',
url: 'https://example.com',
},
];

const row = makeLogRow(
[
{
...testTimeField,
config: { links },
},
{
...testLineField,
config: { links },
},
{
config: { links },
name: 'id',
type: FieldType.string,
values: ['id1'],
},
{
config: { links },
name: 'attributes',
type: FieldType.other,
values: [{ a: 1, b: 2 }],
},
{
config: { links },
name: 'severity',
type: FieldType.string,
values: ['info'],
},
],
1
);

const output = getAllFields(row);

expect(output).toHaveLength(5);
expectHasField(output, 'timestamp');
expectHasField(output, 'body');
expectHasField(output, 'id');
expectHasField(output, 'attributes');
expectHasField(output, 'severity');
});

it('should filter out config-hidden fields', () => {
const row = makeLogRow(
[
testTimeField,
testLineField,
{
...testStringField,
config: {
custom: {
hidden: true,
},
},
},
],
1
);

const output = getAllFields(row);

expect(output).toHaveLength(0);
});

it('should filter out fields with null values', () => {
const row = makeLogRow(
[
testTimeField,
testLineField,
{
// null-value
config: {},
type: FieldType.string,
name: 'test1',
values: [null],
},
{
// null-value and data-link
config: {
links: [
{
title: 'link1',
url: 'https://example.com',
},
],
},
type: FieldType.string,
name: 'test2',
values: [null],
},
{
// normal value
config: {},
type: FieldType.string,
name: 'test3',
values: ['testvalue'],
},
],
1
);

const output = getAllFields(row);

expect(output).toHaveLength(1);
expectHasField(output, 'test3');
});
});
});

describe('createLogLineLinks', () => {
Expand Down
Loading

0 comments on commit 0da1993

Please sign in to comment.