Coder Social home page Coder Social logo

go-health's Introduction

health

Build Status Go Report Card codecov GoDoc

Health package simplifies the way you add health check to your service.

For a real application using go-health please check ImgArt

Supported Features

  • Service health status
  • Graceful Shutdown Pattern
  • Health check external dependencies
  • HTTP Handler out of the box that returns the health status

Installation

go get -u github.com/Talento90/go-health

How to use

    // Create a new instance of Health
    h := New("service-name", Options{CheckersTimeout: time.Second * 1})

    // Register external dependencies
    h.RegisterChecker("redis", redisDb)
    h.RegisterChecker("mongo", mongoDb)
    h.RegisterChecker("external_api", api)

    // Get service health status
    s := h.GetStatus()

    // Listen interrupt OS signals for graceful shutdown
    var gracefulShutdown = make(chan os.Signal)

    signal.Notify(gracefulShutdown, syscall.SIGTERM)
    signal.Notify(gracefulShutdown, syscall.SIGINT)

    go func() {
    <-gracefulShutdown
        h.Shutdown()

        // Close Databases gracefully
        // Close HttpServer gracefully
    }


    // if you have an http server you can register the default handler
    // ServeHTTP return 503 (Service Unavailable) if service is shutting down
    http.HandleFunc("/health", h.ServeHTTP)

Response Example

{
    "service":"imgart",
    "up_time":"14m5.788341028s",
    "start_time":"2018-03-11T17:02:33Z",
    "memory":{
        "current":{
            "total_alloc":8359984,
            "heap_alloc":2285896,
            "rss":5767168
        },
        "initial":{
            "total_alloc":7784792,
            "heap_alloc":1754064,
            "rss":5701632
        },
        "diff":{
            "total_alloc":575192,
            "heap_alloc":531832,
            "rss":65536
        }
    },
    "go_routines":21,
    "is_shutting_down":false,
    "health_checkers":{
        "mongo":{
            "status":"CHECKED",
            "response_time":"573.813µs"
        },
        "redis":{
            "status":"CHECKED",
            "error":{
                "Syscall":"getsockopt",
                "Err":113
            },
            "response_time":"93.526014ms"
        },
        "external_api": {
            "status":"TIMEOUT",
            "response_time":"1.2s"
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.