Skip to content

Commit

Permalink
improvement: Adds timestamp to messages
Browse files Browse the repository at this point in the history
Includes a timestamp for each message/line that flows through the client.

Allows the user to provide their own timestamp through Options- and if the timestamp is not provided- the value is defaulted.

Semver: Patch
Ref: LOG-7156
  • Loading branch information
dm36 committed Sep 14, 2020
1 parent aa78e96 commit 7f8ffc4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
11 changes: 6 additions & 5 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ type Payload struct {

// Line contains properties related to an individual log message.
type Line struct {
App string `json:"app,omitempty"`
Body string `json:"line,omitempty"`
Level string `json:"level,omitempty"`
Env string `json:"env,omitempty"`
Meta metaEnvelope `json:"meta,omitempty"`
Body string `json:"line"`
Timestamp int64 `json:"timestamp"`
App string `json:"app,omitempty"`
Level string `json:"level,omitempty"`
Env string `json:"env,omitempty"`
Meta metaEnvelope `json:"meta,omitempty"`
}

type ingestAPIResponse struct {
Expand Down
8 changes: 8 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestLogger_Log(t *testing.T) {
assert.Equal(t, "testing", line["line"])
assert.Equal(t, "info", line["level"])
assert.Equal(t, "test", line["app"])
assert.NotEmpty(t, line["timestamp"])

assert.Equal(t, "application/json", head["Content-Type"][0])
assert.Equal(t, "abc123", head["Apikey"][0])
Expand All @@ -95,11 +96,14 @@ func TestLogger_LogWithOptions(t *testing.T) {
}))
defer ts.Close()

now := time.Now()

o := Options{
IngestURL: ts.URL,
App: "app",
Env: "development",
Level: "info",
Timestamp: now,
}

l, err := NewLogger(o, "abc123")
Expand All @@ -121,6 +125,7 @@ func TestLogger_LogWithOptions(t *testing.T) {
assert.Equal(t, "anotherapp", line["app"])
assert.Equal(t, "production", line["env"])
assert.Equal(t, "error", line["level"])
assert.Equal(t, now.UnixNano()/int64(time.Millisecond), int64(line["timestamp"].(float64)))
})

t.Run("Invalid options", func(t *testing.T) {
Expand Down Expand Up @@ -167,6 +172,7 @@ func TestLogger_LogWithLevel(t *testing.T) {
line := ls[0].(map[string]interface{})
assert.Equal(t, "testing", line["line"])
assert.Equal(t, "error", line["level"])
assert.NotEmpty(t, line["timestamp"])
}

func TestLogger_LogWithMeta(t *testing.T) {
Expand Down Expand Up @@ -196,6 +202,7 @@ func TestLogger_LogWithMeta(t *testing.T) {
ls := body["lines"].([]interface{})
line := ls[0].(map[string]interface{})
assert.Equal(t, meta, line["meta"])
assert.NotEmpty(t, line["timestamp"])
}

func TestLogger_LogWithMetaIndexed(t *testing.T) {
Expand Down Expand Up @@ -224,6 +231,7 @@ func TestLogger_LogWithMetaIndexed(t *testing.T) {
ls := body["lines"].([]interface{})
line := ls[0].(map[string]interface{})
assert.NotEmpty(t, line["meta"])
assert.NotEmpty(t, line["timestamp"])

meta := line["meta"].(map[string](interface{}))
assert.Equal(t, "value", meta["key"])
Expand Down
7 changes: 4 additions & 3 deletions logger/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Options struct {
MaxBufferLen int
Meta string
Tags string
Timestamp time.Time
}

func (e InvalidOptionMessage) String() string {
Expand Down Expand Up @@ -104,10 +105,10 @@ func (options *Options) setDefaults() {
if options.FlushInterval == 0 {
options.FlushInterval = defaultFlushInterval
}
if options.MaxBufferLen == 0 {
options.MaxBufferLen = defaultMaxBufferLen
}
if options.IngestURL == "" {
options.IngestURL = defaultIngestURL
}
if options.MaxBufferLen == 0 {
options.MaxBufferLen = defaultMaxBufferLen
}
}
6 changes: 6 additions & 0 deletions logger/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func (t *transport) send(msgs []Message) error {
Level: msg.Options.Level,
}

timestamp := msg.Options.Timestamp
if timestamp.IsZero() {
timestamp = time.Now()
}
line.Timestamp = timestamp.UnixNano() / int64(time.Millisecond)

if msg.Options.Meta != "" {
line.Meta = metaEnvelope{
indexed: msg.Options.IndexMeta,
Expand Down

0 comments on commit 7f8ffc4

Please sign in to comment.