Skip to content

Instantly share code, notes, and snippets.

@eranharel
Created August 4, 2019 14:39
Show Gist options
  • Save eranharel/c5dfd0bb1989465350a93f5457756f4c to your computer and use it in GitHub Desktop.
Save eranharel/c5dfd0bb1989465350a93f5457756f4c to your computer and use it in GitHub Desktop.
Introducing go-sundheit code examples
import (
"context"
"fmt"
"net"
"time"
"github.com/AppsFlyer/go-sundheit/checks"
"github.com/pkg/errors"
)
func NewDNSCheck(host string, timeout time.Duration, minRequiredResults int) checks.Check {
resolver := net.DefaultResolver
return &checks.CustomCheck{
CheckName: "resolve." + host,
CheckFunc: func() (details interface{}, err error) {
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
addrs, err := resolver.LookupHost(ctx, host)
resolvedCount := len(addrs)
details = fmt.Sprintf("[%d] results were resolved", resolvedCount)
if err != nil {
return
}
if resolvedCount < minRequiredResults {
err = errors.Errorf("[%s] lookup returned %d results, but requires at least %d", host, resolvedCount, minRequiredResults)
}
return
},
}
}
$ curl -i http://localhost:8080/admin/health.json?type=short
HTTP/1.1 200 OK
Content-Type: application/json
Date: Wed, 17 Jul 2019 12:54:18 GMT
Content-Length: 35
{
"resolve.example.com": "PASS"
}
$ curl -i http://localhost:8080/admin/health.json
HTTP/1.1 200 OK
Content-Type: application/json
Date: Wed, 17 Jul 2019 12:54:20 GMT
Content-Length: 208
{
"resolve.example.com": {
"message": "[2] results were resolved",
"timestamp": "2019-07-17T15:54:18.225468956+03:00",
"duration": 1689142,
"contiguousFailures": 0,
"timeOfFirstFailure": null
}
}
http.HandleFunc("/health", func(w http.ResponseWriter, request *http.Request) {
w.WriteHeader(200)
})
log.Fatal(http.ListenAndServe(":8080", nil))
import (
"time"
health "github.com/AppsFlyer/go-sundheit"
"github.com/AppsFlyer/go-sundheit/checks"
)
func registerHealthChecks() {
// create a new health instance
var h = health.New()
// Schedule a host resolution check for `example.com`, requiring at least one results, and running every 10 sec
h.RegisterCheck(&health.Config{
Check: checks.NewResolveCheck("example.com", 200*time.Millisecond, 1),
ExecutionPeriod: 10 * time.Second,
})
// schedule more checks...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment