-
Notifications
You must be signed in to change notification settings - Fork 1
/
prometheus.go
35 lines (30 loc) · 910 Bytes
/
prometheus.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
package advanced_metrics
import (
"net/http"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func StartServer(port int) {
if _, ok := counters[port]; !ok {
counters[port] = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "caddy_advanced_metrics_requests",
Help: "Number of requests",
},
[]string{"method", "path", "status", "host"},
)
reg := prometheus.NewRegistry()
reg.MustRegister(counters[port])
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
go http.ListenAndServe(":"+strconv.Itoa(port), nil)
}
}
func HandleRequest(port int, method string, path string, status int, host string) {
if _, ok := counters[port]; ok {
counters[port].WithLabelValues(method, path, strconv.Itoa(status), host).Inc()
}
}
var (
counters = make(map[int]*prometheus.CounterVec)
)