forked from 766b/chi-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (36 loc) · 1.19 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
package main
import (
"fmt"
"math/rand"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus/promhttp"
chiprometheus "github.com/tommyo/chi-prometheus"
)
func main() {
n := chi.NewRouter()
m := chiprometheus.NewMiddleware("serviceName")
// if you want to use other buckets than the default (300, 1200, 5000) you can run:
// m := negroniprometheus.NewMiddleware("serviceName", 400, 1600, 700)
n.Use(m)
n.Handle("/metrics", promhttp.Handler())
n.Get("/ok", func(w http.ResponseWriter, r *http.Request) {
sleep := rand.Intn(4999) + 1
time.Sleep(time.Duration(sleep) * time.Millisecond)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "slept %d milliseconds\n", sleep)
})
n.Get("/echo/{content}", func(w http.ResponseWriter, r *http.Request) {
content := chi.URLParam(r, "content")
sleep := rand.Intn(4999) + 1
time.Sleep(time.Duration(sleep) * time.Millisecond)
w.Write([]byte(fmt.Sprintf("echo: %v", content)))
fmt.Fprintf(w, "slept %d milliseconds\n", sleep)
})
n.Get("/notfound", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, "not found")
})
http.ListenAndServe(":3000", n)
}