Created
August 5, 2019 07:15
-
-
Save eranharel/a441354f03015c9fb2bd7c577eb5636c to your computer and use it in GitHub Desktop.
go-sundheit Custom DNS check Example
This file contains 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
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 | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment