Skip to content

Instantly share code, notes, and snippets.

@ryansmith3136
Created January 16, 2013 01:46
Show Gist options
  • Select an option

  • Save ryansmith3136/4543915 to your computer and use it in GitHub Desktop.

Select an option

Save ryansmith3136/4543915 to your computer and use it in GitHub Desktop.
l2met Go client
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"sync"
"time"
"io/ioutil"
)
func main() {
ids, err := getIds()
if err != nil {
fmt.Println("unable to get ids")
return
}
var wg sync.WaitGroup
for i := range ids {
wg.Add(1)
go getMetric(&wg, ids[i])
}
wg.Wait()
}
func getIds() ([]int64, error) {
u := "https://l2met-next.herokuapp.com/query?limit=10"
resp, err := http.Get(u)
if err != nil {
fmt.Printf("http-error: %s\n", err)
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("read-error: %s\n", err)
return nil, err
}
var vals []int64
err = json.Unmarshal(b, &vals)
if err != nil {
fmt.Printf("json-error: %s\n", err)
return nil, err
}
return vals, nil
}
type M struct {
Id int64 `json:"id"`
Time time.Time `json:"time"`
Name string `json:"name"`
Vals []float64 `json:"vals"`
}
func getMetric(wg *sync.WaitGroup, id int64) {
defer wg.Done()
idstr := strconv.FormatInt(id, 10)
u := "https://l2met-next.herokuapp.com/buckets?id=" + idstr
resp, err := http.Get(u)
if err != nil {
fmt.Printf("http-error: %s\n", err)
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("read-error: %s\n", err)
return
}
m := new(M)
err = json.Unmarshal(b, &m)
if err != nil {
fmt.Printf("json-error: %s\n", err)
return
}
fmt.Printf("metric=%s length=%d\n", m.Name, len(m.Vals))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment