-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (142 loc) · 3.95 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
addr = ":8080"
dataDir = "./data"
webDir = "./web"
promNamespace = "quiz"
)
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("[%s] %s %s\n", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func handleError(status int, msg string, w http.ResponseWriter) error {
w.WriteHeader(status)
w.Write([]byte(msg))
return fmt.Errorf(msg)
}
func goToSleepIfNeeded(w http.ResponseWriter, r *http.Request) error {
s := r.URL.Query().Get("sleep")
if s != "" {
i, err := strconv.Atoi(s)
if err != nil {
return handleError(400, "only integers allowed with 'sleep'", w)
}
time.Sleep(time.Duration(i) * time.Millisecond)
}
return nil
}
func forceStatusIfNeeded(w http.ResponseWriter, r *http.Request) error {
s := r.URL.Query().Get("forceStatus")
if s != "" {
i, err := strconv.Atoi(s)
if err != nil {
return err
}
return handleError(i, fmt.Sprintf("status forced to %d\n", i), w)
}
return nil
}
// handlers
func empty(w http.ResponseWriter, r *http.Request) {
w.Write([]byte{})
}
func ping(w http.ResponseWriter, r *http.Request) {
if err := goToSleepIfNeeded(w, r); err != nil {
return
}
if err := forceStatusIfNeeded(w, r); err != nil {
return
}
w.Write([]byte("pong\n"))
}
func file(w http.ResponseWriter, r *http.Request) {
}
func main() {
// RED metrics
inFlightReqGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: promNamespace,
Name: "api_in_flight_requests",
Help: "A gauge of requests currently being served by the wrapped handler.",
})
reqCounter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: promNamespace,
Name: "api_requests_total",
Help: "A counter for requests.",
},
[]string{"code", "method"},
)
reqDuration := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: promNamespace,
Name: "api_requests_duration_seconds",
Help: "A histogram of latencies.",
Buckets: []float64{.25, .5, 1, 2.5, 5, 10},
},
[]string{"code", "method"},
)
// quiz metrics
visitCounter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: promNamespace,
Name: "visit_counter",
Help: "A counter for visits to the quiz.",
},
[]string{},
)
answerCounter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: promNamespace,
Name: "quiz_answer_event",
Help: "A counter for answers to the quiz.",
},
[]string{"question", "result"},
)
answerHistogram := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: promNamespace,
Name: "quiz_result",
Help: "A histogram of quiz results.",
Buckets: []float64{0, 1, 2, 3, 4, 5, 6},
},
[]string{},
)
prometheus.MustRegister(inFlightReqGauge, reqCounter, reqDuration, visitCounter, answerCounter, answerHistogram)
// instrumentation chains
instrumentHandler := func(handler http.Handler) http.Handler {
return promhttp.InstrumentHandlerInFlight(
inFlightReqGauge,
promhttp.InstrumentHandlerDuration(
reqDuration,
promhttp.InstrumentHandlerCounter(
reqCounter,
handler,
),
),
)
}
// router handlers
http.Handle("/", instrumentHandler(presentQuiz(&quizMetrics{
visitCounter: visitCounter,
})))
http.Handle("/answer", instrumentHandler(answerQuiz(&quizMetrics{
answerCounter: answerCounter,
answerHistogram: answerHistogram,
})))
http.Handle("/images/", instrumentHandler(http.StripPrefix("/images/", http.FileServer(http.Dir(dataDir)))))
http.Handle("/ping", instrumentHandler(http.HandlerFunc(ping)))
http.Handle("/metrics", promhttp.Handler())
log.Println("Waiting for requests on ", addr)
log.Fatal(http.ListenAndServe(addr, logRequest(http.DefaultServeMux)))
}