Logs: Fix creating of detected fields for each letter in log line (#40507) (#40560)

* 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:
Grot (@grafanabot)
2021-10-18 11:44:09 +02:00
committed by GitHub
co-authored by Ivana Huckova
parent 014e5ba0c2
commit a3842b4111
3 changed files with 8 additions and 3 deletions
+2 -2
View File
@@ -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();
});
+5 -1
View File
@@ -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';
},
},