mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 11:11:30 -04:00
Unit tests for metrics code
Added unit tests for metrics logging. Refactored the logMetrics() function to allow for easier testing
This commit is contained in:
parent
25f059f4c4
commit
fe3356a54d
2 changed files with 149 additions and 13 deletions
|
@ -116,25 +116,32 @@ func NewMetrics(metricsLogger *log.Logger) (*Metrics, error) {
|
|||
return m, nil
|
||||
}
|
||||
|
||||
// Logs metrics in intervals specified by metricsResolution
|
||||
func (m *Metrics) logMetrics() {
|
||||
|
||||
heartbeat := time.Tick(metricsResolution)
|
||||
for range heartbeat {
|
||||
m.logger.Println("snowflake-stats-end", time.Now().UTC().Format("2006-01-02 15:04:05"), "(", int(metricsResolution.Seconds()), "s)")
|
||||
m.logger.Println("snowflake-ips", m.countryStats.Display())
|
||||
m.logger.Println("snowflake-idle-count", binCount(m.proxyIdleCount))
|
||||
m.logger.Println("client-denied-count", binCount(m.clientDeniedCount))
|
||||
m.logger.Println("client-snowflake-match-count", binCount(m.clientProxyMatchCount))
|
||||
|
||||
//restore all metrics to original values
|
||||
m.proxyIdleCount = 0
|
||||
m.clientDeniedCount = 0
|
||||
m.clientProxyMatchCount = 0
|
||||
m.countryStats.counts = make(map[string]int)
|
||||
m.countryStats.ips = make(map[string]bool)
|
||||
m.printMetrics()
|
||||
m.zeroMetrics()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Metrics) printMetrics() {
|
||||
m.logger.Println("snowflake-stats-end", time.Now().UTC().Format("2006-01-02 15:04:05"), "(", int(metricsResolution.Seconds()), "s)")
|
||||
m.logger.Println("snowflake-ips", m.countryStats.Display())
|
||||
m.logger.Println("snowflake-idle-count", binCount(m.proxyIdleCount))
|
||||
m.logger.Println("client-denied-count", binCount(m.clientDeniedCount))
|
||||
m.logger.Println("client-snowflake-match-count", binCount(m.clientProxyMatchCount))
|
||||
}
|
||||
|
||||
// Restores all metrics to original values
|
||||
func (m *Metrics) zeroMetrics() {
|
||||
m.proxyIdleCount = 0
|
||||
m.clientDeniedCount = 0
|
||||
m.clientProxyMatchCount = 0
|
||||
m.countryStats.counts = make(map[string]int)
|
||||
m.countryStats.ips = make(map[string]bool)
|
||||
}
|
||||
|
||||
// Rounds up a count to the nearest multiple of 8.
|
||||
func binCount(count int) int {
|
||||
return int((math.Ceil(float64(count) / 8)) * 8)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue