package
0.0.0-20230828071713-ffae65984116
Repository: https://github.com/ronak-searce/graph-api.git
Documentation: pkg.go.dev

# 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{})
}


# Functions

NewHealthCheck returns new healthcheck.
WithBucketCheck adds GCS bucket check (tries to read nonexistent test object).
WithMetrics enables metrics collection.
WithName allows to change healthcheck name in metrics.
WithRedisCheck adds Redis check (PING command).
WithSpannerCheck adds Spanner check (SELECT 1).

# Constants

HTTPLivenessRoute is a standard route to use for our liveness probes.
HTTPReadinessRoute is a standard route to use for our readiness probes.

# Interfaces

HealthChecker is an HTTP healthcheck handler that runs checks.
HealthCheckerOption is an option to apply to HealthChecker.