Skip to content

Commit

Permalink
Merge e81b819 into 8a413a9
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed May 10, 2021
2 parents 8a413a9 + e81b819 commit 54d5228
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -153,7 +153,7 @@ The `NewDialPinger` function supports all the network/address parameters support

### Custom Checks
The library provides 2 means of defining a custom check.
The bottom line is that you need an implementation of the `checks.Check` interface:
The bottom line is that you need an implementation of the `Check` interface:
```go
// Check is the API for defining health checks.
// A valid check has a non empty Name() and a check (Execute()) function.
Expand Down Expand Up @@ -201,7 +201,7 @@ h.RegisterCheck(&gosundheit.Config{
#### Implement the Check interface
Sometimes you need to define a more elaborate custom check.
For example when you need to manage state.
For these cases it's best to implement the `checks.Check` interface yourself.
For these cases it's best to implement the `Check` interface yourself.

Let's define a flexible example of the lottery check, that allows you to define a fail probability:
```go
Expand Down
2 changes: 1 addition & 1 deletion checks/check.go → check.go
@@ -1,4 +1,4 @@
package checks
package gosundheit

// Check is the API for defining health checks.
// A valid check has a non empty Name() and a check (Execute()) function.
Expand Down
4 changes: 1 addition & 3 deletions check_task.go
Expand Up @@ -2,14 +2,12 @@ package gosundheit

import (
"time"

"github.com/AppsFlyer/go-sundheit/checks"
)

type checkTask struct {
stopChan chan bool
ticker *time.Ticker
check checks.Check
check Check
}

func (t *checkTask) stop() {
Expand Down
4 changes: 3 additions & 1 deletion checks/custom.go
@@ -1,5 +1,7 @@
package checks

import gosundheit "github.com/AppsFlyer/go-sundheit"

// CustomCheck is a simple Check implementation if all you need is a functional check
type CustomCheck struct {
// CheckName s the name of the check.
Expand All @@ -8,7 +10,7 @@ type CustomCheck struct {
CheckFunc func() (details interface{}, err error)
}

var _ Check = (*CustomCheck)(nil)
var _ gosundheit.Check = (*CustomCheck)(nil)

// Name is the name of the check.
// Check names must be metric compatible.
Expand Down
9 changes: 5 additions & 4 deletions checks/dns.go
Expand Up @@ -6,21 +6,22 @@ import (
"net"
"time"

gosundheit "github.com/AppsFlyer/go-sundheit"
"github.com/pkg/errors"
)

// NewHostResolveCheck returns a Check that makes sure the provided host can resolve
// NewHostResolveCheck returns a gosundheit.Check that makes sure the provided host can resolve
// to at least `minRequiredResults` IP address within the specified timeout.
func NewHostResolveCheck(host string, timeout time.Duration, minRequiredResults int) Check {
func NewHostResolveCheck(host string, timeout time.Duration, minRequiredResults int) gosundheit.Check {
return NewResolveCheck(NewHostLookup(nil), host, timeout, minRequiredResults)
}

// LookupFunc is a function that is used for looking up something (in DNS) and return the resolved results count, and a possible error
type LookupFunc func(ctx context.Context, lookFor string) (resolvedCount int, err error)

// NewResolveCheck returns a Check that makes sure the `resolveThis` arg can be resolved using the `lookupFn`
// NewResolveCheck returns a gosundheit.Check that makes sure the `resolveThis` arg can be resolved using the `lookupFn`
// to at least `minRequiredResults` result within the specified timeout.
func NewResolveCheck(lookupFn LookupFunc, resolveThis string, timeout time.Duration, minRequiredResults int) Check {
func NewResolveCheck(lookupFn LookupFunc, resolveThis string, timeout time.Duration, minRequiredResults int) gosundheit.Check {
return &CustomCheck{
CheckName: "resolve." + resolveThis,
CheckFunc: func() (details interface{}, err error) {
Expand Down
3 changes: 2 additions & 1 deletion checks/http.go
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

gosundheit "github.com/AppsFlyer/go-sundheit"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ type httpCheck struct {
type BodyProvider func() io.Reader

// NewHTTPCheck creates a new http check defined by the given config
func NewHTTPCheck(config HTTPCheckConfig) (check Check, err error) {
func NewHTTPCheck(config HTTPCheckConfig) (check gosundheit.Check, err error) {
if config.URL == "" {
return nil, errors.Errorf("URL must not be empty")
}
Expand Down
6 changes: 4 additions & 2 deletions checks/must.go
@@ -1,9 +1,11 @@
package checks

// Must is a helper that wraps a call to a function returning (Check, error) and panics if the error is non-nil.
import gosundheit "github.com/AppsFlyer/go-sundheit"

// Must is a helper that wraps a call to a function returning (gosundheit.Check, error) and panics if the error is non-nil.
// It is intended for use in check initializations such as
// c := checks.Must(checks.NewHTTPCheck(/*...*/))
func Must(check Check, err error) Check {
func Must(check gosundheit.Check, err error) gosundheit.Check {
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion checks/ping.go
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"time"

gosundheit "github.com/AppsFlyer/go-sundheit"
"github.com/pkg/errors"
)

Expand All @@ -23,7 +24,7 @@ func (f PingContextFunc) PingContext(ctx context.Context) error {
}

// NewPingCheck returns a Check that pings using the specified Pinger and fails on timeout or ping failure
func NewPingCheck(name string, pinger Pinger, timeout time.Duration) (Check, error) {
func NewPingCheck(name string, pinger Pinger, timeout time.Duration) (gosundheit.Check, error) {
if pinger == nil {
return nil, errors.New("Pinger must not be nil")
}
Expand Down
4 changes: 1 addition & 3 deletions config.go
Expand Up @@ -2,14 +2,12 @@ package gosundheit

import (
"time"

"github.com/AppsFlyer/go-sundheit/checks"
)

// Config defines a health Check and it's scheduling timing requirements.
type Config struct {
// Check is the health Check to be scheduled for execution.
Check checks.Check
Check Check
// ExecutionPeriod is the period between successive executions.
ExecutionPeriod time.Duration
// InitialDelay is the time to delay first execution; defaults to zero.
Expand Down
3 changes: 2 additions & 1 deletion health_test.go
@@ -1,4 +1,4 @@
package gosundheit
package gosundheit_test

import (
"errors"
Expand All @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

. "github.com/AppsFlyer/go-sundheit"
"github.com/AppsFlyer/go-sundheit/checks"
)

Expand Down

0 comments on commit 54d5228

Please sign in to comment.