Files
plane/monitor/cli/cmd/start.go
Henit Chobisa a93467b95b feat: Added Prime Monitor Go Service for monitoring, health check and feature flagging (#411)
* feat: added build meta data to monitor

* feat: added versioning to prime-monitor

* feat: added logger package for prime-monitor

* feat: implemented logging in cmd

* feat: implemented moduled monorepo architecture

* feat: added cron package for healthcheck

* feat: added constants and modules for healthcheck

* feat: added environment service getter method

* feat: added environment service parser for parsing the recieved keys

* feat: added environment service provider method

* feat: added retry based executer for the test methods

* feat: added http health check method and placeholders for redis & pg

* feat: added tests for healthcheck methods

* feat: added PerformHealthCheck Controller Method for execution of healthcheck controller

* feat: added healthcheck test file for testing the healthcheck

* feat: added healthcheck to work file

* feat: added readme for healthcheck package

* feat: created healthcheck register for prime scheduler

* feat: added cron command for running cron subpart of monitor

* feat: added prime schedule handler for cron

* feat: added start command for starting up prime scheduler

* feat: added build utility files for monitor

* fix: goreleaser builds

* feat: removed Redis method from healthcheck methods

* feat: added docker file for building prime-monitor

* feat: added flag for adding healthcheck duration for cron start

* chore: removed redis as a healthcheck method

* chore: modified actions to build monitor on branch build and docker images

* feat: added monitor service in docker compose caddy

* feat: added description for HealthCheckJob

* fix: status code issue for reachable and non reachable

* feat: added api package for connecting with prime api

* feat: modified cmd package to report healthcheck status to prime

* feat: added api types inside prime-monitor

* feat: added message field for meta while status reporting

* fix: modified constants for environment variables

* feat: added monitor readme

* fix: build-branch monitor content shift to build-branch-ee

* chore: added build args on release

* feat: remove version meta data from cli

* fix: docker file changed to fix the build to default

* fix: removed build flags from github branch build

* fix: moved cron start to root level start cmd

* fix: passed the recieved machine signature to api headers

* feat: optimised docker build for monitor
2024-06-20 12:14:46 +05:30

103 lines
3.4 KiB
Go

/*
Copyright © 2024 plane.so engineering@plane.so
*/
package cmd
import (
"context"
"fmt"
"strings"
"time"
"github.com/go-co-op/gocron/v2"
prime_api "github.com/makeplane/plane-ee/monitor/lib/api"
prime_cron "github.com/makeplane/plane-ee/monitor/lib/cron"
"github.com/makeplane/plane-ee/monitor/lib/healthcheck"
"github.com/makeplane/plane-ee/monitor/pkg/constants/descriptors"
"github.com/makeplane/plane-ee/monitor/pkg/handlers"
"github.com/spf13/cobra"
)
var StartCmd = &cobra.Command{
Use: descriptors.CMD_START_USAGE,
Short: descriptors.CMD_START_USAGE_DESC,
RunE: func(cmd *cobra.Command, args []string) error {
healthCheckInterval, err := cmd.Flags().GetInt(descriptors.FLAG_INTERVAL_HEALTHCHECK)
if err != nil {
return err
}
primeSchedulerHandler := handlers.NewPrimeScheduleHandler()
primeScheduler, err := prime_cron.NewPrimeScheduler(primeSchedulerHandler)
if err != nil {
return err
}
primeScheduler.RegisterNewHealthCheckJob(
context.Background(),
gocron.DurationJob(time.Duration(healthCheckInterval)*time.Minute),
func(statuses []*healthcheck.HealthCheckStatus, errors []*error) {
if len(errors) != 0 {
CmdLogger.Error(context.Background(), fmt.Sprintf("Health Check Job returned not nil error message (%v)", errors[0]))
return
}
statusMap := map[string]string{}
metaMap := map[string]prime_api.StatusMeta{}
msg := ""
for _, status := range statuses {
normServiceName := "service_" + strings.ToLower(status.ServiceName)
// If the service status is inside the ok status range
if status.StatusCode >= 200 && status.StatusCode <= 227 {
statusMap[normServiceName] = descriptors.HEALTHY
msg = fmt.Sprintf("Recieved Service (%v) status code (%d)", status.ServiceName, status.StatusCode)
CmdLogger.Info(context.Background(), msg)
} else {
statusMap[normServiceName] = descriptors.UNHEALTHY
var code = prime_api.NotReachable
var statusCode = status.StatusCode
reachable := 1
if status.Status == healthcheck.SERVICE_STATUS_REACHABLE {
code = prime_api.ReachableWithNotOkStatus
msg = fmt.Sprintf("Recieved Non Healthy Status Code (%d) from Service (%v), Unhealthy", status.StatusCode, status.ServiceName)
CmdLogger.Error(context.Background(), msg)
} else {
code = prime_api.NotReachable
reachable = 0
msg = fmt.Sprintf("Recieved Non Healthy Status Code (%d) from Service (%v), Not Reachable", status.StatusCode, status.ServiceName)
CmdLogger.Error(context.Background(), msg)
}
metaMap[normServiceName] = prime_api.StatusMeta{
Message: msg,
Code: code,
StatusCode: statusCode,
Reachable: reachable,
}
}
}
monitorApi := prime_api.NewMonitorApi(HOST, LICENSE_KEY, LICENSE_VERSION, MACHINE_SIGNATURE)
errorCode := monitorApi.PostServiceStatus(prime_api.StatusPayload{
Status: statusMap,
Meta: metaMap,
Version: LICENSE_VERSION,
})
if errorCode != 0 {
CmdLogger.Error(context.Background(), fmt.Sprintf("Recived Error while reporting health status, %v", errorCode))
}
},
)
primeScheduler.StartWithBlocker()
return nil
},
}
func init() {
// interval is used as healthcheck for added consistency
StartCmd.Flags().Int(descriptors.FLAG_INTERVAL_HEALTHCHECK, 5, descriptors.FLAG_INTERVAL_HEALTHCHECK_USE)
rootCmd.AddCommand(StartCmd)
}