Skip to content

Commit

Permalink
adding a healthcheck endpoint that confirms communication with influx…
Browse files Browse the repository at this point in the history
…db and sqlite.

ref: #381
  • Loading branch information
AnalogJ committed Nov 30, 2022
1 parent 58ef1aa commit f2856e0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions webapp/backend/pkg/database/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type DeviceRepo interface {
Close() error
HealthCheck(ctx context.Context) error

RegisterDevice(ctx context.Context, dev models.Device) error
GetDevices(ctx context.Context) ([]models.Device, error)
Expand Down
23 changes: 23 additions & 0 deletions webapp/backend/pkg/database/scrutiny_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,29 @@ func (sr *scrutinyRepository) Close() error {
return nil
}

func (sr *scrutinyRepository) HealthCheck(ctx context.Context) error {
//check influxdb
status, err := sr.influxClient.Health(ctx)
if err != nil {
return fmt.Errorf("influxdb healthcheck failed: %w", err)
}
if status.Status != "pass" {
return fmt.Errorf("influxdb healthcheckf failed: status=%s", status.Status)
}

//check sqlite db.
database, err := sr.gormClient.DB()
if err != nil {
return fmt.Errorf("sqlite healthcheck failed: %w", err)
}
err = database.Ping()
if err != nil {
return fmt.Errorf("sqlite healthcheck failed during ping: %w", err)
}
return nil

}

func InfluxSetupComplete(influxEndpoint string) (bool, error) {
influxUri, err := url.Parse(influxEndpoint)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions webapp/backend/pkg/web/handler/health_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package handler

import (
"github.com/analogj/scrutiny/webapp/backend/pkg/database"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"net/http"
)

func HealthCheck(c *gin.Context) {
logger := c.MustGet("LOGGER").(*logrus.Entry)
deviceRepo := c.MustGet("DEVICE_REPOSITORY").(database.DeviceRepo)
logger.Infof("Checking Influxdb & Sqlite health")

//check sqlite and influxdb health
err := deviceRepo.HealthCheck(c)
if err != nil {
logger.Errorln("An error occurred during healthcheck", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
return
}

//TODO:
// check if the /web folder is populated.

c.JSON(http.StatusOK, gin.H{
"success": true,
})
}
10 changes: 1 addition & 9 deletions webapp/backend/pkg/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,7 @@ func (ae *AppEngine) Setup(logger *logrus.Entry) *gin.Engine {
{
api := base.Group("/api")
{
api.GET("/health", func(c *gin.Context) {
//TODO:
// check if the /web folder is populated.
// check if access to influxdb
// check if access to sqlitedb.
c.JSON(http.StatusOK, gin.H{
"success": true,
})
})
api.GET("/health", handler.HealthCheck)
api.POST("/health/notify", handler.SendTestNotification) //check if notifications are configured correctly

api.POST("/devices/register", handler.RegisterDevices) //used by Collector to register new devices and retrieve filtered list
Expand Down

0 comments on commit f2856e0

Please sign in to comment.