forked from influxdata/influxdb-comparisons
/
serializer_splunk.go
71 lines (65 loc) · 1.87 KB
/
serializer_splunk.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package common
import (
"bytes"
"fmt"
"io"
)
type SerializerSplunkJson struct {
buf []byte
}
func NewSerializerSplunkJson() *SerializerSplunkJson {
return &SerializerSplunkJson{
buf:make([]byte, 0, 4096),
}
}
// SerializePoint writes Point data to the given writer, conforming to the
// Splunk JSON format.
//
// This function writes output that looks like:
// ...
func (s *SerializerSplunkJson) SerializePoint(w io.Writer, p *Point) (err error) {
timestamp := p.Timestamp.UTC().Unix()
buf := s.buf[:0]
var host []byte
for i := 0; i < len(p.TagKeys); i++ {
if bytes.Compare(p.TagKeys[i], []byte("hostname")) == 0 {
host = p.TagValues[i]
break
}
}
timestampPart := fmt.Sprintf("\"time\":%d,", timestamp)
sourcePart := fmt.Sprintf("\"source\":\"%s\",", p.MeasurementName)
hostPart := fmt.Sprintf("\"host\":\"%s\",", string(host))
for i := 0; i < len(p.FieldKeys); i++ {
buf = append(buf, "{"...)
buf = append(buf, []byte(timestampPart)...)
buf = append(buf, []byte("\"event\":\"metric\",")...)
buf = append(buf, []byte(sourcePart)...)
buf = append(buf, []byte(hostPart)...)
buf = append(buf, []byte("\"fields\":{",)...)
for i := 0; i < len(p.TagKeys); i++ {
buf = append(buf, "\""...)
buf = append(buf, p.TagKeys[i]...)
buf = append(buf, "\":\""...)
buf = append(buf, p.TagValues[i]...)
buf = append(buf, "\","...)
}
buf = append(buf, "\"_value\":"...)
v := p.FieldValues[i]
buf = fastFormatAppend(v, buf, false)
buf = append(buf, ",\"metric_name\":\""...)
buf = append(buf, p.MeasurementName...)
buf = append(buf, "."...)
buf = append(buf, p.FieldKeys[i]...)
buf = append(buf, "\""...)
buf = append(buf, "}}\n"...)
}
_, err = w.Write(buf)
if err != nil {
return err
}
return nil
}
func (s *SerializerSplunkJson) SerializeSize(w io.Writer, points int64, values int64) error {
return serializeSizeInText(w, points, values)
}