-
Notifications
You must be signed in to change notification settings - Fork 168
/
handler.go
126 lines (100 loc) · 3.24 KB
/
handler.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package health
import (
"encoding/json"
"fmt"
gosundheit "github.com/AppsFlyer/go-sundheit"
"github.com/openziti/xweb/v2"
"github.com/sirupsen/logrus"
"net/http"
"strings"
"time"
)
const (
Binding = "health-checks"
)
var _ xweb.ApiHandlerFactory = &HealthCheckApiFactory{}
func NewHealthCheckApiFactory(healthChecker gosundheit.Health) *HealthCheckApiFactory {
return &HealthCheckApiFactory{
healthChecker: healthChecker,
}
}
type HealthCheckApiFactory struct {
healthChecker gosundheit.Health
}
func (factory HealthCheckApiFactory) Validate(*xweb.InstanceConfig) error {
return nil
}
func (factory HealthCheckApiFactory) Binding() string {
return Binding
}
func (factory HealthCheckApiFactory) New(_ *xweb.ServerConfig, options map[interface{}]interface{}) (xweb.ApiHandler, error) {
return &HealthCheckApiHandler{
healthChecker: factory.healthChecker,
options: options,
}, nil
}
type HealthCheckApiHandler struct {
options map[interface{}]interface{}
healthChecker gosundheit.Health
}
func (self *HealthCheckApiHandler) Binding() string {
return Binding
}
func (self *HealthCheckApiHandler) Options() map[interface{}]interface{} {
return self.options
}
func (self *HealthCheckApiHandler) RootPath() string {
return "/health-checks"
}
func (self *HealthCheckApiHandler) IsHandler(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, self.RootPath())
}
func (self *HealthCheckApiHandler) ServeHTTP(w http.ResponseWriter, request *http.Request) {
output := map[string]interface{}{}
output["meta"] = map[string]interface{}{}
data := map[string]interface{}{}
output["data"] = data
w.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
results, healthy := self.healthChecker.Results()
data["healthy"] = healthy
var checks []map[string]interface{}
shortFormat := request.URL.Query().Get("type") == "short"
for id, result := range results {
check := map[string]interface{}{}
checks = append(checks, check)
check["id"] = id
check["healthy"] = result.IsHealthy()
if !shortFormat {
check["lastCheckDuration"] = fmt.Sprintf("%v", result.Duration)
check["lastCheckTime"] = result.Timestamp.UTC().Format(time.RFC3339)
if result.Error != nil {
check["err"] = result.Error
check["consecutiveFailures"] = result.ContiguousFailures
}
if result.TimeOfFirstFailure != nil {
check["failingSince"] = result.TimeOfFirstFailure.UTC().Format(time.RFC3339)
}
if result.Details != "didn't run yet" {
check["details"] = result.Details
}
}
}
data["checks"] = checks
if err := encoder.Encode(output); err != nil {
logrus.WithError(err).Error("failure encoding health check results")
}
}