From f93a33d29af1f64c271d63ce1b3fcdf6503fb571 Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Tue, 13 Jun 2023 12:03:04 +0200 Subject: [PATCH] util/log: share some decoding code Release note: None --- pkg/util/log/format_crdb_v1.go | 23 +++++++++++++++-------- pkg/util/log/format_crdb_v2.go | 10 ++-------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/pkg/util/log/format_crdb_v1.go b/pkg/util/log/format_crdb_v1.go index b0713df5cb21..57946ff47097 100644 --- a/pkg/util/log/format_crdb_v1.go +++ b/pkg/util/log/format_crdb_v1.go @@ -389,6 +389,19 @@ type entryDecoderV1 struct { truncatedLastEntry bool } +func decodeTimestamp(fragment []byte) (unixNano int64, err error) { + timeFormat := MessageTimeFormat + if len(fragment) > 7 && (fragment[len(fragment)-7] == '+' || fragment[len(fragment)-7] == '-') { + // The timestamp has a timezone offset. + timeFormat = MessageTimeFormatWithTZ + } + t, err := time.Parse(timeFormat, string(fragment)) + if err != nil { + return 0, err + } + return t.UnixNano(), nil +} + // Decode decodes the next log entry into the provided protobuf message. func (d *entryDecoderV1) Decode(entry *logpb.Entry) error { for { @@ -411,17 +424,11 @@ func (d *entryDecoderV1) Decode(entry *logpb.Entry) error { entry.Severity = Severity(strings.IndexByte(severityChar, m[1][0]) + 1) // Process the timestamp. - ts := string(m[2]) - timeFormat := MessageTimeFormat - if len(ts) > 7 && (ts[len(ts)-7] == '+' || ts[len(ts)-7] == '-') { - // The timestamp has a timezone offset. - timeFormat = MessageTimeFormatWithTZ - } - t, err := time.Parse(timeFormat, ts) + var err error + entry.Time, err = decodeTimestamp(m[2]) if err != nil { return err } - entry.Time = t.UnixNano() // Process the goroutine ID. if len(m[3]) > 0 { diff --git a/pkg/util/log/format_crdb_v2.go b/pkg/util/log/format_crdb_v2.go index 84b80d3e2b0e..25a21d2336ad 100644 --- a/pkg/util/log/format_crdb_v2.go +++ b/pkg/util/log/format_crdb_v2.go @@ -745,17 +745,11 @@ func (f entryDecoderV2Fragment) getGoroutine() int64 { } func (f entryDecoderV2Fragment) getTimestamp() (unixNano int64) { - ts := string(f[v2DateTimeIdx]) - timeFormat := MessageTimeFormat - if len(ts) > 7 && (ts[len(ts)-7] == '+' || ts[len(ts)-7] == '-') { - // The timestamp has a timezone offset. - timeFormat = MessageTimeFormatWithTZ - } - t, err := time.Parse(timeFormat, ts) + t, err := decodeTimestamp(f[v2DateTimeIdx]) if err != nil { panic(err) } - return t.UnixNano() + return t } func (f entryDecoderV2Fragment) getChannel() logpb.Channel {