Skip to content

Keep .Message raw with --timestamps; expose timestamp as .Timestamp - #372

Merged
tksm merged 1 commit into
stern:masterfrom
s3onghyun:342-timestamps-message
Jun 18, 2026
Merged

tksm merged 1 commit into
stern:masterfrom
s3onghyun:342-timestamps-message

Conversation

@s3onghyun

Copy link
Copy Markdown
Contributor

Fixes #342.

Problem

With --timestamps, stern prepends the timestamp onto the log content that becomes .Message. So template helpers that parse .Message as JSON — extractJSONParts, parseJSON, tryParseJSON, prettyJSON, extjson — receive 2023-... {"...":...} instead of {"...":...} and fail with invalid character '<digit>' after top-level value (exactly the error in #342). The README documents .Message as "the log message itself", so this is a contract violation, and there was no separate field to get the timestamp from.

Fix

Keep .Message raw and expose the timestamp as a new .Timestamp template field:

  • add Timestamp to the Log struct; consumeLine no longer mutates the content, it passes the timestamp through to sprint.
  • the built-in default and raw templates render {{if .Timestamp}}{{.Timestamp}} {{end}}{{.Message}} — visible output is unchanged when --timestamps is on, and byte-identical when off (empty field).
  • documented .Timestamp in the README.

Testing

Added TestConsumeStreamTailTimestamp: with --timestamps, {{.Message}} stays raw JSON and {{.Timestamp}} carries the timestamp. Fails before the fix (.Message polluted; .Timestamp undefined), passes after. go test ./... green.

@tksm tksm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🥇 Could you rebase onto the latest master?

With --timestamps, the timestamp was prepended onto the log content that
becomes .Message, so template helpers that parse .Message as JSON
(extractJSONParts, parseJSON, prettyJSON, extjson) failed with
'invalid character ... after top-level value'.

Keep .Message as the raw log line and expose the timestamp as a new
.Timestamp template field. The built-in default/raw templates render
'{{if .Timestamp}}{{.Timestamp}} {{end}}{{.Message}}', so visible output is
unchanged when --timestamps is on and byte-identical when off.

Closes #342

Signed-off-by: Seonghyun Hong <[email protected]>
@s3onghyun
s3onghyun force-pushed the 342-timestamps-message branch from 89dc474 to 05813e4 Compare June 18, 2026 13:50
@s3onghyun

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Rebased onto the latest master and force-pushed. Tests still pass (go test ./...).

@tksm
tksm merged commit 8a00e61 into stern:master Jun 18, 2026
2 checks passed
@tksm

tksm commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

extractJSONParts in template fails when using --timestamps CLI arg

2 participants