import (
"bytes"
"fmt"
health "github.com/AppsFlyer/go-sundheit"
//"io/ioutil"
"net/http"
"time"
"log"
healthhttp "github.com/AppsFlyer/go-sundheit/http"
"github.com/AppsFlyer/go-sundheit/checks"
)
func main() {
// create a new health instance
h := health.New()
s := '{"test":"json"}'
r := bytes.NewBuffer([]byte(s))
httpCheckConf := checks.HTTPCheckConfig{
CheckName: "reverse",
Timeout: 1 * time.Second,
Body: r,
// dependency you're checking - use your own URL here...
// this URL will fail 50% of the times
URL: "http://127.0.0.1:9991/g",
}
// create the HTTP check for the dependency
// fail fast when you misconfigured the URL. Don't ignore errors!!!
httpCheck, err := checks.NewHTTPCheck(httpCheckConf)
if err != nil {
fmt.Println(err)
return // your call...
}
// Alternatively panic when creating a check fails
httpCheck = checks.Must(checks.NewHTTPCheck(httpCheckConf))
err = h.RegisterCheck(&health.Config{
Check: httpCheck,
InitialDelay: time.Second, // the check will run once after 1 sec
ExecutionPeriod: 10 * time.Second, // the check will be executed every 10 sec
})
if err != nil {
fmt.Println("Failed to register check: ", err)
return // or whatever
}
// define more checks...
// register a health endpoint
http.Handle("/admin/health.json", healthhttp.HandleHealthJSON(h))
// serve HTTP
log.Fatal(http.ListenAndServe(":8080", nil))
}
When app started everything is OK - request sent as expected. But on the second iteration i have empty Body in request because they have type io.Reader and can`t use second time.
tcpdump:
GET /g HTTP/1.1
Host: 127.0.0.1:9991
User-Agent: Go-http-client/1.1
Content-Length: 15
Accept-Encoding: gzip
{"test":"json"}HTTP/1.1 404 Not Found
Content-Type: text/plain
Date: Wed, 29 Apr 2020 13:46:40 GMT
Content-Length: 18
404 page not found
-----
GET /g HTTP/1.1
Host: 127.0.0.1:9991
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip
HTTP/1.1 404 Not Found
Content-Type: text/plain
Date: Wed, 29 Apr 2020 12:36:04 GMT
Content-Length: 18
404 page not found
I am newby in Golang and maybe don't understand how to send Body on each check.
In my fork i change type of Body to 'string' and write NewRequest via NewReader:
- req, err := http.NewRequest(check.config.Method, check.config.URL, check.config.Body)
+ req, err := http.NewRequest(check.config.Method, check.config.URL, bytes.NewReader([]byte(check.config.Body)))