-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (122 loc) · 3.27 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var (
heartrate chan time.Duration
heart = promauto.NewCounter(prometheus.CounterOpts{
Name: "kdummy_heartbeat_total",
Help: "The total number of heartbeats",
})
)
// Health holds the health status
type Health struct {
status int
msg string
}
// main metric counter for application
func heartBeat(ch chan time.Duration, rate time.Duration) {
ticker := time.NewTicker(rate)
defer ticker.Stop()
var newRate time.Duration
for {
select {
case newRate = <-ch:
ticker.Reset(newRate)
log.WithFields(log.Fields{
"rate": int(newRate.Seconds()),
}).Info("Set new heart rate.")
case <-ticker.C:
heart.Inc()
log.WithFields(log.Fields{
"rate": int(newRate.Seconds()),
}).Info("This is my heartbeat song and I'm gonna play it.")
}
}
}
// simple health check
func getHealth(w http.ResponseWriter, r *http.Request) {
log.Debug("Received a health check request.")
health := Health{
status: 200,
msg: "OK",
}
w.Header().Set("Content-Type", "application/json")
healthJSON, err := json.Marshal(health)
if err != nil {
log.Error("Could not serialize health status into JSON.")
resp := make(map[string]string)
resp["status"] = "500"
resp["msg"] = "Internal Server Error"
respJSON, err := json.Marshal(resp)
if err != nil {
log.Fatal("Can't serialize anything apparently.")
}
w.WriteHeader(http.StatusInternalServerError)
w.Write(respJSON)
return
}
// we're good
w.WriteHeader(http.StatusOK)
w.Write(healthJSON)
return
}
func setHeartRateSeconds(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
if rate, ok := vars["rate"]; ok {
newRate, err := strconv.Atoi(rate)
if err != nil {
log.WithFields(log.Fields{
"rate": rate,
}).Error("Failed to set new heart rate.")
w.WriteHeader(http.StatusBadRequest)
return
}
go func() {
heartrate <- time.Second * time.Duration(newRate)
}()
w.WriteHeader(http.StatusAccepted)
w.Write([]byte("{}"))
return
}
log.Debug("Received an invalid heart rate.")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("{}"))
return
}
func main() {
verbose := flag.Bool("verbose", false, "show debug messages")
listenPort := flag.Int("port", 8080, "listen port for server")
flag.Parse()
log.SetFormatter(&log.JSONFormatter{})
// debug mode
if *verbose {
log.SetLevel(log.DebugLevel)
}
mainMux := mux.NewRouter()
mainMux.HandleFunc("/heart/{rate}", setHeartRateSeconds)
internalMux := mux.NewRouter()
internalMux.Handle("/metrics", promhttp.Handler())
internalMux.HandleFunc("/healthz", getHealth)
// set up heartbeat goroutine
heartrate = make(chan time.Duration, 1)
go heartBeat(heartrate, time.Second*3)
heartrate <- time.Second * 3
go func() {
log.Debug("Listening for internal requests on 9090")
http.ListenAndServe(":9090", internalMux)
}()
log.Info(fmt.Sprintf("Listening for heartbeat changes on %d", *listenPort))
http.ListenAndServe(fmt.Sprintf(":%d", *listenPort), mainMux)
}