From 1ad1da8e41be940987829ea23b43da91d4bca441 Mon Sep 17 00:00:00 2001 From: Beni Budiharto Date: Thu, 22 Jul 2021 15:03:51 +0700 Subject: [PATCH] trim leading & trailing spaces for kubeparser --- parser/kubeparser/kubeparser.go | 3 +++ parser/kubeparser/kubeparser_test.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/parser/kubeparser/kubeparser.go b/parser/kubeparser/kubeparser.go index ee54ca0..8b3922c 100644 --- a/parser/kubeparser/kubeparser.go +++ b/parser/kubeparser/kubeparser.go @@ -2,6 +2,7 @@ package kubeparser import ( "fmt" + "strings" "github.com/satyrius/gonx" "github.com/tidwall/gjson" @@ -22,6 +23,8 @@ func NewKubeParser(format string) *KubeParser { // ParseString implements the Parser interface. func (t *KubeParser) ParseString(line string) (map[string]string, error) { actualLogLine := gjson.Get(line, "log").String() + actualLogLine = strings.TrimSpace(actualLogLine) + entry, err := t.parser.ParseString(actualLogLine) if err != nil { return nil, fmt.Errorf("text log parsing err: %w", err) diff --git a/parser/kubeparser/kubeparser_test.go b/parser/kubeparser/kubeparser_test.go index d5ab40c..d6341ba 100644 --- a/parser/kubeparser/kubeparser_test.go +++ b/parser/kubeparser/kubeparser_test.go @@ -10,7 +10,7 @@ import ( func TestKubeParse(t *testing.T) { parser := NewKubeParser(`[$time_local] $request_method "$request" $request_length $body_bytes_sent $status $request_time $upstream_response_time`) - line := `{"log": "[03/Feb/2021:11:22:33 +0800] GET \"GET /order/2145 HTTP/1.1\" 123 518 200 0.544 0.543", "stream":"stdout","time":"2021-07-21T07:26:39.102952491Z"}` + line := `{"log": "[03/Feb/2021:11:22:33 +0800] GET \"GET /order/2145 HTTP/1.1\" 123 518 200 0.544 0.543\n", "stream":"stdout","time":"2021-07-21T07:26:39.102952491Z"}` got, err := parser.ParseString(line) require.NoError(t, err)