healthcheck

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2023 License: AGPL-3.0 Imports: 7 Imported by: 0

README

healthcheck

[[TOC]]

Goal

This library aims to make easy to set up healthchecks with prometheus metrics. There are some ready-to-use checks.

Liveness vs Readiness

There are 2 probes in Kubernetes: liveness and readiness.

  • Liveness - need to check whether pod is stuck and needs to be restarted. Most common way to do it - just respond 200 OK on main port always without any subchecks. In most of our services GRPC is the main port so GRPC handler should be used.
  • Readiness - need to check whether pod is ready to accept traffic. There is no need to place in on main port so we use debug port for it.

How to use

  1. Create new HealthChecker with all required subchecks. You can also specify its name (for metrics) and enable metrics
  2. Use it either as http.Handler (HealthChecker implements it) or call GRPCRegisterService method for GRPC handler

How to add new checks

In this library
  1. Add new file with Subcheck implementation in internal/pkg/healthcheck
  2. Add new file with HealthCheckerOption implementation in root directory
Outside library

Implement Subcheck interface and add it via AddSubcheck method on HealthChecker.

package main

import (
	"context"

	"gitlab.com/picnic-app/backend/libs/golang/healthcheck"
)

type BlablaSubcheck struct {}

func (*BlablaSubcheck) name() string { return "blabla" }
func (*BlablaSubcheck) run(_ context.Context) error { return nil }
func (*BlablaSubcheck) isWarning() bool { return false }

func main() {
	hc := healthcheck.New()
	hc.AddSubcheck(&BlablaSubcheck{})
}


Documentation

Index

Constants

View Source
const (
	// HTTPLivenessRoute is a standard route to use for our liveness probes.
	HTTPLivenessRoute = "/healthz/live"
	// HTTPReadinessRoute is a standard route to use for our readiness probes.
	HTTPReadinessRoute = "/healthz/ready"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HealthChecker

type HealthChecker interface {
	http.Handler

	fmt.Stringer

	Shutdown(context.Context) error
	GRPCRegisterService(grpc.ServiceRegistrar)
	Register(grpc.ServiceRegistrar)
	// contains filtered or unexported methods
}

HealthChecker is an HTTP healthcheck handler that runs checks.

func NewHealthCheck

func NewHealthCheck(opts ...HealthCheckerOption) HealthChecker

NewHealthCheck returns new healthcheck.

type HealthCheckerOption

type HealthCheckerOption interface {
	// contains filtered or unexported methods
}

HealthCheckerOption is an option to apply to HealthChecker.

func WithBucketCheck

func WithBucketCheck(bucket *storage.BucketHandle) HealthCheckerOption

WithBucketCheck adds GCS bucket check (tries to read nonexistent test object).

func WithMetrics

func WithMetrics() HealthCheckerOption

WithMetrics enables metrics collection.

func WithName

func WithName(name string) HealthCheckerOption

WithName allows to change healthcheck name in metrics.

func WithRedisCheck

func WithRedisCheck(client healthcheck.IRedisClient, warn bool) HealthCheckerOption

WithRedisCheck adds Redis check (PING command).

func WithSpannerCheck

func WithSpannerCheck(client *spanner.Client) HealthCheckerOption

WithSpannerCheck adds Spanner check (SELECT 1).

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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