-
Notifications
You must be signed in to change notification settings - Fork 1
/
prometheus.go
83 lines (68 loc) · 1.75 KB
/
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
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
package main
import (
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
tagNames = []string{
"server",
"scheme",
"method",
"hostname",
"status",
"uri",
"jrpc_method",
}
requestCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "ngx_request_count",
Help: "request count",
}, tagNames)
requestsSizeCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "ngx_request_size_bytes",
Help: "request size in bytes",
}, tagNames)
responseSizeCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "ngx_response_size_bytes",
Help: "response size in bytes",
}, tagNames)
requestDurationHistogramVec = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "ngx_request_duration_seconds",
Help: "request serving time in seconds",
}, tagNames)
)
func init() {
prometheus.MustRegister(
requestCounter,
requestsSizeCounter,
responseSizeCounter,
requestDurationHistogramVec,
)
}
func prometheusMetricsRegister(l *logEntry) {
tags := []string{
l.server,
l.scheme,
l.method,
l.hostname,
l.status,
l.uri,
l.jrpc_method,
}
requestCounter.WithLabelValues(tags...).Inc()
responseSizeCounter.WithLabelValues(tags...).Add(float64(l.bytesSent))
requestsSizeCounter.WithLabelValues(tags...).Add(float64(l.bytesReceived))
requestDurationHistogramVec.WithLabelValues(tags...).Observe(l.duration)
}
func prometheusListener(listen string) {
r := http.NewServeMux()
r.Handle("/metrics", promhttp.Handler())
httpServer := &http.Server{
Addr: listen,
Handler: r,
}
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Failed to listen on %s: %s", listen, err)
}
}