This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 672
/
metrics.go
156 lines (141 loc) · 5.21 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/weaveworks/weave/ipam"
"github.com/weaveworks/weave/nameserver"
"github.com/weaveworks/weave/net/address"
weave "github.com/weaveworks/weave/router"
)
func metricsHandler(router *weave.NetworkRouter, allocator *ipam.Allocator, ns *nameserver.Nameserver, dnsserver *nameserver.DNSServer) http.Handler {
reg := prometheus.NewRegistry()
reg.MustRegister(prometheus.NewProcessCollector(os.Getpid(), ""))
reg.MustRegister(newMetrics(router, allocator, ns, dnsserver))
return promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
}
type collector struct {
router *weave.NetworkRouter
allocator *ipam.Allocator
ns *nameserver.Nameserver
dnsserver *nameserver.DNSServer
}
type metric struct {
*prometheus.Desc
Collect func(WeaveStatus, *prometheus.Desc, chan<- prometheus.Metric)
}
func desc(fqName, help string, variableLabels ...string) *prometheus.Desc {
return prometheus.NewDesc(fqName, help, variableLabels, prometheus.Labels{})
}
func intGauge(desc *prometheus.Desc, val int, labels ...string) prometheus.Metric {
return prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, float64(val), labels...)
}
func uint64Counter(desc *prometheus.Desc, val uint64, labels ...string) prometheus.Metric {
return prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(val), labels...)
}
var metrics = []metric{
{desc("weave_connections", "Number of peer-to-peer connections.", "state"),
func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
counts := make(map[string]int)
for _, conn := range s.Router.Connections {
counts[conn.State]++
}
for _, state := range allConnectionStates {
ch <- intGauge(desc, counts[state], state)
}
}},
{desc("weave_connection_terminations_total", "Number of peer-to-peer connections terminated."),
func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
ch <- uint64Counter(desc, uint64(s.Router.TerminationCount))
}},
{desc("weave_ips", "Number of IP addresses.", "state"),
func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
if s.IPAM != nil {
ch <- intGauge(desc, s.IPAM.ActiveIPs, "local-used")
}
}},
{desc("weave_max_ips", "Size of IP address space used by allocator."),
func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
if s.IPAM != nil {
ch <- intGauge(desc, s.IPAM.RangeNumIPs)
}
}},
{desc("weave_dns_entries", "Number of DNS entries."),
func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
if s.DNS != nil {
ch <- intGauge(desc, countDNSEntriesForPeer(s.Router.Name, s.DNS.Entries))
}
}},
{desc("weave_flows", "Number of FastDP flows."),
func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
if metrics := fastDPMetrics(s); metrics != nil {
ch <- intGauge(desc, metrics.Flows)
}
}},
{desc("weave_ipam_unreachable_count", "Number of unreachable peers."),
func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
var count int
for _, entry := range summariseIpamStats(s.IPAM) {
if !entry.reachable {
count++
}
}
ch <- intGauge(desc, count)
}},
{desc("weave_ipam_unreachable_percentage", "Percentage of IP addresses owned by unreachable peers."),
func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
var totalUnreachable uint32
for _, entry := range summariseIpamStats(s.IPAM) {
if !entry.reachable {
totalUnreachable += entry.ips
}
}
percentage := float64(totalUnreachable) * 100.0 / float64(s.IPAM.RangeNumIPs)
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, percentage)
}},
{desc("weave_ipam_pending_allocates", "Number of pending allocates."),
func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
if s.IPAM != nil {
ch <- intGauge(desc, len(s.IPAM.PendingAllocates))
}
}},
{desc("weave_ipam_pending_claims", "Number of pending claims."),
func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
if s.IPAM != nil {
ch <- intGauge(desc, len(s.IPAM.PendingClaims))
}
}},
}
func fastDPMetrics(s WeaveStatus) *weave.FastDPMetrics {
if diagMap, ok := s.Router.OverlayDiagnostics.(map[string]interface{}); ok {
if diag, ok := diagMap["fastdp"]; ok {
if fastDPStats, ok := diag.(weave.FastDPStatus); ok {
return fastDPStats.Metrics().(*weave.FastDPMetrics)
}
}
}
return nil
}
func newMetrics(router *weave.NetworkRouter, allocator *ipam.Allocator, ns *nameserver.Nameserver, dnsserver *nameserver.DNSServer) *collector {
return &collector{
router: router,
allocator: allocator,
ns: ns,
dnsserver: dnsserver,
}
}
func (m *collector) Collect(ch chan<- prometheus.Metric) {
status := WeaveStatus{
Router: weave.NewNetworkRouterStatus(m.router),
IPAM: ipam.NewStatus(m.allocator, address.CIDR{}),
DNS: nameserver.NewStatus(m.ns, m.dnsserver)}
for _, metric := range metrics {
metric.Collect(status, metric.Desc, ch)
}
}
func (m *collector) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range metrics {
ch <- metric.Desc
}
}