-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
63 lines (51 loc) · 1.62 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
package main
import (
"log"
"net/http"
"time"
"github.com/armon/go-metrics"
"github.com/armon/go-metrics/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func monitorRED(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
record := &statusRecorder{ResponseWriter: w, Status: "success"}
next.ServeHTTP(record, r)
labels := []metrics.Label{{Name: "method", Value: r.Method}, {Name: "route", Value: r.RequestURI}, {Name: "status", Value: record.Status}}
duration := time.Since(start)
metrics.AddSampleWithLabels([]string{"request_duration"}, float32(duration.Milliseconds()), labels)
log.Printf("%v %v %v\n", r.Method, r.RequestURI, duration)
})
}
type statusRecorder struct {
http.ResponseWriter
Status string
}
func (r *statusRecorder) WriteHeader(status int) {
if status != http.StatusOK {
r.Status = "error"
}
r.ResponseWriter.WriteHeader(status)
}
func main() {
mon, _ := prometheus.NewPrometheusSink()
metrics.NewGlobal(metrics.DefaultConfig("myapp"), mon)
// rest service with prometheus endpoint
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
w.Write([]byte(`hello`))
})
mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
w.WriteHeader(http.StatusInternalServerError)
})
mux.Handle("/metrics", promhttp.Handler())
server := &http.Server{
Addr: ":8080",
Handler: monitorRED(mux),
}
log.Println("Starting server at", server.Addr)
log.Fatal(server.ListenAndServe())
}