metrics.go 6.23 KB
Newer Older
1
/*
2
3
We export metrics in the format specified in our broker spec:
https://gitweb.torproject.org/pluggable-transports/snowflake.git/tree/doc/broker-spec.txt
4
5
*/

Hooman's avatar
Hooman committed
6
package main
7
8

import (
9
10
	"fmt"
	"log"
11
	"math"
12
	"net"
13
	"sort"
14
	"sync"
15
16
17
	"time"
)

18
19
20
21
var (
	once sync.Once
)

22
const metricsResolution = 60 * 60 * 24 * time.Second //86400 seconds
23

24
type CountryStats struct {
25
26
27
28
	standalone map[string]bool
	badge      map[string]bool
	webext     map[string]bool
	unknown    map[string]bool
29
30
31
32
33
34

	natRestricted   map[string]bool
	natUnrestricted map[string]bool
	natUnknown      map[string]bool

	counts map[string]int
35
36
}

37
38
// Implements Observable
type Metrics struct {
39
40
41
42
	logger  *log.Logger
	tablev4 *GeoIPv4Table
	tablev6 *GeoIPv6Table

43
44
45
46
47
48
49
	countryStats                  CountryStats
	clientRoundtripEstimate       time.Duration
	proxyIdleCount                uint
	clientDeniedCount             uint
	clientRestrictedDeniedCount   uint
	clientUnrestrictedDeniedCount uint
	clientProxyMatchCount         uint
50
51
52

	//synchronization for access to snowflake metrics
	lock sync.Mutex
53
54
}

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
type record struct {
	cc    string
	count int
}
type records []record

func (r records) Len() int      { return len(r) }
func (r records) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r records) Less(i, j int) bool {
	if r[i].count == r[j].count {
		return r[i].cc > r[j].cc
	}
	return r[i].count < r[j].count
}

70
func (s CountryStats) Display() string {
71
	output := ""
72
73
74

	// Use the records struct to sort our counts map by value.
	rs := records{}
75
	for cc, count := range s.counts {
76
77
78
79
80
		rs = append(rs, record{cc: cc, count: count})
	}
	sort.Sort(sort.Reverse(rs))
	for _, r := range rs {
		output += fmt.Sprintf("%s=%d,", r.cc, r.count)
81
	}
82
83
84
85
86
87

	// cut off trailing ","
	if len(output) > 0 {
		return output[:len(output)-1]
	}

88
	return output
89
90
}

91
func (m *Metrics) UpdateCountryStats(addr string, proxyType string, natType string) {
92
93
94
95

	var country string
	var ok bool

96
	if proxyType == "standalone" {
97
98
99
		if m.countryStats.standalone[addr] {
			return
		}
100
	} else if proxyType == "badge" {
101
102
103
		if m.countryStats.badge[addr] {
			return
		}
104
	} else if proxyType == "webext" {
105
106
107
108
109
110
111
		if m.countryStats.webext[addr] {
			return
		}
	} else {
		if m.countryStats.unknown[addr] {
			return
		}
112
113
	}

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
	ip := net.ParseIP(addr)
	if ip.To4() != nil {
		//This is an IPv4 address
		if m.tablev4 == nil {
			return
		}
		country, ok = GetCountryByAddr(m.tablev4, ip)
	} else {
		if m.tablev6 == nil {
			return
		}
		country, ok = GetCountryByAddr(m.tablev6, ip)
	}

	if !ok {
		country = "??"
	}

132
	//update map of unique ips and counts
133
	m.countryStats.counts[country]++
134
	if proxyType == "standalone" {
135
		m.countryStats.standalone[addr] = true
136
	} else if proxyType == "badge" {
137
		m.countryStats.badge[addr] = true
138
	} else if proxyType == "webext" {
139
140
141
142
		m.countryStats.webext[addr] = true
	} else {
		m.countryStats.unknown[addr] = true
	}
143

144
145
146
147
148
149
150
151
152
	switch natType {
	case NATRestricted:
		m.countryStats.natRestricted[addr] = true
	case NATUnrestricted:
		m.countryStats.natUnrestricted[addr] = true
	default:
		m.countryStats.natUnknown[addr] = true
	}

153
154
155
156
157
158
159
160
161
162
163
164
}

func (m *Metrics) LoadGeoipDatabases(geoipDB string, geoip6DB string) error {

	// Load geoip databases
	log.Println("Loading geoip databases")
	tablev4 := new(GeoIPv4Table)
	err := GeoIPLoadFile(tablev4, geoipDB)
	if err != nil {
		m.tablev4 = nil
		return err
	}
165
	m.tablev4 = tablev4
166
167
168
169
170
171
172

	tablev6 := new(GeoIPv6Table)
	err = GeoIPLoadFile(tablev6, geoip6DB)
	if err != nil {
		m.tablev6 = nil
		return err
	}
173
	m.tablev6 = tablev6
174
175
176
	return nil
}

177
func NewMetrics(metricsLogger *log.Logger) (*Metrics, error) {
178
	m := new(Metrics)
179
180

	m.countryStats = CountryStats{
181
182
183
184
185
186
187
188
		counts:          make(map[string]int),
		standalone:      make(map[string]bool),
		badge:           make(map[string]bool),
		webext:          make(map[string]bool),
		unknown:         make(map[string]bool),
		natRestricted:   make(map[string]bool),
		natUnrestricted: make(map[string]bool),
		natUnknown:      make(map[string]bool),
189
190
	}

191
192
	m.logger = metricsLogger

193
	// Write to log file every hour with updated metrics
194
195
196
197
	go once.Do(m.logMetrics)

	return m, nil
}
198

Cecylia Bocovich's avatar
Cecylia Bocovich committed
199
// Logs metrics in intervals specified by metricsResolution
200
201
202
func (m *Metrics) logMetrics() {
	heartbeat := time.Tick(metricsResolution)
	for range heartbeat {
Cecylia Bocovich's avatar
Cecylia Bocovich committed
203
204
		m.printMetrics()
		m.zeroMetrics()
205
	}
206
}
207

Cecylia Bocovich's avatar
Cecylia Bocovich committed
208
func (m *Metrics) printMetrics() {
209
	m.lock.Lock()
210
	m.logger.Println("snowflake-stats-end", time.Now().UTC().Format("2006-01-02 15:04:05"), fmt.Sprintf("(%d s)", int(metricsResolution.Seconds())))
Cecylia Bocovich's avatar
Cecylia Bocovich committed
211
	m.logger.Println("snowflake-ips", m.countryStats.Display())
212
213
214
215
216
	m.logger.Println("snowflake-ips-total", len(m.countryStats.standalone)+
		len(m.countryStats.badge)+len(m.countryStats.webext)+len(m.countryStats.unknown))
	m.logger.Println("snowflake-ips-standalone", len(m.countryStats.standalone))
	m.logger.Println("snowflake-ips-badge", len(m.countryStats.badge))
	m.logger.Println("snowflake-ips-webext", len(m.countryStats.webext))
Cecylia Bocovich's avatar
Cecylia Bocovich committed
217
218
	m.logger.Println("snowflake-idle-count", binCount(m.proxyIdleCount))
	m.logger.Println("client-denied-count", binCount(m.clientDeniedCount))
219
220
	m.logger.Println("client-restricted-denied-count", binCount(m.clientRestrictedDeniedCount))
	m.logger.Println("client-unrestricted-denied-count", binCount(m.clientUnrestrictedDeniedCount))
Cecylia Bocovich's avatar
Cecylia Bocovich committed
221
	m.logger.Println("client-snowflake-match-count", binCount(m.clientProxyMatchCount))
222
223
224
	m.logger.Println("snowflake-ips-nat-restricted", len(m.countryStats.natRestricted))
	m.logger.Println("snowflake-ips-nat-unrestricted", len(m.countryStats.natUnrestricted))
	m.logger.Println("snowflake-ips-nat-unknown", len(m.countryStats.natUnknown))
225
	m.lock.Unlock()
Cecylia Bocovich's avatar
Cecylia Bocovich committed
226
227
228
229
230
231
}

// Restores all metrics to original values
func (m *Metrics) zeroMetrics() {
	m.proxyIdleCount = 0
	m.clientDeniedCount = 0
232
233
	m.clientRestrictedDeniedCount = 0
	m.clientUnrestrictedDeniedCount = 0
Cecylia Bocovich's avatar
Cecylia Bocovich committed
234
235
	m.clientProxyMatchCount = 0
	m.countryStats.counts = make(map[string]int)
236
237
238
239
	m.countryStats.standalone = make(map[string]bool)
	m.countryStats.badge = make(map[string]bool)
	m.countryStats.webext = make(map[string]bool)
	m.countryStats.unknown = make(map[string]bool)
240
241
242
	m.countryStats.natRestricted = make(map[string]bool)
	m.countryStats.natUnrestricted = make(map[string]bool)
	m.countryStats.natUnknown = make(map[string]bool)
Cecylia Bocovich's avatar
Cecylia Bocovich committed
243
244
}

245
// Rounds up a count to the nearest multiple of 8.
246
247
func binCount(count uint) uint {
	return uint((math.Ceil(float64(count) / 8)) * 8)
248
}