forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
msgpack.go
52 lines (44 loc) · 1.29 KB
/
msgpack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package msgpack
import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/serializers"
)
// Serializer encodes metrics in MessagePack format
type Serializer struct{}
func marshalMetric(buf []byte, metric telegraf.Metric) ([]byte, error) {
return (&Metric{
Name: metric.Name(),
Time: MessagePackTime{time: metric.Time()},
Tags: metric.Tags(),
Fields: metric.Fields(),
}).MarshalMsg(buf)
}
// Serialize implements serializers.Serializer.Serialize
// github.com/influxdata/telegraf/plugins/serializers/Serializer
func (s *Serializer) Serialize(metric telegraf.Metric) ([]byte, error) {
return marshalMetric(nil, metric)
}
// SerializeBatch implements serializers.Serializer.SerializeBatch
// github.com/influxdata/telegraf/plugins/serializers/Serializer
func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
buf := make([]byte, 0)
for _, m := range metrics {
var err error
buf, err = marshalMetric(buf, m)
if err != nil {
return nil, err
}
}
return buf, nil
}
func init() {
serializers.Add("msgpack",
func() serializers.Serializer {
return &Serializer{}
},
)
}
// InitFromConfig is a compatibility function to construct the parser the old way
func (s *Serializer) InitFromConfig(_ *serializers.Config) error {
return nil
}