Skip to content

Commit

Permalink
POST-937 Move version to top level health check (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsafoodpanda authored Apr 17, 2018
1 parent 202de55 commit 7e83b40
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
22 changes: 2 additions & 20 deletions example.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func main() {
// /health/check/lb and /health/check
// any instance of `func() ranger_http.HealthCheckService` sent as parameter of the configuration will be converted to JSON and printed
// if necessary, you also can add a prefix to the endpoints with WithPrefix("/prefix")
// ex: WithHealthCheckFor(ranger_http.NewHealthCheckConfiguration(versionHealthCheck()).WithPrefix("/prefix"))
WithHealthCheckFor(ranger_http.NewHealthCheckConfiguration(versionHealthCheck(), etcdHealthCheck()))
// ex: WithHealthCheckFor(ranger_http.NewHealthCheckConfiguration(someHealthCheck()).WithPrefix("/prefix"))
WithHealthCheckFor(ranger_http.NewHealthCheckConfiguration(etcdHealthCheck()).WithVersion("version.json"))

// add some endpoints. based on "github.com/julienschmidt/httprouter"
s.GET("/hello", helloEndpoint())
Expand Down Expand Up @@ -109,24 +109,6 @@ func helloEndpoint() httprouter.Handle {
}
}

func versionHealthCheck() func() ranger_http.HealthCheckService {
type versionHealthCheck struct {
Tag string `json:"tag"`
Commit string `json:"commit"`
}

return func() ranger_http.HealthCheckService {
return ranger_http.HealthCheckService{
Name: "version",
Status: true,
Info: versionHealthCheck{
Tag: "0.0.0",
Commit: "30ac4383d0260f517d7f171de244fa46c1879c67",
},
}
}
}

func etcdHealthCheck() func() ranger_http.HealthCheckService {
type etcdHealthCheck struct {
ResponseTime int `json:"response_time"`
Expand Down
46 changes: 42 additions & 4 deletions ranger_http/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package ranger_http
import (
"encoding/json"
"fmt"
"github.com/julienschmidt/httprouter"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"

"github.com/julienschmidt/httprouter"
)

//HealthCheckService
Expand All @@ -20,12 +22,19 @@ type HealthCheckService struct {
type healthCheckConfiguration struct {
Prefix string
Services []func() HealthCheckService
Version healthCheckVersion
}

type healthCheckVersion struct {
Tag string `json:"tag"`
Commit string `json:"commit"`
}

//NewHealthCheckConfiguration
func NewHealthCheckConfiguration(services ...func() HealthCheckService) healthCheckConfiguration {
return healthCheckConfiguration{
Services: services,
Version: healthCheckVersion{"n/a", "n/a"},
}
}

Expand All @@ -36,15 +45,39 @@ func (configuration healthCheckConfiguration) WithPrefix(prefix string) healthCh
return configuration
}

// WithVersion expects the path to a json file for healthCheckVersion
func (configuration healthCheckConfiguration) WithVersion(versionPath string) healthCheckConfiguration {

version := healthCheckVersion{}
configuration.Version = version

absPath, _ := filepath.Abs(versionPath)
fileBytes, err := ioutil.ReadFile(absPath)

if err != nil {
logger.Warning("Unable to load file "+versionPath, nil)
}
err = json.Unmarshal(fileBytes, &version)
if err != nil {
logger.Warning("Error parsing version file "+versionPath, nil)
}

configuration.Version = version

return configuration
}

type healthCheckResponse struct {
HTTPStatus int `json:"http-status"`
Time float64 `json:"time"`
Services map[string]interface{} `json:"checks"`
Version healthCheckVersion `json:"version"`
Host string `json:"host"`
}

//WithHealthCheckFor ...
func (s Server) WithHealthCheckFor(configuration healthCheckConfiguration) Server {
s.GET(fmt.Sprintf("%s/health/check", configuration.Prefix), HealthCheckHandler(configuration.Services))
s.GET(fmt.Sprintf("%s/health/check", configuration.Prefix), HealthCheckHandler(configuration))
s.GET(fmt.Sprintf("%s/health/check/lb", configuration.Prefix), HealthCheckHandlerLB())
return s
}
Expand All @@ -64,11 +97,14 @@ type healthCheckServiceResponse struct {
}

// HealthCheckHandler to check the service and external dependencies
func HealthCheckHandler(services []func() HealthCheckService) httprouter.Handle {
func HealthCheckHandler(configuration healthCheckConfiguration) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Header().Set("Cache-Control", "no-cache, private, max-age=0")

services := configuration.Services
hostname, _ := os.Hostname()

mapServices := make(map[string]interface{})

statusCode := http.StatusOK
Expand Down Expand Up @@ -96,6 +132,8 @@ func HealthCheckHandler(services []func() HealthCheckService) httprouter.Handle
HTTPStatus: statusCode,
Time: ElapsedTimeSince(sAll),
Services: mapServices,
Version: configuration.Version,
Host: hostname,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"tag": "this-is-tag", "commit": "this-is-commit"}

0 comments on commit 7e83b40

Please sign in to comment.