This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stats.go
126 lines (104 loc) · 3.88 KB
/
stats.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package network
import (
"context"
"time"
gnatsd "github.com/nats-io/nats-server/v2/server"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
var (
connectionsGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "choria_network_connections",
Help: "Current connections on the network broker",
}, []string{"identity"})
totalConnectionsGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "choria_network_total_connections",
Help: "Total connections received since start",
}, []string{"identity"})
routesGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "choria_network_routes",
Help: "Current active routes to other brokers",
}, []string{"identity"})
remotesGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "choria_network_remotes",
Help: "Current active connections to other brokers",
}, []string{"identity"})
leafsGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "choria_network_leafnode_remotes",
Help: "Current active connections to other leafnodes",
}, []string{"identity"})
inMsgsGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "choria_network_in_msgs",
Help: "Messages received by the network broker",
}, []string{"identity"})
outMsgsGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "choria_network_out_msgs",
Help: "Messages sent by the network broker",
}, []string{"identity"})
inBytesGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "choria_network_in_bytes",
Help: "Total size of messages received by the network broker",
}, []string{"identity"})
outBytesGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "choria_network_out_bytes",
Help: "Total size of messages sent by the network broker",
}, []string{"identity"})
slowConsumerGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "choria_network_slow_consumers",
Help: "Total number of clients who were considered slow consumers",
}, []string{"identity"})
subscriptionsGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "choria_network_subscriptions",
Help: "Number of active subscriptions to subjects on this broker",
}, []string{"identity"})
)
func init() {
prometheus.MustRegister(connectionsGauge)
prometheus.MustRegister(totalConnectionsGauge)
prometheus.MustRegister(routesGauge)
prometheus.MustRegister(remotesGauge)
prometheus.MustRegister(leafsGauge)
prometheus.MustRegister(inMsgsGauge)
prometheus.MustRegister(outMsgsGauge)
prometheus.MustRegister(inBytesGauge)
prometheus.MustRegister(outBytesGauge)
prometheus.MustRegister(slowConsumerGauge)
prometheus.MustRegister(subscriptionsGauge)
}
func (s *Server) getVarz() (*gnatsd.Varz, error) {
return s.gnatsd.Varz(&gnatsd.VarzOptions{})
}
func (s *Server) publishStats(ctx context.Context, interval time.Duration) {
if s.opts.HTTPPort == 0 {
return
}
ticker := time.NewTicker(interval)
for {
select {
case <-ticker.C:
log.Debug("Starting NATS /varz update")
s.updatePrometheus()
case <-ctx.Done():
return
}
}
}
func (s *Server) updatePrometheus() {
varz, err := s.getVarz()
if err != nil {
log.Errorf("Could not publish network broker stats: %s", err)
return
}
i := s.config.Identity
connectionsGauge.WithLabelValues(i).Set(float64(varz.Connections))
totalConnectionsGauge.WithLabelValues(i).Set(float64(varz.TotalConnections))
routesGauge.WithLabelValues(i).Set(float64(varz.Routes))
remotesGauge.WithLabelValues(i).Set(float64(varz.Remotes))
inMsgsGauge.WithLabelValues(i).Set(float64(varz.InMsgs))
outMsgsGauge.WithLabelValues(i).Set(float64(varz.OutMsgs))
inBytesGauge.WithLabelValues(i).Set(float64(varz.InBytes))
outBytesGauge.WithLabelValues(i).Set(float64(varz.OutBytes))
slowConsumerGauge.WithLabelValues(i).Set(float64(varz.SlowConsumers))
subscriptionsGauge.WithLabelValues(i).Set(float64(varz.Subscriptions))
leafsGauge.WithLabelValues(i).Set(float64(varz.Leafs))
}