forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphite_metric.go
51 lines (48 loc) · 1.15 KB
/
graphite_metric.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
package graphite
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
)
type GraphiteMetric struct {
name string
isInt bool
integerValue int64
floatValue float64
timestamp int64
}
// returns err == io.EOF when we hit EOF without any further data
func (self *GraphiteMetric) Read(reader *bufio.Reader) error {
buf, err := reader.ReadBytes('\n')
str := strings.TrimSpace(string(buf))
if err != nil {
if err != io.EOF {
return fmt.Errorf("connection closed uncleanly/broken: %s\n", err.Error())
}
if str == "" {
return err
}
// else we got EOF but also data, so just try to process it as valid data
}
elements := strings.Fields(str)
if len(elements) != 3 {
return fmt.Errorf("Received '%s' which doesn't have three fields", str)
}
self.name = elements[0]
self.floatValue, err = strconv.ParseFloat(elements[1], 64)
if err != nil {
return err
}
if i := int64(self.floatValue); float64(i) == self.floatValue {
self.isInt = true
self.integerValue = int64(self.floatValue)
}
timestamp, err := strconv.ParseUint(elements[2], 10, 32)
if err != nil {
return err
}
self.timestamp = int64(timestamp * 1000000)
return nil
}