Created
August 12, 2014 00:19
-
-
Save gorsuch/4e6aab9b216d3db42e13 to your computer and use it in GitHub Desktop.
parsing graphite with go
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 ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
type GraphiteResult struct { | |
Target string `json:"target"` | |
Datapoints [][]interface{} `json:"datapoints"` | |
} | |
type Datapoint struct { | |
Epoch int | |
Value float64 | |
} | |
type TranslatedResult struct { | |
Target string | |
Datapoints []Datapoint | |
} | |
func main() { | |
res, err := http.Get("http://graphite:8000/render/?target=some_metric&format=json&from=-5min") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer res.Body.Close() | |
body, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var data []GraphiteResult | |
err = json.Unmarshal(body, &data) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, r := range data { | |
newResult := TranslatedResult{Target: r.Target, Datapoints: make([]Datapoint, 0)} | |
for _, d := range r.Datapoints { | |
p := Datapoint{} | |
p.Epoch = int(d[1].(float64)) | |
// this could be nil! | |
p.Value = d[0].(float64) | |
newResult.Datapoints = append(newResult.Datapoints, p) | |
} | |
fmt.Println(newResult) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment