healthcheck

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MPL-2.0 Imports: 6 Imported by: 1

README

Tyk Healthcheck

Overview

Tyk Healthcheck is a flexible health checking framework designed for Tyk ecosystem applications. It offers a structured approach to implement and manage both liveness and readiness checks, ensuring your services are healthy and ready to handle requests. With TykHealthcheck, you can define custom health checks tailored to your application's specific needs and easily expose them over HTTP.

Key Features

  • Custom Health Checks: Easily define your own liveness and readiness checks specific to your application's requirements.
  • Health Check Types: Distinguish between essential (required), informational and optional checks.
  • HTTP Handler Support: TykcHealthcheck provides HTTP handlers to expose your health checks over HTTP, making it easy to integrate with your application's routing and middleware.
  • Caching: The library features built-in caching that performs periodic updates of the cache at a configurable interval. Once set, the library will automatically execute the checker every specified number of seconds, ensuring that an up-to-date value is always readily available for retrieval.

Getting Started

Prerequisites

Before you begin, ensure you have a working Go environment. TykHealthcheck is built using Go and requires Go installed to compile and run the applications that use it.

Installing TykHealthcheck

To start using TykHealthcheck in your project, you need to add it as a dependency:

go get github.com/TykTechnologies/healthcheck
Implementing Health Checks

Implementing health checks with Tyk Healthcheck involves creating health checkers, registering checks, and exposing them via HTTP. Here's a quick start guide:

1- Create Health Checkers:

Initialize health checkers for liveness and readiness checks.

readinessHealthChecker := hc.NewHealthChecker()
livenessHealthChecker := hc.NewHealthChecker()
2- Register checks

Define and register your custom health checks.

livenessHealthChecker.RegisterCheck("PingCheck", hc.Required, func() (hc.HealthStatus, error) {
    return hc.StatusPass, nil
})

readinessHealthChecker.RegisterCheck("Database", hc.Required, func() (hc.HealthStatus, error) {
    // Implement your check logic here
    return hc.StatusPass, nil
})

Optionally you can enable the caching mechanism by calling the method WithCache(seconds), Eg:

readinessHealthChecker.RegisterCheck("Database", hc.Required, func() (hc.HealthStatus, error) {
    // Implement your check logic here
    return hc.StatusPass, nil
}).WithCache(100)
3- Expose Health Checks:

Add HTTP handlers to expose your health checks.

http.HandleFunc("/health/live", livenessHealthChecker.HTTPHandler())
http.HandleFunc("/health/ready", readinessHealthChecker.HTTPHandler())
4- Start the HTTP Server:

Listen on a port to serve the health check endpoints.

http.ListenAndServe(":9000", nil)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Check

type Check struct {
	Name       string
	Importance CheckImportance
	Perform    func() CheckResult
	// contains filtered or unexported fields
}

Check represents an individual health check

func (*Check) StopCacheTicker

func (c *Check) StopCacheTicker()

func (*Check) UpdateCache

func (c *Check) UpdateCache()

func (*Check) WithCache

func (c *Check) WithCache(ttl int)

type CheckFunc

type CheckFunc func() (HealthStatus, error)

CheckFunc defines the signature for health check functions. Each health check function must conform to this signature to be registered and executed by the HealthChecker. The return values are as follows:

  • HealthStatus: The status of the check, represented by predefined constants (StatusPass, StatusWarn, StatusFail). This indicates the health of the component being checked.
  • error: An error value that should be nil if the check was successful, or provide error details if the check failed.

type CheckImportance

type CheckImportance string
const (
	// Required should be used when a check should be always ok
	Required CheckImportance = "required"
	// Optional can be used when a check is not required to be everytime ok, but if it fails then it not make the application fail
	Optional CheckImportance = "optional"
	// Info should be used when the check doesn't directly impact the operational status
	Info CheckImportance = "info"
)

type CheckResult

type CheckResult struct {
	Name          string       `json:"name"`
	Status        HealthStatus `json:"status"`
	ObservationTS time.Time    `json:"observation_ts"`
}

CheckResult represents the result of a health check

type HealthCheckResponse

type HealthCheckResponse struct {
	Status     HealthStatus  `json:"status"`
	StatusCode int           `json:"status_code"`
	Components []CheckResult `json:"components,omitempty"`
}

type HealthChecker

type HealthChecker struct {
	// contains filtered or unexported fields
}

HealthChecker manages a set of checks

func NewHealthChecker

func NewHealthChecker() *HealthChecker

func (*HealthChecker) HTTPHandler

func (hc *HealthChecker) HTTPHandler() http.HandlerFunc

func (*HealthChecker) PerformChecks

func (hc *HealthChecker) PerformChecks() HealthCheckResponse

func (*HealthChecker) RegisterCheck

func (hc *HealthChecker) RegisterCheck(name string, importance CheckImportance, checkFunc CheckFunc) *Check

type HealthStatus

type HealthStatus string
const (
	StatusPass HealthStatus = "pass"
	StatusWarn HealthStatus = "warn"
	StatusFail HealthStatus = "fail"
)

Directories

Path Synopsis
examples
basic module

Jump to

Keyboard shortcuts

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