Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: add liveness and readiness checks #2509

Merged
merged 22 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
144c631
:sparkles: feat: add liveness and readiness checkers
luk3skyw4lker Jun 17, 2023
fed6c5e
:memo: docs: add docs for liveness and readiness
luk3skyw4lker Jun 17, 2023
96d3944
:sparkles: feat: add options method for probe checkers
luk3skyw4lker Jun 17, 2023
ec98691
:white_check_mark: tests: add tests for liveness and readiness
luk3skyw4lker Jun 17, 2023
7615bec
:recycle: refactor: change default endpoint values
luk3skyw4lker Jun 17, 2023
b00ee64
:recycle: refactor: change default value for liveness endpoint
luk3skyw4lker Jun 17, 2023
87faa6a
:memo: docs: add return status for liveness and readiness probes
luk3skyw4lker Jun 17, 2023
789b882
:recycle: refactor: change probechecker to middleware
luk3skyw4lker Jun 27, 2023
ef1e265
:twisted_rightwards_arrows: chore: update branch with master
luk3skyw4lker Jun 27, 2023
df23f68
:memo: docs: move docs to middleware session
luk3skyw4lker Jun 30, 2023
4ad217e
:recycle: refactor: apply gofumpt formatting
luk3skyw4lker Aug 15, 2023
2c41068
:recycle: refactor: remove unused parameter
luk3skyw4lker Aug 15, 2023
7fdb8aa
split config and apply a review
efectn Dec 23, 2023
931526a
apply reviews and add testcases
efectn Dec 23, 2023
508bddb
add benchmark
efectn Dec 23, 2023
2041679
cleanup
efectn Dec 23, 2023
e987a8e
rename middleware
efectn Dec 24, 2023
c33626b
fix linter
efectn Dec 24, 2023
013e362
Update docs and config values
gaby Jan 3, 2024
5c08790
Revert change to IsReady
gaby Jan 3, 2024
92e1cac
Updates based on code review
gaby Jan 3, 2024
87b1822
Update docs to match other middlewares
gaby Jan 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ const (
DefaultReadBufferSize = 4096
DefaultWriteBufferSize = 4096
DefaultCompressedFileSuffix = ".fiber.gz"
DefaultLivenessEndpoint = "/livez"
DefaultReadinessEndpoint = "/readyz"
luk3skyw4lker marked this conversation as resolved.
Show resolved Hide resolved
)

// HTTP methods enabled by default
Expand Down
2 changes: 1 addition & 1 deletion docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -654,4 +654,4 @@ Hooks is a method to return [hooks](../guide/hooks.md) property.

```go title="Signature"
func (app *App) Hooks() *Hooks
```
```
efectn marked this conversation as resolved.
Show resolved Hide resolved
75 changes: 75 additions & 0 deletions docs/api/middleware/probechecker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
id: probecheker
efectn marked this conversation as resolved.
Show resolved Hide resolved
title: probecheker
---

Liveness and readiness probes middleware for [Fiber](https://github.com/gofiber/fiber) that provides two endpoints for checking the health and ready state of any HTTP application.

The endpoint values default to `/livez` for liveness and `/readyz` for readiness. Both functions are optional, the liveness endpoint will return `true` right when the server is up and running but the readiness endpoint will not answer any requests if an `IsReady` function isn't provided.
gaby marked this conversation as resolved.
Show resolved Hide resolved

The HTTP status returned to the containerized environment are: 200 OK if the checker function returns true and 503 Service Unavailable if the checker function returns false.
efectn marked this conversation as resolved.
Show resolved Hide resolved

## Signatures

```go
func New() fiber.Handler
```

## Examples

Import the middleware package that is part of the Fiber web framework

```go
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/probechecker"
)
```

After you initiate your Fiber app, you can use the following possibilities:

```go
// Initializing with default config
app.Use(probechecker.New())

// Initialize with custom config
app.Use(
probechecker.New(
IsLive: func (c *fiber.Ctx) bool {
return true
efectn marked this conversation as resolved.
Show resolved Hide resolved
},
IsLiveEndpoint: "/livez",
IsReady: func (c *fiber.Ctx) bool {
return serviceA.Ready() && serviceB.Ready() && ...
}
IsReadyEndpoint: "/readyz",
)
)

```

## Config

```go
type Config struct {
// Config for liveness probe of the container engine being used
//
// Optional. Default: func(c *Ctx) bool { return true }
IsLive ProbeChecker

// HTTP endpoint of the liveness probe
//
// Optional. Default: /livez
IsLiveEndpoint string

// Config for readiness probe of the container engine being used
//
// Optional. Default: nil
IsReady ProbeChecker

// HTTP endpoint of the readiness probe
//
// Optional. Default: /readyz
IsReadyEndpoint string
}
```
63 changes: 63 additions & 0 deletions middleware/probechecker/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package probechecker
efectn marked this conversation as resolved.
Show resolved Hide resolved

import "github.com/gofiber/fiber/v2"

// Config is the config struct for the probechecker middleware
type Config struct {
// Config for liveness probe of the container engine being used
//
// Optional. Default: func(c *Ctx) bool { return true }
IsLive ProbeChecker

// HTTP endpoint of the liveness probe
//
// Optional. Default: /livez
IsLiveEndpoint string

// Config for readiness probe of the container engine being used
//
// Optional. Default: nil
IsReady ProbeChecker

// HTTP endpoint of the readiness probe
//
// Optional. Default: /readyz
IsReadyEndpoint string
}

const (
DefaultLivenessEndpoint = "/livez"
DefaultReadinessEndpoint = "/readyz"
)

func defaultLiveFunc(*fiber.Ctx) bool { return true }

// ConfigDefault is the default config
var ConfigDefault = Config{
IsLive: defaultLiveFunc,
IsLiveEndpoint: DefaultLivenessEndpoint,
IsReadyEndpoint: DefaultReadinessEndpoint,
}

// defaultConfig returns a default config for the probechecker middleware.
func defaultConfig(config ...Config) Config {
if len(config) < 1 {
return ConfigDefault
}

cfg := config[0]

if cfg.IsLive == nil {
cfg.IsLive = defaultLiveFunc
}

if cfg.IsLiveEndpoint == "" {
cfg.IsLiveEndpoint = DefaultLivenessEndpoint
}

if cfg.IsReadyEndpoint == "" {
cfg.IsReadyEndpoint = DefaultReadinessEndpoint
}

return cfg
}
64 changes: 64 additions & 0 deletions middleware/probechecker/probechecker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package probechecker

import (
"fmt"

"github.com/gofiber/fiber/v2"
)

// ProbeChecker defines a function to check liveness or readiness of the application
type ProbeChecker func(*fiber.Ctx) bool

// ProbeCheckerHandler defines a function that returns a ProbeChecker
type ProbeCheckerHandler func(ProbeChecker) fiber.Handler
luk3skyw4lker marked this conversation as resolved.
Show resolved Hide resolved

func probeCheckerHandler(checker ProbeChecker) fiber.Handler {
return func(c *fiber.Ctx) error {
if checker == nil {
return c.Next()
}

if checker(c) {
return c.SendStatus(fiber.StatusOK)
}

return c.SendStatus(fiber.StatusServiceUnavailable)
}
}

func checkRoute(path string, config *Config) string {
switch path {
case DefaultLivenessEndpoint, config.IsLiveEndpoint:
return "liveness"
case DefaultReadinessEndpoint, config.IsReadyEndpoint:
return "readiness"
default:
return ""
}
}

func New(config ...Config) fiber.Handler {
cfg := defaultConfig(config...)

checkers := map[string]fiber.Handler{
"liveness": probeCheckerHandler(cfg.IsLive),
"readiness": probeCheckerHandler(cfg.IsReady),
}

return func(c *fiber.Ctx) error {
route := c.Route()
routeType := checkRoute(route.Path, &cfg)

if routeType != "" || route.Method != fiber.MethodGet {
handler, ok := checkers[routeType]

if !ok {
return fmt.Errorf("routeType of %s not found in checkers", routeType)
}

return handler(c)
}

return c.Next()
}
}
efectn marked this conversation as resolved.
Show resolved Hide resolved
Loading