-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (83 loc) · 2.3 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
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
totalRequestCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Number of get request",
},
[]string{"path"},
)
responseStatus = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "response_status_total",
Help: "Status of HTTP response",
},
[]string{"status"},
)
httpDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_response_time_seconds",
Help: "Duration of HTTP Requests",
// Buckets: prometheus.DefBuckets,
},
[]string{"path"},
)
)
func registerCustomMetrics() error {
err := prometheus.Register(totalRequestCount)
if err != nil {
return fmt.Errorf("failed to register %s metric: %w", "Total Request Count", err)
}
err = prometheus.Register(responseStatus)
if err != nil {
return fmt.Errorf("failed to register %s metric: %w", "Response Status Count", err)
}
err = prometheus.Register(httpDuration)
if err != nil {
return fmt.Errorf("failed to register %s metric: %w", "Http duration histogram", err)
}
return nil
}
func prometheusMW(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := mux.CurrentRoute(r)
path, _ := route.GetPathTemplate()
timer := prometheus.NewTimer(httpDuration.WithLabelValues(path))
next.ServeHTTP(w, r)
fmt.Println("Entire Map: ", w.Header())
// Still not sure how to catch the response status without creating my own response writer
responseStatus.WithLabelValues(strconv.Itoa(http.StatusOK)).Inc()
totalRequestCount.WithLabelValues(path).Inc()
timer.ObserveDuration()
})
}
func main() {
err := registerCustomMetrics()
if err != nil {
// this only makes sense for our exercise
log.Fatal(err)
}
r := mux.NewRouter()
r.Use(prometheusMW)
// Reporting metrics for Prometheus using default handler
r.Path("/metrics").Handler(promhttp.Handler())
r.PathPrefix("/").
Handler(
http.FileServer(http.Dir("./static/")),
)
fmt.Println("Starting App on port 2112")
err = http.ListenAndServe(":2112", r)
if err != nil {
// Should kill it if something goes wrong
log.Fatal(err)
}
}