Commit 25f059f4 authored by Cecylia Bocovich's avatar Cecylia Bocovich
Browse files

Log geoip stats of proxies by unique IP

Change it so that we log the geoip country code of proxies if they poll
within the current metrics epoch. We make sure we log by unique IP
address
parent c782ebdc
...@@ -149,6 +149,15 @@ func proxyPolls(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) { ...@@ -149,6 +149,15 @@ func proxyPolls(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
return return
} }
log.Println("Received snowflake: ", id) log.Println("Received snowflake: ", id)
// Log geoip stats
remoteIP, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Println("Error processing proxy IP: ", err.Error())
} else {
ctx.metrics.UpdateCountryStats(remoteIP)
}
// Wait for a client to avail an offer to the snowflake, or timeout if nil. // Wait for a client to avail an offer to the snowflake, or timeout if nil.
offer := ctx.RequestOffer(id) offer := ctx.RequestOffer(id)
if nil == offer { if nil == offer {
...@@ -224,15 +233,6 @@ func proxyAnswers(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) { ...@@ -224,15 +233,6 @@ func proxyAnswers(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
return return
} }
// Get proxy country stats
remoteIP, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Println("Error processing proxy IP: ", err.Error())
} else {
ctx.metrics.UpdateCountryStats(remoteIP)
}
log.Println("Received answer.") log.Println("Received answer.")
snowflake.answerChannel <- body snowflake.answerChannel <- body
} }
......
...@@ -17,6 +17,7 @@ var ( ...@@ -17,6 +17,7 @@ var (
const metricsResolution = 86400 * time.Second const metricsResolution = 86400 * time.Second
type CountryStats struct { type CountryStats struct {
ips map[string]bool
counts map[string]int counts map[string]int
} }
...@@ -65,8 +66,11 @@ func (m *Metrics) UpdateCountryStats(addr string) { ...@@ -65,8 +66,11 @@ func (m *Metrics) UpdateCountryStats(addr string) {
log.Println("Unknown geoip") log.Println("Unknown geoip")
} }
//update map of countries and counts //update map of unique ips and counts
m.countryStats.counts[country]++ if !m.countryStats.ips[addr] {
m.countryStats.counts[country]++
m.countryStats.ips[addr] = true
}
return return
} }
...@@ -101,6 +105,7 @@ func NewMetrics(metricsLogger *log.Logger) (*Metrics, error) { ...@@ -101,6 +105,7 @@ func NewMetrics(metricsLogger *log.Logger) (*Metrics, error) {
m.countryStats = CountryStats{ m.countryStats = CountryStats{
counts: make(map[string]int), counts: make(map[string]int),
ips: make(map[string]bool),
} }
m.logger = metricsLogger m.logger = metricsLogger
...@@ -126,6 +131,7 @@ func (m *Metrics) logMetrics() { ...@@ -126,6 +131,7 @@ func (m *Metrics) logMetrics() {
m.clientDeniedCount = 0 m.clientDeniedCount = 0
m.clientProxyMatchCount = 0 m.clientProxyMatchCount = 0
m.countryStats.counts = make(map[string]int) m.countryStats.counts = make(map[string]int)
m.countryStats.ips = make(map[string]bool)
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment