@@ -3,6 +3,7 @@ package victorialogs_test
33import (
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\n line 2 \n " ,
472+ want : "line 1\n line 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\n source: victorialogs\n url: %s\n query: '*'\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+
440506func TestTailModeIgnoredOptionsWarning (t * testing.T ) {
441507 tests := []struct {
442508 name string
0 commit comments