-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extract JSON fields from log lines
Confirmed to work with Elastic Filebeat. Signed-off-by: Alexey Palazhchenko <alexey.palazhchenko@talos-systems.com>
- Loading branch information
Showing
5 changed files
with
161 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package logging | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
"time" | ||
|
||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime" | ||
) | ||
|
||
func parseLogLine(l []byte, now time.Time) *runtime.LogEvent { | ||
e := &runtime.LogEvent{ | ||
Msg: string(l), | ||
Time: now, | ||
Level: zapcore.InfoLevel, | ||
} | ||
|
||
var m map[string]interface{} | ||
if err := json.Unmarshal(l, &m); err != nil { | ||
return e | ||
} | ||
|
||
if msgS, ok := m["msg"].(string); ok { | ||
e.Msg = strings.TrimSpace(msgS) | ||
|
||
delete(m, "msg") | ||
} | ||
|
||
for _, k := range []string{"time", "ts"} { | ||
if timeS, ok := m[k].(string); ok { | ||
t, err := time.Parse(time.RFC3339Nano, timeS) | ||
if err == nil { | ||
e.Time = t | ||
|
||
delete(m, k) | ||
|
||
break | ||
} | ||
} | ||
} | ||
|
||
if levelS, ok := m["level"].(string); ok { | ||
levelS = strings.ToLower(levelS) | ||
|
||
// convert containerd's logrus' level to zap's level | ||
if levelS == "warning" { | ||
levelS = "warn" | ||
} | ||
|
||
var level zapcore.Level | ||
if err := level.UnmarshalText([]byte(levelS)); err == nil { | ||
e.Level = level | ||
|
||
delete(m, "level") | ||
} | ||
} | ||
|
||
e.Fields = m | ||
|
||
return e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package logging //nolint:testpackage | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime" | ||
) | ||
|
||
func TestParseLogLine(t *testing.T) { | ||
t.Parallel() | ||
|
||
now := time.Date(2021, 10, 19, 12, 42, 37, 123456789, time.UTC) | ||
|
||
for name, tc := range map[string]struct { | ||
l string | ||
expected *runtime.LogEvent | ||
}{ | ||
"machined": { | ||
l: `[talos] task updateBootloader (1/1): done, 219.885384ms`, | ||
expected: &runtime.LogEvent{ | ||
Msg: `[talos] task updateBootloader (1/1): done, 219.885384ms`, | ||
Time: now, | ||
Level: zapcore.InfoLevel, | ||
}, | ||
}, | ||
"etcd-zap": { | ||
l: `{"level":"info","ts":"2021-10-19T14:53:05.815Z","caller":"mvcc/kvstore_compaction.go:57","msg":"finished scheduled compaction","compact-revision":34567,"took":"21.041639ms"}`, | ||
expected: &runtime.LogEvent{ | ||
Msg: `finished scheduled compaction`, | ||
Time: time.Date(2021, 10, 19, 14, 53, 5, 815000000, time.UTC), | ||
Level: zapcore.InfoLevel, | ||
Fields: map[string]interface{}{ | ||
"caller": "mvcc/kvstore_compaction.go:57", | ||
"compact-revision": float64(34567), | ||
"took": "21.041639ms", | ||
}, | ||
}, | ||
}, | ||
"cri-logrus": { | ||
l: `{"level":"warning","msg":"cleanup warnings time=\"2021-10-19T14:52:20Z\" level=info msg=\"starting signal loop\" namespace=k8s.io pid=2629\n","time":"2021-10-19T14:52:20.578858689Z"}`, | ||
expected: &runtime.LogEvent{ | ||
Msg: `cleanup warnings time="2021-10-19T14:52:20Z" level=info msg="starting signal loop" namespace=k8s.io pid=2629`, | ||
Time: time.Date(2021, 10, 19, 14, 52, 20, 578858689, time.UTC), | ||
Level: zapcore.WarnLevel, | ||
Fields: map[string]interface{}{}, | ||
}, | ||
}, | ||
} { | ||
name, tc := name, tc | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual := parseLogLine([]byte(tc.l), now) | ||
assert.Equal(t, tc.expected, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters