-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (83 loc) · 2.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package gohealthwatch
import (
"context"
"net/http"
"time"
"github.com/pcpratheesh/go-healthwatch/config"
"github.com/pcpratheesh/go-healthwatch/constants"
"github.com/pcpratheesh/go-healthwatch/service"
"github.com/pcpratheesh/go-healthwatch/utils/errors"
"github.com/pcpratheesh/go-healthwatch/utils/worker"
"github.com/sirupsen/logrus"
)
type Options func(health *HealthCheck)
type HealthCheck struct {
Checks []config.HealthCheckConfig
StatusNotificationWebhook config.ServiceStatusNotificationHook
}
// NewChecker
func NewChecker(options ...Options) *HealthCheck {
checker := &HealthCheck{}
for _, opt := range options {
opt(checker)
}
return checker
}
// AddIntegrations
func WithIntegrations(checks []config.HealthCheckConfig) Options {
return func(health *HealthCheck) {
health.Checks = checks
}
}
// WithServiceFailureHandler
func WithServiceStatusWebHook(handler config.ServiceStatusNotificationHook) Options {
return func(health *HealthCheck) {
health.StatusNotificationWebhook = handler
}
}
// Append new integration
func (health *HealthCheck) AddIntegration(integration config.HealthCheckConfig) *HealthCheck {
health.Checks = append(health.Checks, integration)
return health
}
// Custom checking
func (health *HealthCheck) AddCheck(name string, callback config.CustomHandler) {
config.CustomHandlerMap[name] = callback
}
// Check the services status integrated
func (health *HealthCheck) Check() {
workers := worker.NewTasks()
for _, integration := range health.Checks {
srv := service.InitService(integration, health.StatusNotificationWebhook)
if err := srv.Validate(); err != nil {
logrus.Errorf("[%v] service validation failed : %v", integration.GetName(), err)
}
workers = workers.Add(integration.GetName(), srv, integration.Interval)
}
workers.Start(context.Background())
}
func main() {
checker := NewChecker(
WithIntegrations([]config.HealthCheckConfig{
{
Name: "profile-api",
URL: "https://profile-dev.my.mtn.com/api/v1/health",
Type: constants.External,
StatusCode: http.StatusOK,
Interval: time.Second * 1,
},
}),
// WithServiceStatusWebHook(func(check config.HealthCheckConfig, statusCode constants.HealthCheckStatus, err errors.Error) {
// switch statusCode {
// case constants.Success:
// logrus.Infof("Custom Handler [%v] health check success\n", check.GetName())
// case constants.Failure:
// logrus.Errorf("Custom Handler [%v] service check failing due to : %v", check.GetName(), err.Reason())
// }
// }),
)
checker.AddCheck("profile-api", func(check config.HealthCheckConfig) errors.Error {
return errors.New("trigger-failure", "")
})
checker.Check()
}