Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract parser: update heuristic for mixed logs #717

Merged
merged 6 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,4 @@ svgs
Dont
REFID
unroute
myown
6 changes: 3 additions & 3 deletions src/services/fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ describe('updateParserFromDataFrame', () => {
});
jest.mocked(getLogsFormatVariable).mockReturnValue(logsFmtVariable);

it('should exclude json errors', () => {
it('should exclude mixed parser errors', () => {
const dataFrame = createDataFrame({
refId: 'A',
fields: [
{ name: 'Time', type: FieldType.time, values: [0] },
{
name: 'Line',
type: FieldType.string,
values: ['{"jsonLabel": "jsonValue"}'],
values: ['{"jsonLabel": "jsonValue"}', 'level=error myown=summer'],
},
{ name: 'labelTypes', type: FieldType.other, values: [{ field1: 'I', field2: 'P', field3: 'S' }] },
],
});
const scene = {} as SceneObject;

expect(updateParserFromDataFrame(dataFrame, scene)).toEqual({
type: 'json',
type: 'mixed',
fields: ['field2'],
});
expect(logsFmtVariable.state.value).toEqual('| json | logfmt | drop __error__, __error_details__');
Expand Down
25 changes: 22 additions & 3 deletions src/services/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export type DetectedLabelsResponse = {
detectedLabels: DetectedLabel[];
};

type ExtractedFieldsType = 'logfmt' | 'json' | 'mixed';

interface ExtractedFields {
type: 'logfmt' | 'json';
type: ExtractedFieldsType;
fields: string[];
}

Expand All @@ -30,7 +32,7 @@ export function updateParserFromDataFrame(frame: DataFrame, sceneRef: SceneObjec
let newType;
if (!res.type) {
newType = '';
} else if (res.type === 'json') {
} else if (res.type === 'mixed') {
newType = `| json | logfmt | drop __error__, __error_details__`;
} else {
newType = ` | ${res.type}`;
Expand All @@ -55,8 +57,25 @@ export function extractParserAndFieldsFromDataFrame(data: DataFrame) {
}, {}) ?? {}
);

const types: ExtractedFieldsType[] = [];
const linesField = data.fields.find((f) => f.name === 'Line' || f.name === 'body');
result.type = linesField?.values[0]?.[0] === '{' ? 'json' : 'logfmt';

if (!linesField) {
return result;
}

for (let i = 0; i < linesField.values.length && types.length < 2; i++) {
const line = linesField.values[i].trim();
if (line.startsWith('{') && line.endsWith('}')) {
if (!types.includes('json')) {
types.push('json');
}
} else if (!types.includes('logfmt')) {
types.push('logfmt');
}
}

result.type = types.length === 1 ? types[0] : 'mixed';

return result;
}
Expand Down
Loading