From 0c37c430eab10934fcf787139b99a8ab3c2231f8 Mon Sep 17 00:00:00 2001 From: Chris Hager Date: Fri, 22 Nov 2024 14:51:31 +0100 Subject: [PATCH] retain original message --- README.md | 4 ++-- systemapi/server.go | 2 -- systemapi/server_test.go | 10 +++++----- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3ac1f16..70d3ab3 100644 --- a/README.md +++ b/README.md @@ -80,8 +80,8 @@ $ echo "1634966400 this is a test" > pipe.fifo # Query events $ curl localhost:3535/logs -2024-11-21T19:48:04Z hello world -2021-10-23T05:20:00Z this is a test <--- custom timestamp on this entry +2024-11-22T13:50:09Z hello world +2021-10-23T05:20:00Z 1634966400 this is a test <--- custom timestamp on this entry ``` ## Actions diff --git a/systemapi/server.go b/systemapi/server.go index 0230211..bce90d4 100644 --- a/systemapi/server.go +++ b/systemapi/server.go @@ -222,11 +222,9 @@ func (s *Server) addEvent(event Event) { if len(timestampStr) == 10 { // timestamp in seconds, update event event.ReceivedAt = time.Unix(timeInt, 0).UTC() - event.Message = strings.TrimSpace(event.Message[len(timestampStr):]) } else if len(timestampStr) == 13 { // timestamp in milliseconds, update event event.ReceivedAt = time.UnixMilli(timeInt).UTC() - event.Message = strings.TrimSpace(event.Message[len(timestampStr):]) } } diff --git a/systemapi/server_test.go b/systemapi/server_test.go index b0c2782..a15bb82 100644 --- a/systemapi/server_test.go +++ b/systemapi/server_test.go @@ -188,9 +188,9 @@ func TestAddEntryMessageParsing(t *testing.T) { testTime2TimestampMs := testTime2.UnixMilli() // Add messages - srv.addEvent(Event{ReceivedAt: testTime1, Message: "1"}) // regular message - srv.addEvent(Event{ReceivedAt: testTime1, Message: fmt.Sprintf("%d 2", testTime2TimestampSec)}) // custom timestamp - srv.addEvent(Event{ReceivedAt: testTime1, Message: fmt.Sprintf("%d \t 3 \t ", testTime2TimestampMs)}) // custom timestamp, with whitespace to test trimming + srv.addEvent(Event{ReceivedAt: testTime1, Message: "1"}) // regular message + srv.addEvent(Event{ReceivedAt: testTime1, Message: fmt.Sprintf("%d 2", testTime2TimestampSec)}) // custom timestamp + srv.addEvent(Event{ReceivedAt: testTime1, Message: fmt.Sprintf(" %d \t 3 \t ", testTime2TimestampMs)}) // custom timestamp, with whitespace to test trimming // Add empty messages to ensure they are ignored srv.addEvent(Event{ReceivedAt: testTime1, Message: ""}) // empty message @@ -204,10 +204,10 @@ func TestAddEntryMessageParsing(t *testing.T) { require.Equal(t, testTime1, srv.events[0].ReceivedAt) // Check entry 2 (timestamp in seconds) - require.Equal(t, "2", srv.events[1].Message) + require.Equal(t, fmt.Sprintf("%d 2", testTime2TimestampSec), srv.events[1].Message) require.Equal(t, testTime2, srv.events[1].ReceivedAt) // Check entry 3 (timestamp in milliseconds) - require.Equal(t, "3", srv.events[2].Message) // check that whitespace was trimmed + require.Equal(t, fmt.Sprintf("%d \t 3", testTime2TimestampMs), srv.events[2].Message) // check that whitespace was trimmed require.Equal(t, testTime2, srv.events[2].ReceivedAt) }