-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetricsd.go
74 lines (66 loc) · 1.88 KB
/
metricsd.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
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/KawaiiDevs/kawaii-metrics/prometheus"
prom "github.com/prometheus/client_golang/prometheus"
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
// Message contains some information about the command used
type Message struct {
CommandName string `json:"command_name"`
IsNSFW bool `json:"is_nsfw"`
}
var mainLog = log.New()
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
// HandleWS handles /ws endpoint
func HandleWS(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
mainLog.WithError(err).Errorln("encountered error while trying to upgrade /ws")
return
}
defer conn.Close()
// begin main loop
for {
message := &Message{}
err = conn.ReadJSON(message)
if err != nil {
mainLog.WithError(err).Errorln("encountered error while trying to read json")
return
}
labels := prom.Labels{"name": message.CommandName}
if message.IsNSFW {
prometheus.NSFWCommandsTotal.With(labels).Inc()
} else {
prometheus.NormalCommandsTotal.With(labels).Inc()
}
}
}
// HandleMetrics handles /metrics endpoint
func HandleMetrics(w http.ResponseWriter, r *http.Request) {
contentType, err := prometheus.WriteMetrics(w, r.Header.Get("Accept"))
if err != nil {
mainLog.WithError(err).Errorln("failed to generate prom metrics")
w.WriteHeader(http.StatusInternalServerError)
w.Header().Add("Content-Type", "text/plain; charset=utf8")
fmt.Fprint(w, "500 Internal Server Error")
return
}
// w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", contentType)
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/ws", HandleWS)
r.HandleFunc("/prometheus/metrics", HandleMetrics)
err := http.ListenAndServe("0.0.0.0:8080", r) // TODO
if err != nil {
mainLog.WithError(err).Errorln("couldn't listen")
}
}