Skip to content

Commit a52c314

Browse files
committed
Include milliseconds in event timestamps
1 parent 094ff9e commit a52c314

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ func main() {
4040
"{your-index}"
4141
)
4242

43-
// Use the client to send a log with time auto-generated by go host [ time.Now().Unix() ]
43+
// Use the client to send a log with the go host's current time
4444
err := splunk.Log(
4545
interface{"msg": "send key/val pairs or json objects here", "msg2": "anything that is useful to you in the log event"}
4646
)
4747
if err != nil {
4848
return err
4949
}
5050

51-
// Use the client to send a log with time passed by source
51+
// Use the client to send a log with a provided timestamp
5252
err = splunk.LogWithTime(
53-
1514764800,
53+
time.Now(),
5454
interface{"msg": "send key/val pairs or json objects here", "msg2": "anything that is useful to you in the log event"}
5555
)
5656
if err != nil {

splunk/splunk.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"encoding/json"
77
"errors"
8+
"fmt"
89
"io"
910
"io/ioutil"
1011
"net/http"
@@ -14,14 +15,24 @@ import (
1415

1516
// Event represents the log event object that is sent to Splunk when Client.Log is called.
1617
type Event struct {
17-
Time int64 `json:"time"` // epoch time in seconds
18+
Time EventTime `json:"time"` // when the event happened
1819
Host string `json:"host"` // hostname
1920
Source string `json:"source,omitempty"` // optional description of the source of the event; typically the app's name
2021
SourceType string `json:"sourcetype,omitempty"` // optional name of a Splunk parsing configuration; this is usually inferred by Splunk
2122
Index string `json:"index,omitempty"` // optional name of the Splunk index to store the event in; not required if the token has a default index set in Splunk
2223
Event interface{} `json:"event"` // throw any useful key/val pairs here
2324
}
2425

26+
// EventTime marshals timestamps using the Splunk HTTP Event Collector's default format.
27+
type EventTime struct {
28+
time.Time
29+
}
30+
31+
func (t EventTime) MarshalJSON() ([]byte, error) {
32+
// The milliseconds are truncated, not rounded to nearest; eg. 12:00:00.5008274 will be logged as 12:00:00.500.
33+
return []byte(fmt.Sprintf("%d.%d", t.Unix(), t.Nanosecond()/1e6)), nil
34+
}
35+
2536
// Client manages communication with Splunk's HTTP Event Collector.
2637
// New client objects should be created using the NewClient function.
2738
//
@@ -66,7 +77,7 @@ func NewClient(httpClient *http.Client, URL string, Token string, Source string,
6677
// This method takes the current timestamp for the event, meaning that the event is generated at runtime.
6778
func (c *Client) NewEvent(event interface{}, source string, sourcetype string, index string) *Event {
6879
e := &Event{
69-
Time: time.Now().Unix(),
80+
Time: EventTime{time.Now()},
7081
Host: c.Hostname,
7182
Source: source,
7283
SourceType: sourcetype,
@@ -79,9 +90,9 @@ func (c *Client) NewEvent(event interface{}, source string, sourcetype string, i
7990
// NewEventWithTime creates a new log event with a specified timetamp to send to Splunk.
8091
// This is similar to NewEvent but if you want to log in a different time rather than time.Now this becomes handy. If that's
8192
// the case, use this function to create the Event object and the the LogEvent function.
82-
func (c *Client) NewEventWithTime(t int64, event interface{}, source string, sourcetype string, index string) *Event {
93+
func (c *Client) NewEventWithTime(t time.Time, event interface{}, source string, sourcetype string, index string) *Event {
8394
e := &Event{
84-
Time: t,
95+
Time: EventTime{time.Now()},
8596
Host: c.Hostname,
8697
Source: source,
8798
SourceType: sourcetype,
@@ -102,10 +113,10 @@ func (c *Client) Log(event interface{}) error {
102113
return c.LogEvent(log)
103114
}
104115

105-
// Client.LogWithTime is used to construct a new log event with a scpecified timestamp and POST it to the Splunk server.
116+
// Client.LogWithTime is used to construct a new log event with a specified timestamp and POST it to the Splunk server.
106117
//
107118
// This is similar to Client.Log, just with the t parameter.
108-
func (c *Client) LogWithTime(t int64, event interface{}) error {
119+
func (c *Client) LogWithTime(t time.Time, event interface{}) error {
109120
// create Splunk log
110121
log := c.NewEventWithTime(t, event, c.Source, c.SourceType, c.Index)
111122
return c.LogEvent(log)

splunk/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (w *Writer) send(messages []*message, retries int) {
103103
events := make([]*Event, len(messages))
104104
for i, m := range messages {
105105
// Use the configuration of the Client for the event
106-
events[i] = w.Client.NewEventWithTime(m.writtenAt.Unix(), m.data, w.Client.Source, w.Client.SourceType, w.Client.Index)
106+
events[i] = w.Client.NewEventWithTime(m.writtenAt, m.data, w.Client.Source, w.Client.SourceType, w.Client.Index)
107107
}
108108
// Send the events to splunk
109109
err := w.Client.LogEvents(events)

0 commit comments

Comments
 (0)