Created
July 10, 2014 20:35
-
-
Save fridgei/82d67bc5b19b499ca0c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"bytes" | |
) | |
const ( | |
FLT = iota | |
INT = iota | |
) | |
type Metric struct { | |
path string | |
timestamp int | |
value interface{} | |
value_type int | |
} | |
type Metrics []Metric | |
func (self *Metric) _pickle(idx int) string { | |
var val string | |
var proto_header string | |
if self.value_type == INT { | |
val = fmt.Sprintf("I%d", self.value.(int)) | |
} else { | |
val = fmt.Sprintf("F%f", self.value.(float64)) | |
} | |
if idx == 0 { | |
proto_header = "(lp0\n" | |
} | |
return fmt.Sprintf( | |
"%s(S'%s'\np%d\n(I%d\n%s\ntp%d\ntp%d\na", | |
proto_header, | |
self.path, | |
(idx * 3) + 1, // first nested paren | |
self.timestamp,val, | |
(idx * 3) + 2, | |
(idx * 3) + 3, | |
) | |
} | |
func (self *Metric) Pickle() string { | |
return fmt.Sprintf("%s.", self._pickle(0)) | |
} | |
func (self *Metrics) Pickle() string { | |
var buffer bytes.Buffer | |
for idx, m := range *self { | |
buffer.WriteString(m._pickle(idx)) | |
} | |
buffer.WriteString(".") | |
return buffer.String() | |
} | |
func main() { | |
m1 := Metric { | |
path: "server.foo.bar", | |
timestamp: 1111111111, | |
value: 2343.232, | |
value_type: FLT, | |
} | |
m2 := Metric { | |
path: "server.foo.bar", | |
timestamp: 1111111111, | |
value: 2343, | |
value_type: INT, | |
} | |
fmt.Println(m1.Pickle()) | |
fmt.Println(m2.Pickle()) | |
ms := Metrics {m1 , m2 } | |
fmt.Println(ms.Pickle()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment