From 82f186c6bc5c12e4992c31121e81a4bb6f4fd697 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 30 May 2024 19:56:50 +0200 Subject: [PATCH] wip --- lib/logstorage/logfmt_parser.go | 17 ++++++++++++++--- lib/logstorage/logfmt_parser_test.go | 8 ++++---- lib/logstorage/pipe_unpack_logfmt_test.go | 12 +----------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/logstorage/logfmt_parser.go b/lib/logstorage/logfmt_parser.go index 31901cd3c..e2a5008dc 100644 --- a/lib/logstorage/logfmt_parser.go +++ b/lib/logstorage/logfmt_parser.go @@ -15,6 +15,10 @@ func (p *logfmtParser) reset() { } func (p *logfmtParser) addField(name, value string) { + name = strings.TrimSpace(name) + if name == "" && value == "" { + return + } p.fields = append(p.fields, Field{ Name: name, Value: value, @@ -24,14 +28,21 @@ func (p *logfmtParser) addField(name, value string) { func (p *logfmtParser) parse(s string) { for { // Search for field name - n := strings.IndexByte(s, '=') + n := strings.IndexAny(s, "= ") if n < 0 { - // field name couldn't be read + // empty value + p.addField(s, "") return } - name := strings.TrimSpace(s[:n]) + name := s[:n] + ch := s[n] s = s[n+1:] + if ch == ' ' { + // empty value + p.addField(name, "") + continue + } if len(s) == 0 { p.addField(name, "") return diff --git a/lib/logstorage/logfmt_parser_test.go b/lib/logstorage/logfmt_parser_test.go index 60161271e..711dd26dd 100644 --- a/lib/logstorage/logfmt_parser_test.go +++ b/lib/logstorage/logfmt_parser_test.go @@ -22,9 +22,9 @@ func TestLogfmtParser(t *testing.T) { f(`foo=bar`, `{"foo":"bar"}`) f(`foo="bar=baz x=y"`, `{"foo":"bar=baz x=y"}`) f(`foo=`, `{"foo":""}`) + f(`foo`, `{"foo":""}`) + f(`foo bar`, `{"foo":"","bar":""}`) + f(`foo bar=baz`, `{"foo":"","bar":"baz"}`) f(`foo=bar baz="x y" a=b`, `{"foo":"bar","baz":"x y","a":"b"}`) - - // errors - f(`foo`, `{}`) - f(`foo=bar baz=x z qwe`, `{"foo":"bar","baz":"x"}`) + f(` foo=bar baz=x =z qwe`, `{"foo":"bar","baz":"x","":"z","qwe":""}`) } diff --git a/lib/logstorage/pipe_unpack_logfmt_test.go b/lib/logstorage/pipe_unpack_logfmt_test.go index 90720118d..56099de56 100644 --- a/lib/logstorage/pipe_unpack_logfmt_test.go +++ b/lib/logstorage/pipe_unpack_logfmt_test.go @@ -191,17 +191,6 @@ func TestPipeUnpackLogfmt(t *testing.T) { }, }) - // single row, unpack from non-json field - f("unpack_logfmt from x", [][]Field{ - { - {"x", `foobar`}, - }, - }, [][]Field{ - { - {"x", `foobar`}, - }, - }) - // single row, unpack from non-logfmt f("unpack_logfmt from x", [][]Field{ { @@ -210,6 +199,7 @@ func TestPipeUnpackLogfmt(t *testing.T) { }, [][]Field{ { {"x", `foobar`}, + {"foobar", ""}, }, })