Skip to content

Commit 63d4350

Browse files
authored
victorialogs: drop the trailing line terminator from messages (#4831)
1 parent 52184e1 commit 63d4350

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎pkg/acquisition/modules/victorialogs/run.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package victorialogs
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"github.com/prometheus/client_golang/prometheus"
89
"gopkg.in/tomb.v2"
@@ -48,7 +49,9 @@ func (s *Source) OneShotAcquisition(ctx context.Context, out chan pipeline.Event
4849

4950
func (s *Source) readOneEntry(ctx context.Context, entry *vlclient.Log, labels map[string]string, out chan pipeline.Event) {
5051
ll := pipeline.Line{}
51-
ll.Raw = entry.Message
52+
// Messages can keep the newline of the original log line (e.g. podman + journald); the other
53+
// line-based datasources strip it, and parsers anchored with `$` fail if it's left in.
54+
ll.Raw = trimLineTerminator(entry.Message)
5255
ll.Time = entry.Time
5356
ll.Src = s.Config.URL
5457
ll.Labels = labels
@@ -96,3 +99,12 @@ func (s *Source) Stream(ctx context.Context, out chan pipeline.Event) error {
9699

97100
return <-errChan
98101
}
102+
103+
// trimLineTerminator removes a single trailing "\n" or "\r\n".
104+
func trimLineTerminator(msg string) string {
105+
if trimmed, ok := strings.CutSuffix(msg, "\r\n"); ok {
106+
return trimmed
107+
}
108+
109+
return strings.TrimSuffix(msg, "\n")
110+
}

‎pkg/acquisition/modules/victorialogs/victorialogs_test.go‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package victorialogs_test
33
import (
44
"bytes"
55
"context"
6+
"encoding/json"
67
"fmt"
78
"io"
89
"math/rand"
@@ -437,6 +438,71 @@ func TestStreamReturnsWhenServerCloses(t *testing.T) {
437438
require.NoError(t, <-streamErr)
438439
}
439440

441+
func TestStreamStripsTrailingNewline(t *testing.T) {
442+
// VictoriaLogs (e.g. via journald) may store messages with a trailing line terminator;
443+
// the datasource must strip at most one trailing \n or \r\n to match bufio.ScanLines.
444+
tests := []struct {
445+
name string
446+
msg string
447+
want string
448+
}{
449+
{
450+
name: "no trailing newline",
451+
msg: "line 1",
452+
want: "line 1",
453+
},
454+
{
455+
name: "trailing LF",
456+
msg: "line 1\n",
457+
want: "line 1",
458+
},
459+
{
460+
name: "trailing CRLF",
461+
msg: "line 1\r\n",
462+
want: "line 1",
463+
},
464+
{
465+
name: "multiple trailing LFs drops only one",
466+
msg: "line 1\n\n",
467+
want: "line 1\n",
468+
},
469+
{
470+
name: "internal newline and trailing space preserved",
471+
msg: "line 1\nline 2 \n",
472+
want: "line 1\nline 2 ",
473+
},
474+
}
475+
476+
for _, tc := range tests {
477+
t.Run(tc.name, func(t *testing.T) {
478+
ctx := t.Context()
479+
480+
payload, err := json.Marshal(map[string]string{
481+
"_msg": tc.msg,
482+
"_time": "2026-11-12T07:19:02Z",
483+
})
484+
require.NoError(t, err)
485+
486+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
487+
fmt.Fprintf(w, "%s\n", payload)
488+
}))
489+
defer srv.Close()
490+
491+
vlSource := victorialogs.Source{}
492+
config := fmt.Sprintf("mode: tail\nsource: victorialogs\nurl: %s\nquery: '*'\n", srv.URL)
493+
err = vlSource.Configure(ctx, []byte(config), log.WithField("type", victorialogs.ModuleName), metrics.AcquisitionMetricsLevelNone)
494+
require.NoError(t, err)
495+
496+
out := make(chan pipeline.Event, 10)
497+
498+
err = vlSource.Stream(ctx, out)
499+
require.ErrorContains(t, err, "tail stream closed by server")
500+
require.Len(t, out, 1)
501+
require.Equal(t, tc.want, (<-out).Line.Raw)
502+
})
503+
}
504+
}
505+
440506
func TestTailModeIgnoredOptionsWarning(t *testing.T) {
441507
tests := []struct {
442508
name string

0 commit comments

Comments
 (0)