-
Notifications
You must be signed in to change notification settings - Fork 51
/
canary.go
49 lines (38 loc) · 1.24 KB
/
canary.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
package canary
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/openshift/openshift-azure/pkg/util/log"
)
const externalMountPoint = "/data"
type ping struct{}
var _ http.Handler = &ping{}
func start(cfg *cmdConfig) error {
logrus.SetLevel(log.SanitizeLogLevel(cfg.LogLevel))
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
log := logrus.NewEntry(logrus.StandardLogger())
log.Print("canary pod starting")
p := ping{}
http.Handle("/", prometheus.InstrumentHandler("webkv", &p))
http.Handle("/metrics", promhttp.Handler())
http.Handle("/healthz/ready", http.HandlerFunc(p.readyHandler))
return http.ListenAndServe(":8080", nil)
}
func (p *ping) ServeHTTP(w http.ResponseWriter, r *http.Request) {
str := fmt.Sprintf("%s/%s : %s. Node: %s", r.RemoteAddr, r.URL, r.UserAgent(), os.Getenv("HOSTNAME"))
err := ioutil.WriteFile(path.Join(externalMountPoint, "lastrequest.log"), []byte(str), 0600)
if err != nil {
fmt.Fprintf(w, err.Error())
} else {
fmt.Fprintf(w, str)
}
}
func (p *ping) readyHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}