/
metrics.go
177 lines (150 loc) Β· 4.01 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package navigator
import (
"sort"
"sync"
"time"
"github.com/tevino/abool"
"github.com/safing/portbase/api"
"github.com/safing/portbase/metrics"
)
var metricsRegistered = abool.New()
func registerMetrics() (err error) {
// Only register metrics once.
if !metricsRegistered.SetToIf(false, true) {
return nil
}
// Map Stats.
_, err = metrics.NewGauge(
"spn/map/main/latency/all/lowest/seconds",
nil,
getLowestLatency,
&metrics.Options{
Name: "SPN Map Lowest Latency",
Permission: api.PermitUser,
},
)
if err != nil {
return err
}
_, err = metrics.NewGauge(
"spn/map/main/latency/fas/lowest/seconds",
nil,
getLowestLatencyFromFas,
&metrics.Options{
Name: "SPN Map Lowest Latency",
Permission: api.PermitUser,
},
)
if err != nil {
return err
}
_, err = metrics.NewGauge(
"spn/map/main/capacity/all/highest/bytes",
nil,
getHighestCapacity,
&metrics.Options{
Name: "SPN Map Lowest Latency",
Permission: api.PermitUser,
},
)
if err != nil {
return err
}
_, err = metrics.NewGauge(
"spn/map/main/capacity/fas/highest/bytes",
nil,
getHighestCapacityFromFas,
&metrics.Options{
Name: "SPN Map Lowest Latency",
Permission: api.PermitUser,
},
)
if err != nil {
return err
}
return nil
}
var (
mapStats *mapMetrics
mapStatsExpires time.Time
mapStatsLock sync.Mutex
mapStatsTTL = 55 * time.Second
)
type mapMetrics struct {
lowestLatency float64
lowestForeignASLatency float64
highestCapacity float64
highestForeignASCapacity float64
}
func getLowestLatency() float64 { return getMapStats().lowestLatency }
func getLowestLatencyFromFas() float64 { return getMapStats().lowestForeignASLatency }
func getHighestCapacity() float64 { return getMapStats().highestCapacity }
func getHighestCapacityFromFas() float64 { return getMapStats().highestForeignASCapacity }
func getMapStats() *mapMetrics {
mapStatsLock.Lock()
defer mapStatsLock.Unlock()
// Return cache if still valid.
if time.Now().Before(mapStatsExpires) {
return mapStats
}
// Refresh.
mapStats = &mapMetrics{}
// Get all pins and home.
list := Main.pinList(true)
home, _ := Main.GetHome()
// Return empty stats if we have incomplete data.
if len(list) <= 1 || home == nil {
mapStatsExpires = time.Now().Add(mapStatsTTL)
return mapStats
}
// Sort by latency.
sort.Sort(sortByLowestMeasuredLatency(list))
// Get lowest latency.
lowestLatency, _ := list[0].measurements.GetLatency()
mapStats.lowestLatency = lowestLatency.Seconds()
// Find best foreign AS latency.
bestForeignASPin := findFirstForeignASStatsPin(home, list)
if bestForeignASPin != nil {
lowestForeignASLatency, _ := bestForeignASPin.measurements.GetLatency()
mapStats.lowestForeignASLatency = lowestForeignASLatency.Seconds()
}
// Sort by capacity.
sort.Sort(sortByHighestMeasuredCapacity(list))
// Get highest capacity.
highestCapacity, _ := list[0].measurements.GetCapacity()
mapStats.highestCapacity = float64(highestCapacity) / 8
// Find best foreign AS capacity.
bestForeignASPin = findFirstForeignASStatsPin(home, list)
if bestForeignASPin != nil {
highestForeignASCapacity, _ := bestForeignASPin.measurements.GetCapacity()
mapStats.highestForeignASCapacity = float64(highestForeignASCapacity) / 8
}
mapStatsExpires = time.Now().Add(mapStatsTTL)
return mapStats
}
func findFirstForeignASStatsPin(home *Pin, list []*Pin) *Pin {
// Find best foreign AS latency.
for _, pin := range list {
compared := false
// Skip if IPv4 AS matches.
if home.LocationV4 != nil && pin.LocationV4 != nil {
if home.LocationV4.AutonomousSystemNumber == pin.LocationV4.AutonomousSystemNumber {
continue
}
compared = true
}
// Skip if IPv6 AS matches.
if home.LocationV6 != nil && pin.LocationV6 != nil {
if home.LocationV6.AutonomousSystemNumber == pin.LocationV6.AutonomousSystemNumber {
continue
}
compared = true
}
// Skip if no data was compared
if !compared {
continue
}
return pin
}
return nil
}