Created
January 22, 2017 09:31
-
-
Save cabrinha/016331a0ff896deb1221c17cbcd503d4 to your computer and use it in GitHub Desktop.
Query a URL and write the response into a redis database...
This file contains hidden or 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 ( | |
| "flag" | |
| "fmt" | |
| "io/ioutil" | |
| "log" | |
| "net/http" | |
| "time" | |
| "github.com/garyburd/redigo/redis" | |
| ) | |
| func newPool() *redis.Pool { | |
| return &redis.Pool{ | |
| MaxIdle: 40, | |
| MaxActive: 12000, | |
| Dial: func() (redis.Conn, error) { | |
| c, err := redis.Dial("tcp", "my_redis_host.tld:6379") | |
| if err != nil { | |
| panic(err) | |
| } | |
| return c, err | |
| }, | |
| } | |
| } | |
| var pool = newPool() | |
| type client struct { | |
| httpClient *http.Client | |
| } | |
| func (c *client) HelpfulGet(apiURL string) (*http.Response, error) { | |
| return c.httpClient.Get(apiURL) | |
| } | |
| func NewClient(hc *http.Client) *client { | |
| return &client{ | |
| httpClient: hc, | |
| } | |
| } | |
| func main() { | |
| host := flag.String("H", "hostname", "hostname of the ostrich") | |
| port := flag.Int("P", 9999, "port that ostrich is listening on") | |
| json := flag.String("j", "/stats.json", "path of the ostrich url, usually: stats.json") | |
| timeout := flag.Int("T", 10, "seconds to wait for ostrich metrics to return") | |
| flag.Parse() | |
| apiURL := fmt.Sprintf("http://%s:%d%s?period=60", *host, *port, *json) | |
| hc := &http.Client{Timeout: time.Duration(*timeout) * time.Second} | |
| c := NewClient(hc) | |
| r, err := c.HelpfulGet(apiURL) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| db := pool.Get() | |
| defer db.Close() | |
| apiResp, err := ioutil.ReadAll(r.Body) | |
| if err != nil { | |
| log.Fatal("ioutil.ReadAll: ", err) | |
| } | |
| _, err = db.Do("SET", *host, apiResp) | |
| if err != nil { | |
| log.Fatal(err) | |
| } else { | |
| log.Println("OK") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment