Health Check library in Golang.
-
checks: Folder containing checks for external dependencies (SQL Server, RabbitMQ, and MongoDB).
- mongo:
- check.go: Implementation of MongoDB check.
- check_test.go: Tests for MongoDB check.
- stub.go: Stub for MongoDB tests.
- rabbit:
- check.go: Implementation of RabbitMQ check.
- check_test.go: Tests for RabbitMQ check.
- stub.go: Stub for RabbitMQ tests.
- sqlServer:
- check.go: Implementation of SQL Server check.
- check_test.go: Tests for SQL Server check.
- stub.go: Stub for SQL Server tests.
- mongo:
-
healthcheck.go: Main configuration file.
-
docker-compose.yml: Docker Compose file for tests.
- MongoDB;
- SQL Server;
- RabbitMQ.
$ go get github.com/wesleycosta/healthcheck-go
$ govendor add github.com/wesleycosta/healthcheck-go
$ govendor add github.com/wesleycosta/healthcheck-go/checks
$ govendor add github.com/wesleycosta/healthcheck-go/checks/mongo
$ govendor add github.com/wesleycosta/healthcheck-go/checks/rabbit
$ govendor add github.com/wesleycosta/healthcheck-go/checks/sqlServer
To configure Healthcheck in your application, follow these steps:
-
Create a HealthCheck instance:
- Use
HealthCheckLib.New()
to create a Healthcheck instance.
- Use
-
Configure which services will be monitored:
- For each service you want to monitor, configure specific options.
- Examples:
- MongoDB: Use the
mongo.Config
file. - RabbitMQ: Use the
rabbit.Config
file. - SQL Server: Use the
sqlServer.Config
file.
- MongoDB: Use the
-
Add Configurations to HealthCheck:
- After creating specific service configurations, add them to the HealthCheck instance using the
AddService
method.
- After creating specific service configurations, add them to the HealthCheck instance using the
-
Integrate HealthCheck into your Endpoint:
- When you have a configured HealthCheck instance, integrate it into your endpoint to perform service checks.
- Return the results of these checks as a JSON response.
package healthcheck
import (
"github.com/gin-gonic/gin"
HealthCheckLib "github.com/wesleycosta/healthcheck-go"
"github.com/wesleycosta/healthcheck-go/checks/mongo"
"github.com/wesleycosta/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
}
package api
import (
"github.com/gin-gonic/gin"
"github.com/wesleycosta/healthcheck"
)
func Base(router *gin.Engine) {
router.GET("/healthcheck", healthcheck.Endpoint)
}
{
"status": "Healthy",
"results": {
"mongo": {
"status": "Healthy",
"description": "rabbit is healthy"
},
"rabbit": {
"status": "Healthy",
"description": "rabbit is healthy"
}
}
}
{
"status": "Unhealthy",
"results": {
"mongo": {
"status": "Unhealthy",
"description": "<error description>"
},
"rabbit": {
"status": "Healthy",
"description": "rabbit is healthy"
}
}
}