checkers

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2017 License: MIT Imports: 11 Imported by: 0

README

checkers

The health library comes with a number of built-in checkers for well known types of dependencies.

If a pre-built checker is not available, you can create your own checkers by implementing the ICheckable interface (which consists of a single method - Status() (interface{}, error)).

If you do create a custom-checker - consider opening a PR and adding it to the list of built-in checkers.

Built-in checkers

HTTP

The HTTP checker is a generic HTTP call executor. To make use of it, instantiate and fill out a HTTPConfig struct and pass it into checkers.NewHTTP(...).

The only required attribute is HTTPConfig.URL (*url.URL). Refer to the source code for all available attributes on the struct.

Redis

The Redis checker allows allows you to test that your server is either available (by ping), is able to set a value, is able to get a value or all of the above.

To make use of it, instantiate and fill out a RedisConfig struct and pass it to checkers.NewRedis(...).

The RedisConfig must contain a valid RedisAuthConfig and at least one check method (ping, set or get).

Refer to the godocs for additional info.

SQL DB

Planned, but PR's welcome!

Mongo

Planned, but PR's welcome!

Documentation

Index

Constants

View Source
const (
	// RedisDefaultSetValue will be used if the `Set` check method is enabled
	// and `RedisSetOptions.Value` is _not_ set.
	RedisDefaultSetValue = "go-health/redis-check"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTP

type HTTP struct {
	Config *HTTPConfig
}

HTTP implements the ICheckable interface

func NewHTTP

func NewHTTP(cfg *HTTPConfig) (*HTTP, error)

NewHTTP creates a new HTTP checker that can be used for `.AddCheck(s)`.

func (*HTTP) Status

func (h *HTTP) Status() (interface{}, error)

Status is used for performing an HTTP check against a dependency; it satisfies the `ICheckable` interface.

type HTTPConfig

type HTTPConfig struct {
	URL        *url.URL      // Required
	Method     string        // Optional (default GET)
	Payload    interface{}   // Optional
	StatusCode int           // Optional (default 200)
	Expect     string        // Optional
	Client     *http.Client  // Optional
	Timeout    time.Duration // Optional (default 3s)
}

HTTPConfig is used for configuring an HTTP check. The only required field is `URL`.

- `Method` is optional and defaults to `GET` if undefined - `Payload` is optional and can accept `string`, `[]byte` or will attempt to marshal the input to JSON for use w/ `bytes.NewReader()` - `StatusCode` is optional and defaults to `200` - `Expect` is optional; if defined, operates as a basic "body should contain <string>" - `Client` is optional; if undefined, a new client will be created using `Timeout` - `Timeout` is optional and defaults to `3s`

type Redis

type Redis struct {
	Config *RedisConfig
	// contains filtered or unexported fields
}

Redis implements the ICheckable interface

func NewRedis

func NewRedis(cfg *RedisConfig) (*Redis, error)

NewRedis creates a new `go-redis/redis` checker that can be used w/ `AddChecks()`.

func (*Redis) Status

func (r *Redis) Status() (interface{}, error)

Status is used for performing a redis check against a dependency; it satisfies the `ICheckable` interface.

type RedisAuthConfig

type RedisAuthConfig struct {
	Addr     string // `host:port` format
	Password string // leave blank if no password
	DB       int    // leave unset if no specific db
}

RedisAuthConfig defines how to connect to redis

type RedisConfig

type RedisConfig struct {
	Auth *RedisAuthConfig
	Ping bool
	Set  *RedisSetOptions
	Get  *RedisGetOptions
}

RedisConfig is used for configuring the go-redis check.

`Auth` is _required_

  • redis connection/auth config

`Ping ` is optional

  • the most basic check method, performs a `.Ping()` on the client

`Get` is optional

  • Perform a `GET` on a key; refer to the `RedisGetOptions` docs for details

`Set` is optional

  • Perform a `SET` on a key; refer to the `RedisSetOptions` docs for details

Note: At least _one_ check method must be set/enabled; you can also enable

_all_ of the check methods (ie. perform a ping, set this key and now try
to retrieve that key).

type RedisGetOptions

type RedisGetOptions struct {
	Key               string
	Expect            string
	NoErrorMissingKey bool
}

RedisGetOptions contains attributes that can alter the behavior of the redis `GET` check.

`Key` is _required_

  • the name of the key that we are attempting to `GET`

`Expect` is optional

  • optionally verify that the value for the key matches the Expect value

`NoErrorMissingKey`

  • by default, the `GET` check will error if the key we are fetching does not exist; flip this bool if that is normal/expected/ok.

type RedisSetOptions

type RedisSetOptions struct {
	Key        string
	Value      string
	Expiration time.Duration
}

RedisSetOptions contains attributes that can alter the behavior of the redis `SET` check.

`Key` is _required_

  • the name of the key we are attempting to `SET`

`Value` is optional

  • what the value should hold; if not set, it will be set to `RedisDefaultSetValue`

`Expiration` is optional

  • if set, a TTL will be attached to the key

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL