probes

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2021 License: MIT Imports: 2 Imported by: 0

README

Probes

reference report tests coverage

Probes is a simple package to implement readiness and liveness endpoints.

⚙️ Installation

go get -u czechia.dev/probes

👀 Example

package main

import (
	"errors"
	"net/http"

	"czechia.dev/probes"
	"github.com/labstack/echo/v4"
)

const alive = true

func isAlive() error {
	if alive {
		return nil
	}
	return errors.New("dead")
}

func main() {
	go probes.StartProbes(isAlive)

	e := echo.New()
	e.GET("/liveness", probeRoute(probes.Liveness))
	e.GET("/readiness", probeRoute(probes.Readiness))
	e.Start(":8080")
}

func probeRoute(p *probes.Probe) echo.HandlerFunc {
	return func(ctx echo.Context) error {
		if p.IsUp() {
			return ctx.NoContent(http.StatusOK)
		}
		return ctx.NoContent(http.StatusServiceUnavailable)
	}
}

Documentation

Index

Constants

View Source
const (
	Up   status = "UP"
	Down status = "DOWN"
)

Variables

This section is empty.

Functions

func LivenessProbe

func LivenessProbe(liveness *Probe, readiness ...*Probe) error

LivenessProbe sets the given Liveness probe to DOWN if any given Readiness probe is DOWN for more than 5 minutes.

This function typically runs in its own goroutine. The return parameter may be used for tests.

func ReadinessProbe

func ReadinessProbe(p *Probe, pf ...ProbeFunc) error

ReadinessProbe runs any probe functions passed to it. If any of the tests fail the given probe is set to DOWN.

This function typically runs in its own goroutine. The return parameter may be used for tests.

func RunProbe

func RunProbe(p *Probe) error

RunProbe waits for status messages on the probe channel.

The probe status is updated to reflect the current status. The probe timestamp is also updated when the status is UP.

This function typically runs in its own goroutine. The return parameter may be used for tests.

func StartProbes added in v1.1.0

func StartProbes(pf ...ProbeFunc)

StartProbes is a convenience function to run the default Readiness and Liveness probes every 3 seconds using the given probe functions.

Types

type Probe

type Probe struct {
	// contains filtered or unexported fields
}
var (
	Liveness  *Probe
	Readiness *Probe
)

func New added in v1.2.0

func New(n string, s status) *Probe

New initializes a new probe with an initial status.

func (*Probe) Chan

func (p *Probe) Chan() chan<- status

Chan exposes the write end of the probe channel.

func (*Probe) Downtime

func (p *Probe) Downtime() time.Duration

Downtime is the duration since the probe went DOWN.

func (*Probe) IsUp added in v1.0.1

func (p *Probe) IsUp() bool

IsUp tests if the probe status is UP.

type ProbeFunc added in v1.2.0

type ProbeFunc func() error

Jump to

Keyboard shortcuts

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