forked from olzhasar/gochat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
47 lines (41 loc) · 1.18 KB
/
metrics.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
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
)
var (
roomCount = promauto.NewGauge(prometheus.GaugeOpts{
Name: "room_count",
Help: "The number of active rooms",
})
clientCount = promauto.NewGauge(prometheus.GaugeOpts{
Name: "client_count",
Help: "The number of active clients",
})
messagesReceivedCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "messages_received",
Help: "The number of messages received from all connections",
})
messagesBroadcastedCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "messages_broadcasted",
Help: "The number of messages broadcasted to all connections",
})
)
type MetricsServer struct {
port string
}
func NewMetricsServer(port string) *MetricsServer {
return &MetricsServer{port: port}
}
func (s *MetricsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
promhttp.Handler().ServeHTTP(w, r)
}
func (s *MetricsServer) Run() {
go func() {
log.Println("Starting metrics server on port", s.port)
log.Fatal(http.ListenAndServe(":"+s.port, s))
}()
}