-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmonitoring.go
More file actions
124 lines (99 loc) · 2.85 KB
/
monitoring.go
File metadata and controls
124 lines (99 loc) · 2.85 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package util
import (
"bufio"
"errors"
"fmt"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net"
"net/http"
"strconv"
"time"
)
type responseWriter struct {
http.ResponseWriter
statusCode int
}
func NewResponseWriter(w http.ResponseWriter) *responseWriter {
return &responseWriter{w, http.StatusOK}
}
func (rw *responseWriter) WriteHeader(code int) {
rw.statusCode = code
rw.ResponseWriter.WriteHeader(code)
}
func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error){
h, ok := rw.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("hijack error")
}else{
return h.Hijack()
}
}
var (
totalRequests, requestInitiators, responseStatus *prometheus.CounterVec
httpDuration *prometheus.HistogramVec
)
func InitMonitoring(ms string, router *mux.Router) {
if !DockerChecker() {
return
}
initMonitoringVars(ms)
prometheus.Register(totalRequests)
prometheus.Register(requestInitiators)
prometheus.Register(responseStatus)
prometheus.Register(httpDuration)
promRouter := mux.NewRouter().StrictSlash(true)
router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := mux.CurrentRoute(r)
path, _ := route.GetPathTemplate()
totalRequests.WithLabelValues(path).Inc()
rw := NewResponseWriter(w)
start := time.Now()
next.ServeHTTP(rw, r)
elapsed := time.Since(start)
statusCode := rw.statusCode
httpDuration.WithLabelValues(path, strconv.Itoa(statusCode)).Observe(elapsed.Seconds())
responseStatus.WithLabelValues(path, strconv.Itoa(statusCode)).Inc()
initiator := rw.Header().Get("initiator")
requestInitiators.WithLabelValues(path, initiator).Inc()
})
})
promRouter.Path("/metrics").Handler(promhttp.Handler())
go func() {
err := http.ListenAndServe(":9090", promRouter)
if err != nil {
fmt.Println("FAILED TO START PROMETHEUS METRICS!")
return
}
}()
}
func initMonitoringVars(ms string) {
totalRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "nistagram_" + ms + "_http_requests_total",
Help: "Number of requests.",
},
[]string{"path"},
)
requestInitiators = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "nistagram_http_requests_initiators",
Help: "Initiators of HTTP requests.",
},
[]string{"path", "initiator"},
)
responseStatus = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "nistagram_" + ms + "_http_response_status",
Help: "Status of HTTP response",
},
[]string{"path", "status"},
)
httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "nistagram_" + ms + "_http_response_time_seconds",
Help: "Duration of HTTP requests.",
}, []string{"path", "status"})
}