Debian Codegraph @

fluent/proto.go

raw
1 //go:generate msgp
2
3 package fluent
4
5 import (
6 "fmt"
7 "time"
8
9 "github.com/tinylib/msgp/msgp"
10 )
11
12 //msgp:tuple Entry
13 type Entry struct {
14 Time int64 `msg:"time"`
15 Record interface{} `msg:"record"`
16 }
17
18 //msgp:tuple Forward
19 type Forward struct {
20 Tag string `msg:"tag"`
21 Entries []Entry `msg:"entries"`
22 Option map[string]string
23 }
24
25 //msgp:tuple Message
26 type Message struct {
27 Tag string `msg:"tag"`
28 Time int64 `msg:"time"`
29 Record interface{} `msg:"record"`
30 Option map[string]string
31 }
32
33 //msgp:tuple MessageExt
34 type MessageExt struct {
35 Tag string `msg:"tag"`
36 Time EventTime `msg:"time,extension"`
37 Record interface{} `msg:"record"`
38 Option map[string]string
39 }
40
41 type AckResp struct {
42 Ack string `json:"ack" msg:"ack"`
43 }
44
45 // EventTime is an extension to the serialized time value. It builds in support
46 // for sub-second (nanosecond) precision in serialized timestamps.
47 //
48 // You can find the full specification for the msgpack message payload here:
49 // https://github.com/fluent/fluentd/wiki/Forward-Protocol-Specification-v1.
50 //
51 // You can find more information on msgpack extension types here:
52 // https://github.com/tinylib/msgp/wiki/Using-Extensions.
53 type EventTime time.Time
54
55 const (
56 extensionType = 0
57 length = 8
58 )
59
60 func init() {
61 msgp.RegisterExtension(extensionType, func() msgp.Extension { return new(EventTime) })
62 }
63
64 func (t *EventTime) ExtensionType() int8 { return extensionType }
65
66 func (t *EventTime) Len() int { return length }
67
68 func (t *EventTime) MarshalBinaryTo(b []byte) error {
69 // Unwrap to Golang time
70 goTime := time.Time(*t)
71
72 // There's no support for timezones in fluentd's protocol for EventTime.
73 // Convert to UTC.
74 utc := goTime.UTC()
75
76 // Warning! Converting seconds to an int32 is a lossy operation. This code
77 // will hit the "Year 2038" problem.
78 sec := int32(utc.Unix())
79 nsec := utc.Nanosecond()
80
81 // Fill the buffer with 4 bytes for the second component of the timestamp.
82 b[0] = byte(sec >> 24)
83 b[1] = byte(sec >> 16)
84 b[2] = byte(sec >> 8)
85 b[3] = byte(sec)
86
87 // Fill the buffer with 4 bytes for the nanosecond component of the
88 // timestamp.
89 b[4] = byte(nsec >> 24)
90 b[5] = byte(nsec >> 16)
91 b[6] = byte(nsec >> 8)
92 b[7] = byte(nsec)
93
94 return nil
95 }
96
97 // Although decoding messages is not officially supported by this library,
98 // UnmarshalBinary is implemented for testing and general completeness.
99 func (t *EventTime) UnmarshalBinary(b []byte) error {
100 if len(b) != length {
101 return fmt.Errorf("Invalid EventTime byte length: %d", len(b))
102 }
103
104 sec := (int32(b[0]) << 24) | (int32(b[1]) << 16)
105 sec = sec | (int32(b[2]) << 8) | int32(b[3])
106
107 nsec := (int32(b[4]) << 24) | (int32(b[5]) << 16)
108 nsec = nsec | (int32(b[6]) << 8) | int32(b[7])
109
110 *t = EventTime(time.Unix(int64(sec), int64(nsec)))
111 return nil
112 }