probes

package module
v1.1.0 Latest Latest
Warning

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

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

README

Probes

codecov Test Action Status

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, tests ...func() error) error

ReadinessProbe runs any test 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 if the recieved status differs from the current status. The probe timestamp is also updated when the status changes to DOWN.

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(tests ...func() error)

StartProbes is a convenience function to run the default Readiness and Liveness probes and test them every 3*time.Second using the given test functions.

Types

type Probe

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

func NewProbe

func NewProbe(n string, s status) *Probe

NewProbe initializes a new status probe.

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.

Jump to

Keyboard shortcuts

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