-
Notifications
You must be signed in to change notification settings - Fork 32
/
measure.go
59 lines (49 loc) · 1.18 KB
/
measure.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
package influxdb
import (
"strconv"
"time"
"github.com/segmentio/stats"
)
// AppendMeasure is a formatting routine to append the InflxDB line protocol
// representation of a measure to a memory buffer.
func AppendMeasure(b []byte, t time.Time, m stats.Measure) []byte {
b = append(b, m.Name...)
for _, tag := range m.Tags {
b = append(b, ',')
b = append(b, tag.Name...)
b = append(b, '=')
b = append(b, tag.Value...)
}
for i, field := range m.Fields {
if len(field.Name) == 0 {
field.Name = "value"
}
if i == 0 {
b = append(b, ' ')
} else {
b = append(b, ',')
}
b = append(b, field.Name...)
b = append(b, '=')
switch v := field.Value; v.Type() {
case stats.Null:
case stats.Bool:
if v.Bool() {
b = append(b, "true"...)
} else {
b = append(b, "false"...)
}
case stats.Int:
b = strconv.AppendInt(b, v.Int(), 10)
case stats.Uint:
b = strconv.AppendUint(b, v.Uint(), 10)
case stats.Float:
b = strconv.AppendFloat(b, v.Float(), 'g', -1, 64)
case stats.Duration:
b = strconv.AppendFloat(b, v.Duration().Seconds(), 'g', -1, 64)
}
}
b = append(b, ' ')
b = strconv.AppendInt(b, t.UnixNano(), 10)
return append(b, '\n')
}