Skip to content

Commit

Permalink
retain original message
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Nov 22, 2024
1 parent e9deed2 commit 0c37c43
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions systemapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):])
}
}

Expand Down
10 changes: 5 additions & 5 deletions systemapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

0 comments on commit 0c37c43

Please sign in to comment.