2014-03-04 01:52:57 +00:00
|
|
|
package graphite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GraphiteMetric struct {
|
|
|
|
name string
|
|
|
|
isInt bool
|
|
|
|
integerValue int64
|
|
|
|
floatValue float64
|
|
|
|
timestamp int64
|
|
|
|
}
|
|
|
|
|
2014-04-11 12:25:09 +00:00
|
|
|
// returns err == io.EOF when we hit EOF without any further data
|
2014-03-04 01:52:57 +00:00
|
|
|
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 {
|
2014-04-11 12:25:09 +00:00
|
|
|
return fmt.Errorf("connection closed uncleanly/broken: %s\n", err.Error())
|
2014-03-04 01:52:57 +00:00
|
|
|
}
|
2014-04-11 12:25:09 +00:00
|
|
|
if str == "" {
|
|
|
|
return err
|
2014-03-04 01:52:57 +00:00
|
|
|
}
|
2014-04-11 12:25:09 +00:00
|
|
|
// else we got EOF but also data, so just try to process it as valid data
|
2014-03-04 01:52:57 +00:00
|
|
|
}
|
2014-07-11 16:09:22 +00:00
|
|
|
elements := strings.Fields(str)
|
2014-03-04 01:52:57 +00:00
|
|
|
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
|
|
|
|
}
|