-
Notifications
You must be signed in to change notification settings - Fork 7
/
health.go
53 lines (47 loc) · 1.14 KB
/
health.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
package main
import (
"context"
"fmt"
"net"
"net/http"
"os/exec"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
)
func addHealthHandlers(router *httprouter.Router) {
router.GET("/health", makeRequestHandlerWithContext(handleHealth))
}
func handleHealth(ctx context.Context, w http.ResponseWriter, _ httprouter.Params) error {
// This is a blind yes/no response
var err error
if config.EnhancedCheck {
_, err = getUsers(ctx, db)
if err != nil {
return errors.Wrap(err, "PGbouncer enhanced health check failed")
}
} else {
err = probeLocalPort(config.PGBouncerPort)
if err != nil {
return errors.Wrap(err, "PGbouncer port probe check failed")
}
}
if config.CheckDDAgent {
return getAgentHealth(ctx)
}
return nil
}
func probeLocalPort(port int) error {
conn, err := net.Dial("tcp", fmt.Sprintf(":%d", config.PGBouncerPort))
if err == nil {
conn.Close()
}
return err
}
func getAgentHealth(ctx context.Context) error {
psCmd := exec.CommandContext(ctx, "sudo", "-n", "datadog-agent", "health")
err := psCmd.Run()
if err != nil {
return errors.Wrap(err, "Datadog agent health command failed")
}
return nil
}