Skip to content

mundipagg/healthcheck-go

Repository files navigation

GoDoc

Golang health check

Bliblioteca de health check em Golang.

Lista de serviços

  1. MongoDB;
  2. RabbitMQ.
  3. SQL Server.

Instalação

Usando go get

$ go get github.com/mundipagg/healthcheck-go

Usando govendor

$ govendor add github.com/mundipagg/healthcheck-go
$ govendor add github.com/mundipagg/healthcheck-go/checks
$ govendor add github.com/mundipagg/healthcheck-go/checks/mongo
$ govendor add github.com/mundipagg/healthcheck-go/checks/rabbit       

Exemplo

HealthCheck

package healthcheck

import (
	"github.com/gin-gonic/gin"
	"github.com/mundipagg/boleto-api/config"

	HealthCheckLib "github.com/mundipagg/healthcheck-go"
	"github.com/mundipagg/healthcheck-go/checks/mongo"
	"github.com/mundipagg/healthcheck-go/checks/rabbit"
)

func createHealthCheck() HealthCheckLib.HealthCheck {
	mongoConfig := &mongo.Config{
		Url:        config.Get().MongoURL,
		User:       config.Get().MongoUser,
		Password:   config.Get().MongoPassword,
		Database:   config.Get().MongoDatabase,
		AuthSource: config.Get().MongoAuthSource,
		Timeout:    3,
		ForceTLS:   config.Get().ForceTLS,
		MaxPoolSize:   100,
	}

	rabbitConfig := &rabbit.Config{
		ConnectionString: config.Get().ConnQueue,
	}

	healthCheck := HealthCheckLib.New()
	healthCheck.AddService(mongoConfig)
	healthCheck.AddService(rabbitConfig)

	return healthCheck
}

func Endpoint(c *gin.Context) {
	healthcheck := createHealthCheck()
	c.JSON(200, healthcheck.Execute())
}

API

package api

import (
	"github.com/gin-gonic/gin"
	"github.com/mundipagg/boleto-api/healthcheck"
)

func Base(router *gin.Engine) {
	router.GET("/healthcheck", healthcheck.Endpoint)
}

Response do endpoint

{
    "status": "Healthy",
    "results": {
        "mongo": {
            "status": "Healthy",
            "description": "mongo is healthy"
        },
        "rabbit": {
            "status": "Healthy",
            "description": "rabbit is healthy"
        }
    }
}