health

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: GPL-3.0 Imports: 7 Imported by: 0

README

Test Maintainability Test Coverage

Background

This package helps setup health check based on status of external dependencies. The idea is to add all external dependencies like database, queue connections etc, and based on their status, the health handler will return 500 or 200 response with details about all the dependencies.

Installation

go get github.com/daveamit/health@latest

Limitation

Current implementation maintains everything in default instance of healthImpl as this package is intended to be used as singleton instance. But if there are use cases where I need to expose API such that health interface and newHealth are make public and provide added api to setup registry and do various customization for prometheus

Usage

package main

import "github.com/daveamit/health"

func main() {
    // Register a dep
    health.EnsureService("posgtres", "default")


    // postgresConn is a hypothetical function, assume that it return
    // a valid pg connection on sucess and err on failure.
    pg, err := postgresConn()

    if err != nil {
        // tell the package that `postgres` service is connected and usable.
        health.ServiceUp("postgres", "default")
    } else {
        health.ServiceDown("postgres", "default")
    }

    // so something with the pg connection
    ...
    ...
    ...

    // serve prom and health endpoints @ 8000 port
    http.Handle("/prom", health.PrometheusScrapHandler())
    http.Handle("/health", health.HealthCheckHandler())
    http.ListenAndServe(":8000", nil)
}

Documentation

Index

Constants

View Source
const (
	UndefinedServiceState serviceState = iota
	RunningServiceState
	StoppedServiceState
)

Variables

This section is empty.

Functions

func ClearItems added in v1.0.1

func ClearItems()

func EnsureService

func EnsureService(name string, namespace string)

func HealthCheckHandler

func HealthCheckHandler() http.Handler

func PrometheusScrapHandler

func PrometheusScrapHandler() http.Handler

func ServiceDown

func ServiceDown(name string, namespace string)

func ServiceUp

func ServiceUp(name string, namespace string)

func SetSafeNameReplacer added in v1.0.1

func SetSafeNameReplacer(r Replacer)

Types

type Replacer added in v1.0.1

type Replacer interface {
	Replace(string) string
}

func GetSafeNameReplacer added in v1.0.1

func GetSafeNameReplacer() Replacer

func NewCharacterToUnderscoreReplacer added in v1.0.1

func NewCharacterToUnderscoreReplacer(c ...string) Replacer

NewCharacterToUnderscoreReplacer will retun a replacer which will replace given chars to _

Jump to

Keyboard shortcuts

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