Skip to content

feat: Mark truncated log lines with identifier #18262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2025

Conversation

chaudum
Copy link
Contributor

@chaudum chaudum commented Jun 27, 2025

What this PR does / why we need it:

While truncated JSON log lines are usually easy to identify (because the JSON becomes invalid), it is not always the case with other plain text log lines.

This PR adds the possibility to add an identifier as suffix for truncated log lines, such as [...] so that truncated log lines are easier to spot and identify when querying them.


A truncation identifier has almost been implemented with the initial truncation PR (#4051). This implementation is essentially the same.

@@ -3720,6 +3720,10 @@ The `limits_config` block configures global and per-tenant limits in Loki. The v
# CLI flag: -distributor.max-line-size-truncate
[max_line_size_truncate: <boolean> | default = false]

# Identifier that is added at the end of a truncated log line.
# CLI flag: -distributor.max-line-size-truncate-identifier
[max_line_size_truncate_identifier: <string> | default = ""]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternative naming proposal:

Suggested change
[max_line_size_truncate_identifier: <string> | default = ""]
[max_line_size_truncate_suffix: <string> | default = ""]
Copy link
Contributor

github-actions bot commented Jun 27, 2025

💻 Deploy preview deleted.

@pull-request-size pull-request-size bot added size/L and removed size/M labels Jun 27, 2025
This PR adds the possibility to add an identifier as suffix for
truncated log lines, such as `[...]` so that truncated log lines are
easier to spot and identify when querying them.

Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
@chaudum chaudum force-pushed the chaudum/truncation-identifier branch from 52cb8d3 to 866e9b5 Compare June 27, 2025 14:44
@chaudum chaudum marked this pull request as ready for review June 27, 2025 14:44
@chaudum chaudum requested a review from a team as a code owner June 27, 2025 14:44
var truncatedSamples, truncatedBytes int
for i, e := range stream.Entries {
if maxSize := vContext.maxLineSize; maxSize != 0 && len(e.Line) > maxSize {
stream.Entries[i].Line = e.Line[:maxSize]
truncateTo := maxSize - len(suffix)
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add a test? :)

Copy link
Contributor

Choose a reason for hiding this comment

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

given this is hotpath, should we have the suffix logic in its own branch only when len(suffix) > 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ofc!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

continue
}

stream.Entries[i].Line = e.Line[:truncateTo] + suffix
Copy link
Contributor

Choose a reason for hiding this comment

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

i guess this also allocs new slice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it creates a new string. There are benchmarks in the original truncation PR, which show that there is very little overhead, though.

Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
Copy link
Contributor

@ashwanthgoli ashwanthgoli left a comment

Choose a reason for hiding this comment

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

lgtm

@chaudum chaudum merged commit 0ee8e76 into main Jun 30, 2025
67 checks passed
@chaudum chaudum deleted the chaudum/truncation-identifier branch June 30, 2025 06:30
loki-gh-app bot pushed a commit that referenced this pull request Jun 30, 2025
While truncated JSON log lines are usually easy to identify (because the JSON becomes invalid), it is not always the case with other plain text log lines.

This PR adds the possibility to add an identifier as suffix for truncated log lines, such as `[...]` so that truncated log lines are easier to spot and identify when querying them.

Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
(cherry picked from commit 0ee8e76)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment