-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
healthprobe.go
179 lines (146 loc) · 4.27 KB
/
healthprobe.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.
// Package healthprobeimpl implements the healthprobe component interface
package healthprobeimpl
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"runtime"
"time"
"go.uber.org/fx"
healthprobeComponent "github.com/DataDog/datadog-agent/comp/core/healthprobe"
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/pkg/status/health"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/gorilla/mux"
)
const defaultTimeout = time.Second
// Module defines the fx options for this component.
func Module() fxutil.Module {
return fxutil.Component(
fx.Provide(newHealthProbe),
)
}
type dependencies struct {
fx.In
Log log.Component
Options healthprobeComponent.Options
}
type healthprobe struct {
options healthprobeComponent.Options
log log.Component
server *http.Server
listener net.Listener
}
func (h *healthprobe) start() error {
h.log.Debugf("Health check listening on port %d", h.options.Port)
go h.server.Serve(h.listener) //nolint:errcheck
return nil
}
func (h *healthprobe) stop() error {
timeout, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
h.log.Debug("Stopping Health check")
return h.server.Shutdown(timeout) //nolint:errcheck
}
func newHealthProbe(lc fx.Lifecycle, deps dependencies) (healthprobeComponent.Component, error) {
healthPort := deps.Options.Port
if healthPort <= 0 {
return nil, nil
}
ln, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%v", healthPort))
if err != nil {
return nil, err
}
server := buildServer(deps.Options, deps.Log)
probe := &healthprobe{
options: deps.Options,
log: deps.Log,
server: server,
listener: ln,
}
// We rely on FX to start and stop the metadata runner
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return probe.start()
},
OnStop: func(ctx context.Context) error {
return probe.stop()
},
})
return probe, nil
}
type liveHandler struct {
logsGoroutines bool
log log.Component
}
func (lh liveHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
healthHandler(lh.logsGoroutines, lh.log, health.GetLiveNonBlocking, w, r)
}
type readyHandler struct {
logsGoroutines bool
log log.Component
}
func (rh readyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
healthHandler(rh.logsGoroutines, rh.log, health.GetReadyNonBlocking, w, r)
}
func buildServer(options healthprobeComponent.Options, log log.Component) *http.Server {
r := mux.NewRouter()
liveHandler := liveHandler{
logsGoroutines: options.LogsGoroutines,
log: log,
}
readyHandler := readyHandler{
logsGoroutines: options.LogsGoroutines,
log: log,
}
r.Handle("/live", liveHandler)
r.Handle("/ready", readyHandler)
// Default route for backward compatibility
r.NewRoute().Handler(liveHandler)
return &http.Server{
Handler: r,
ReadTimeout: defaultTimeout,
ReadHeaderTimeout: defaultTimeout,
WriteTimeout: defaultTimeout,
}
}
func healthHandler(logsGoroutines bool, log log.Component, getStatusNonBlocking func() (health.Status, error), w http.ResponseWriter, _ *http.Request) {
health, err := getStatusNonBlocking()
if err != nil {
body, _ := json.Marshal(map[string]string{"error": err.Error()})
http.Error(w, string(body), http.StatusInternalServerError)
return
}
if len(health.Unhealthy) > 0 {
w.WriteHeader(http.StatusInternalServerError)
log.Infof("Healthcheck failed on: %v", health.Unhealthy)
if logsGoroutines {
log.Infof("Goroutines stack: \n%s\n", allStack())
}
}
jsonHealth, err := json.Marshal(health)
if err != nil {
log.Errorf("Error marshalling status. Error: %v", err)
body, _ := json.Marshal(map[string]string{"error": err.Error()})
http.Error(w, string(body), 500)
return
}
w.Write(jsonHealth)
}
// inspired by https://golang.org/src/runtime/debug/stack.go?s=587:606#L11
func allStack() []byte {
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
return buf[:n]
}
buf = make([]byte, 2*len(buf))
}
}