* Logs: Fix using of JSON parser for strings
* Update packages/grafana-data/src/utils/logs.ts
* Update packages/grafana-data/src/utils/logs.ts
* Update parser typing and documentation
(cherry picked from commit 4e1cf7dea7)
Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
This commit is contained in:
co-authored by
Ivana Huckova
parent
014e5ba0c2
commit
a3842b4111
@@ -131,9 +131,9 @@ export interface LogsParser {
|
||||
getValueFromField: (field: string) => string;
|
||||
/**
|
||||
* Function to verify if this is a valid parser for the given line.
|
||||
* The parser accepts the line unless it returns undefined.
|
||||
* The parser accepts the line if it returns true.
|
||||
*/
|
||||
test: (line: string) => any;
|
||||
test: (line: string) => boolean;
|
||||
}
|
||||
|
||||
export enum LogsDedupDescription {
|
||||
|
||||
@@ -165,6 +165,7 @@ describe('LogsParsers', () => {
|
||||
|
||||
test('should detect format', () => {
|
||||
expect(parser.test('foo')).toBeFalsy();
|
||||
expect(parser.test('"foo"')).toBeFalsy();
|
||||
expect(parser.test('{"foo":"bar"}')).toBeTruthy();
|
||||
});
|
||||
|
||||
|
||||
@@ -83,9 +83,13 @@ export const LogsParsers: { [name: string]: LogsParser } = {
|
||||
getLabelFromField: (field) => (field.match(/^"([^"]+)"\s*:/) || [])[1],
|
||||
getValueFromField: (field) => (field.match(/:\s*(.*)$/) || [])[1],
|
||||
test: (line) => {
|
||||
let parsed;
|
||||
try {
|
||||
return JSON.parse(line);
|
||||
parsed = JSON.parse(line);
|
||||
} catch (error) {}
|
||||
// The JSON parser should only be used for log lines that are valid serialized JSON objects.
|
||||
// If it would be used for a string, detected fields would include each letter as a separate field.
|
||||
return typeof parsed === 'object';
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user