diff --git a/cmd/axiom-syslog-proxy/main.go b/cmd/axiom-syslog-proxy/main.go index 4708fd6..0dcc291 100644 --- a/cmd/axiom-syslog-proxy/main.go +++ b/cmd/axiom-syslog-proxy/main.go @@ -24,7 +24,7 @@ func main() { ) } -func run(_ context.Context, _ *zap.Logger, client *axiom.Client) error { +func run(ctx context.Context, _ *zap.Logger, client *axiom.Client) error { flag.Parse() config := &server.Config{ @@ -38,7 +38,7 @@ func run(_ context.Context, _ *zap.Logger, client *axiom.Client) error { return cmd.Error("create server", err) } - srv.Run() + srv.Run(ctx) return nil } diff --git a/go.mod b/go.mod index c73794e..f90c16b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/axiomhq/axiom-syslog-proxy -go 1.22 +go 1.22.2 require ( github.com/axiomhq/axiom-go v0.17.5 diff --git a/parser/parse_test.go b/parser/parse_test.go index 98bfece..a9eebdd 100755 --- a/parser/parse_test.go +++ b/parser/parse_test.go @@ -105,6 +105,24 @@ func (s *ParseTestSuite) TestParseJson() { severity: int64(Trace), metadata: map[string]interface{}{"forwind.favourites.artist": "Rune Clausen", "bool": "true", "forwind.favourites.release.link.type.origin": "home", "forwind.favourites.album": "Blindlight", "forwind.favourites.release.duration": int64(100), "forwind.favourites.release.catno": "fwd09", "forwind.favourites.release.link.url": "http://www.forwind.net"}, }, + { + raw: []byte(fmt.Sprintf("<34>1 %s mymachine.example.com su - ID47 - {\"level\":\"error\",\"service\":\"public-service\",\"env\":\"production\",\"error\":\"fail\",\"time\":\"2024-04-05T05:47:24Z\",\"req_id\":\"req-id\",\"message_inside\":\"this will work due to \\\" quote foobar\"}", nowFormatted)), + time: now, + application: "su", + hostname: "mymachine.example.com", + text: "{\"level\":\"error\",\"service\":\"public-service\",\"env\":\"production\",\"error\":\"fail\",\"time\":\"2024-04-05T05:47:24Z\",\"req_id\":\"req-id\",\"message_inside\":\"this will work due to \\\" quote foobar\"}", + severity: int64(Error), + metadata: map[string]interface{}{"error": "fail", "time": "2024-04-05T05:47:24Z", "req_id": "req-id", "message_inside": "this will work due to \" quote foobar", "service": "public-service", "env": "production"}, + }, + { + raw: []byte(fmt.Sprintf("<34>1 %s mymachine.example.com su - ID47 - {\"level\":\"error\",\"service\":\"public-service\",\"env\":\"production\",\"error\":\"fail\",\"time\":\"2024-04-05T05:47:24Z\",\"req_id\":\"req-id\",\"message\":\"this will work due to \\\" quote foobar\"}", nowFormatted)), + time: now, + application: "su", + hostname: "mymachine.example.com", + text: "this will work due to \" quote foobar", + severity: int64(Error), + metadata: map[string]interface{}{"service": "public-service", "env": "production", "error": "fail", "time": "2024-04-05T05:47:24Z", "req_id": "req-id"}, + }, } for number, c := range cases { diff --git a/parser/syslog.go b/parser/syslog.go index 4ab04da..303a055 100755 --- a/parser/syslog.go +++ b/parser/syslog.go @@ -303,7 +303,7 @@ func parseRFC5424(msg *Log, data []byte, length int) error { if !valid { return errCorruptedData } - msg.Text = cleanString(textData, false) + msg.Text = textData parseMetadata(msg, data[i:]) diff --git a/server/server.go b/server/server.go index 21c9c59..f21099d 100644 --- a/server/server.go +++ b/server/server.go @@ -110,7 +110,7 @@ func (srv *Server) Flush() error { return nil } -func (srv *Server) Run() { +func (srv *Server) Run(ctx context.Context) { if srv.started { log.Print("server already running") return @@ -118,11 +118,13 @@ func (srv *Server) Run() { srv.started = true ticker := time.NewTicker(5 * time.Second) - done := make(chan bool) for { select { - case <-done: + case <-ctx.Done(): + srv.tcpCloser.Close() + srv.udpCloser.Close() + srv.Flush() return case <-ticker.C: srv.Flush()