-
Notifications
You must be signed in to change notification settings - Fork 221
/
server.go
49 lines (40 loc) · 1.09 KB
/
server.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 main
import (
"flag"
"fmt"
"math/rand"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var requests = prometheus.NewCounter(prometheus.CounterOpts{
Name: "requests",
Help: "Number of requests",
})
type handler struct {
successRate float64
}
func (h *handler) HandleRequest(w http.ResponseWriter, req *http.Request) {
requests.Inc()
if rand.Float64() > h.successRate {
time.Sleep(100 * time.Millisecond)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("internal service error"))
return
}
w.Write([]byte("pong"))
}
func init() {
prometheus.MustRegister(requests)
}
func main() {
addr := flag.String("addr", ":8501", "service port to run on")
successRate := flag.Float64("success-rate", 1.0, "service success rate")
flag.Parse()
fmt.Printf("serving on %s with %2f success rate\n", *addr, *successRate)
httpHandler := handler{successRate: *successRate}
http.HandleFunc("/", httpHandler.HandleRequest)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(*addr, nil)
}