- Notifications
You must be signed in to change notification settings - Fork10
A go implementation of the Health Checks API used for microservice exploration, documentation and monitoring.
License
hootsuite/healthchecks
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
- Introduction
- How to Use It
- Writing a StatusCheck
- Writing a TraverseCheck
- How To Contribute
- License
- Maintainers
A go implementation of theHealth Checks API used for microserviceexploration, documentation and monitoring.
Using thehealthchecks framework in your service is easy.
- Define a
StatusEndpointfor each dependency in your service. - Register the
healthchecksframework to respond to all/status/...requests passing a slice of all yourStatusEndpoints. - That's it! As long as you have defined your
StatusEndpoints correctly, the framework will take care of the rest.
Example:
// Define a StatusEndpoint at '/status/db' for a database dependencydb := healthchecks.StatusEndpoint{ Name: "The DB", Slug: "db", Type: "internal", IsTraversable: false, StatusCheck: sqlsc.SQLDBStatusChecker{ DB: myDB }, TraverseCheck: nil,}// Define a StatusEndpoint at '/status/service-organization' for the Organization serviceorg := healthchecks.StatusEndpoint{ Name: "Organization Service", Slug: "service-organization", Type: "http", IsTraversable: true, StatusCheck: httpsc.HttpStatusChecker{ BaseUrl: "[Read value from config]", }, TraverseCheck: httpsc.HttpStatusChecker{ BaseUrl: "[Read value from config]", },}// Define the list of StatusEndpoints for your servicestatusEndpoints := []healthchecks.StatusEndpoint{ db, org }// Set the path for the about and version filesaboutFilePath := "conf/about.json"versionFilePath := "conf/version.txt"// Set up any service injected customData for /status/about response.// Values can be any valid JSON conversion and will override values set in about.json.customData := make(map[string]interface{})// Examples://// String value// customData["a-string"] = "some-value"//// Number value// customData["a-number"] = 123//// Boolean value// customData["a-bool"] = true//// Array// customData["an-array"] = []string{"val1", "val2"}//// Custom object// customObject := make(map[string]interface{})// customObject["key1"] = 1// customObject["key2"] = "some-value"// customData["an-object"] = customObject// Register all the "/status/..." requests to use our health checking frameworkhttp.Handle("/status/", healthchecks.Handler(statusEndpoints, aboutFilePath, versionFilePath, customData))AStatusCheck is a struct which implements the functionfunc CheckStatus(name string) StatusList. AStatusCheck is defined or used ina service but executed by thehealthchecks framework. The key to a successfulStatusCheck is to handle all errors on thedependency you are checking. Below is an example of aStatusCheck that checks the connection ofRedis using thegopkg.in/redis.v4 driver.
type RedisStatusChecker struct {client RedisClient}func (r RedisStatusChecker) CheckStatus(name string) healthchecks.StatusList {pong, err := r.client.Ping()// Set a default responses := healthchecks.Status{Description: name,Result: healthchecks.OK,Details: "",}// Handle any errors that Ping() function returnedif err != nil {s = healthchecks.Status{Description: name,Result: healthchecks.CRITICAL,Details: err.Error(),}}// Make sure the pong response is what we expectedif pong != "PONG" {s = healthchecks.Status{Description: name,Result: healthchecks.CRITICAL,Details: fmt.Sprintf("Expecting `PONG` response, got `%s`", pong),}}// Return our responsereturn healthchecks.StatusList{ StatusList: []healthchecks.Status{ s }}}ATraverseCheck is a struct which implements the functionfunc Traverse(traversalPath []string, action string) (string, error).ATraverseCheck is defined or used in a service but executed by thehealthchecks framework. The key to a successfulTraverseCheck is to build and execute the/status/traverse?action=[action]&dependencies=[dependencies] request tothe service you are trying to traverse to and returning the response or error you got. Below is an example of aTraverseCheck for an HTTP service.
type HttpStatusChecker struct {BaseUrl stringName string}func (h HttpStatusChecker) Traverse(traversalPath []string, action string) (string, error) {dependencies := ""if len(traversalPath) > 0 {dependencies = fmt.Sprintf("&dependencies=%s", strings.Join(traversalPath, ","))}// Build our HTTP requesturl := fmt.Sprintf("%s/status/traverse?action=%s%s", h.BaseUrl, action, dependencies)req, err := http.NewRequest("GET", url, nil)if err != nil {fmt.Printf("Error creating request: %s \n", err.Error())return "", err}// Execute HTTP requestclient := &http.Client{}resp, err := client.Do(req)if err != nil {fmt.Printf("Error executing request: %s \n", err.Error())return "", err}// Defer the closing of the bodydefer resp.Body.Close()// Read our responseresponseBody, err := ioutil.ReadAll(resp.Body)if err != nil {fmt.Printf("Error reading response body: %s", err.Error())return "", err}// Return our responsereturn string(responseBody), nil}Contribute by submitting a PR and a bug report in GitHub.
healthchecks is released under the Apache License, Version 2.0. SeeLICENSE for details.
About
A go implementation of the Health Checks API used for microservice exploration, documentation and monitoring.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.