Skip to content

Commit

Permalink
feat: make http statuses configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
0x416e746f6e committed Jun 30, 2024
1 parent 51a013e commit 484ff13
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 10 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: release

on:
workflow_dispatch:
push:
tags:
- "v*"

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4

- name: setup go dependencies
uses: actions/setup-go@v5
with:
go-version: "1.21"

- name: build and publish backend release
uses: goreleaser/goreleaser-action@v5
with:
args: release --clean
distribution: goreleaser
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 0 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,5 @@ archives:
checksum:
name_template: 'checksums.txt'

signs:
- artifacts: checksum

release:
prerelease: auto
25 changes: 25 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"net/http"
"time"

"github.com/flashbots/node-healthchecker/healthchecker"
Expand Down Expand Up @@ -34,6 +35,30 @@ func CommandServe() *cli.Command {
Value: time.Second,
},

&cli.IntFlag{
Category: "Serving:",
Destination: &cfg.StatusOk,
Name: "status-ok",
Usage: "http `status` to report for good healthchecks",
Value: http.StatusOK,
},

&cli.IntFlag{
Category: "Serving:",
Destination: &cfg.StatusWarning,
Name: "status-warning",
Usage: "http `status` to report for warning healthchecks",
Value: http.StatusAccepted,
},

&cli.IntFlag{
Category: "Serving:",
Destination: &cfg.StatusError,
Name: "status-error",
Usage: "http `status` to report for error healthchecks",
Value: http.StatusInternalServerError,
},

// Monitoring

&cli.StringFlag{
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/flashbots/node-healthchecker
go 1.21

require (
github.com/google/uuid v1.3.1
github.com/urfave/cli/v2 v2.25.7
go.uber.org/zap v1.26.0
github.com/google/uuid v1.6.0
github.com/urfave/cli/v2 v2.27.2
go.uber.org/zap v1.27.0
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
go.uber.org/multierr v1.11.0 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
Expand All @@ -12,13 +16,19 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 15 additions & 2 deletions healthchecker/healthchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type Healthchecker struct {
timeout time.Duration

monitors []healthcheckMonitor

statusOk int
statusWarning int
statusError int
}

type healthcheckMonitor = func(context.Context) *healthcheckResult
Expand All @@ -37,13 +41,21 @@ type Config struct {

ServeAddress string
Timeout time.Duration

StatusOk int
StatusWarning int
StatusError int
}

func New(cfg *Config) (*Healthchecker, error) {
h := &Healthchecker{
addr: cfg.ServeAddress,
log: zap.L(),
timeout: cfg.Timeout,

statusOk: cfg.StatusOk,
statusWarning: cfg.StatusWarning,
statusError: cfg.StatusError,
}

// Configure geth checks
Expand Down Expand Up @@ -140,10 +152,11 @@ func (h *Healthchecker) handleHTTPRequest(w http.ResponseWriter, r *http.Request

switch {
case len(errs) == 0 && len(warns) == 0:
w.WriteHeader(h.statusOk)
return

case len(errs) > 0:
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(h.statusError)
w.Header().Set("Content-Type", "application/text")

for idx, err := range errs {
Expand All @@ -168,7 +181,7 @@ func (h *Healthchecker) handleHTTPRequest(w http.ResponseWriter, r *http.Request
)

case len(errs) == 0 && len(warns) > 0:
w.WriteHeader(http.StatusAccepted)
w.WriteHeader(h.statusWarning)
w.Header().Set("Content-Type", "application/text")

for idx, warn := range warns {
Expand Down

0 comments on commit 484ff13

Please sign in to comment.