-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
47 lines (37 loc) · 1010 Bytes
/
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
package main
import (
"math/rand"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func init() {
// must register counter on init
prometheus.MustRegister(getBookCounter)
}
func main() {
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", bookHandler)
println("listening..")
http.ListenAndServe(":5005", nil)
}
// create a new counter vector
var getBookCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_request_get_books_count", // metric name
Help: "Number of get_books request.",
},
[]string{"status"}, // labels
)
func bookHandler(w http.ResponseWriter, r *http.Request) {
var status string
defer func() {
// increment the counter on defer func
getBookCounter.WithLabelValues(status).Inc()
}()
var allStatus []string = []string{"success", "error"}
var min, max int = 0, 1
var index = rand.Intn(max-min+1) + min
status = allStatus[index]
w.Write([]byte(status))
}