Skip to content

Commit

Permalink
util/log: share some decoding code
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
knz committed Jun 13, 2023
1 parent d756dec commit f93a33d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
23 changes: 15 additions & 8 deletions pkg/util/log/format_crdb_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
10 changes: 2 additions & 8 deletions pkg/util/log/format_crdb_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f93a33d

Please sign in to comment.