2015-04-21 17:51:01 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-01 21:49:19 +08:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/antonholmquist/jason"
|
2015-04-21 17:51:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const ExpvarsUrl = "/debug/vars"
|
|
|
|
|
2015-05-01 21:49:19 +08:00
|
|
|
type Expvar *jason.Object
|
2015-04-21 17:51:01 +08:00
|
|
|
|
2015-05-01 21:49:19 +08:00
|
|
|
// FetchExpvar fetches expvar by http for the given addr (host:port)
|
|
|
|
func FetchExpvar(addr string) (*jason.Object, error) {
|
|
|
|
var e jason.Object
|
|
|
|
resp, err := http.Get(addr)
|
|
|
|
if err != nil {
|
|
|
|
return &e, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return &e, errors.New("Vars not found. Did you import expvars?")
|
|
|
|
} else {
|
|
|
|
expvar, err := ParseExpvar(resp.Body)
|
|
|
|
e = *expvar
|
|
|
|
if err != nil {
|
|
|
|
return &e, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &e, nil
|
2015-04-21 17:51:01 +08:00
|
|
|
}
|
|
|
|
|
2015-05-01 21:49:19 +08:00
|
|
|
func ParseExpvar(r io.Reader) (*jason.Object, error) {
|
|
|
|
return jason.NewObjectFromReader(r)
|
2015-04-21 17:51:01 +08:00
|
|
|
}
|