22
33package fluent
44
5+ import (
6+ "time"
7+
8+ "github.com/tinylib/msgp/msgp"
9+ )
10+
511//msgp:tuple Entry
612type Entry struct {
713 Time int64 `msg:"time"`
@@ -22,3 +28,69 @@ type Message struct {
2228 Record interface {} `msg:"record"`
2329 Option interface {} `msg:"option"`
2430}
31+
32+ //msgp:tuple MessageExt
33+ type MessageExt struct {
34+ Tag string `msg:"tag"`
35+ Time EventTime `msg:"time,extension"`
36+ Record interface {} `msg:"record"`
37+ Option interface {} `msg:"option"`
38+ }
39+
40+ // EventTime is an extension to the serialized time value. It builds in support
41+ // for sub-second (nanosecond) precision in serialized timestamps.
42+ //
43+ // You can find the full specification for the msgpack message payload here:
44+ // https://github.com/fluent/fluentd/wiki/Forward-Protocol-Specification-v1.
45+ //
46+ // You can find more information on msgpack extension types here:
47+ // https://github.com/tinylib/msgp/wiki/Using-Extensions.
48+ type EventTime time.Time
49+
50+ const (
51+ extensionType = 0
52+ length = 8
53+ )
54+
55+ func init () {
56+ msgp .RegisterExtension (extensionType , func () msgp.Extension { return new (EventTime ) })
57+ }
58+
59+ func (t * EventTime ) ExtensionType () int8 { return extensionType }
60+
61+ func (t * EventTime ) Len () int { return length }
62+
63+ func (t * EventTime ) MarshalBinaryTo (b []byte ) error {
64+ // Unwrap to Golang time
65+ goTime := time .Time (* t )
66+
67+ // There's no support for timezones in fluentd's protocol for EventTime.
68+ // Convert to UTC.
69+ utc := goTime .UTC ()
70+
71+ // Warning! Converting seconds to an int32 is a lossy operation. This code
72+ // will hit the "Year 2038" problem.
73+ sec := int32 (utc .Unix ())
74+ nsec := utc .Nanosecond ()
75+
76+ // Fill the buffer with 4 bytes for the second component of the timestamp.
77+ b [0 ] = byte (sec >> 24 )
78+ b [1 ] = byte (sec >> 16 )
79+ b [2 ] = byte (sec >> 8 )
80+ b [3 ] = byte (sec )
81+
82+ // Fill the buffer with 4 bytes for the nanosecond component of the
83+ // timestamp.
84+ b [4 ] = byte (nsec >> 24 )
85+ b [5 ] = byte (nsec >> 16 )
86+ b [6 ] = byte (nsec >> 8 )
87+ b [7 ] = byte (nsec )
88+
89+ return nil
90+ }
91+
92+ // UnmarshalBinary is not implemented since decoding messages is not supported
93+ // by this library.
94+ func (t * EventTime ) UnmarshalBinary (b []byte ) error {
95+ return nil
96+ }
0 commit comments