-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_check.go
88 lines (79 loc) · 2.34 KB
/
service_check.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
package cuckoo
import (
"time"
"github.com/full360/cuckoo/cloudwatch"
"github.com/full360/cuckoo/consul"
"github.com/full360/cuckoo/log"
)
// serviceCheckConfig is used to represent the configuration of a service check
type ServiceCheckConfig struct {
Name string
Tag string
MetricName string
MetricNamespace string
BlockTime time.Duration
Logger *log.Logger
}
// serviceCheck is used to represent a single service check with consul,
// cloudwatch and a logger
type serviceCheck struct {
consul *consul.Check
metric *cloudwatch.Metric
logger *log.Logger
}
// defaultServiceCheck returns a defaul service check config
func DefaultServiceCheck() *ServiceCheckConfig {
return &ServiceCheckConfig{
Name: "service",
Tag: "tag",
MetricName: "service_monitoring",
MetricNamespace: "microservices",
BlockTime: 10 * time.Minute,
Logger: log.NewLogger(),
}
}
// newServiceCheck returns a new service check
func NewServiceCheck(svcConfig *ServiceCheckConfig) (*serviceCheck, error) {
consul, err := consul.NewCheck(&consul.CheckConfig{
Service: svcConfig.Name,
Tag: svcConfig.Tag,
PassingOnly: true,
BlockTime: svcConfig.BlockTime,
})
if err != nil {
return nil, err
}
svcCheck := &serviceCheck{
consul: consul,
metric: cloudwatch.NewMetric(&cloudwatch.MetricConfig{
Name: svcConfig.MetricName,
Namespace: svcConfig.MetricNamespace,
Service: &cloudwatch.Service{
Name: svcConfig.Name,
Env: svcConfig.Tag,
},
Value: 0,
}),
logger: svcConfig.Logger,
}
return svcCheck, nil
}
// Check checks if a service is healthy and posts that data to a Cloudwatch
// metric based on the service name and environment
func (sc *serviceCheck) Check() error {
count, qm, err := sc.consul.Healthy()
if err != nil {
return err
}
// debug logging for Consul request
sc.logger.Debug("Consul Query metadata, Request Time: %s, Last Index: %d", qm.RequestTime, qm.LastIndex)
// Set the last response index as the wait index for the next request to
// successfully do a blocking query
sc.consul.QueryOptions.WaitIndex = qm.LastIndex
sc.logger.Info("Service count: %d, with name: %s and tag: %s", count, sc.consul.Config.Service, sc.consul.Config.Tag)
_, err = sc.metric.Put(float64(count))
if err != nil {
return err
}
return nil
}