Skip to content
Permalink
Browse files
Added a /metrics handler to fetch metrics data
Right now we're writing metrics data to a file. This new handler allows
others to fetch metrics data. Right now the handler only returns the
counts for the last 24 hours as a temporary thing. This should be changed,
depending on what the metrics team needs but definitely to include
measurements from the last full 24 hour measurement period.
  • Loading branch information
cohosh committed Aug 9, 2019
1 parent 0b55fd3 commit f0e71c7c58f72118313c28c1d90ec4fdfb74a23f
Showing with 22 additions and 15 deletions.
  1. +5 −0 broker/broker.go
  2. +10 −8 broker/metrics.go
  3. +7 −7 broker/snowflake-broker_test.go
@@ -246,6 +246,10 @@ func debugHandler(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(s))
}

func metricsHandler(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(ctx.metrics.printMetrics()))
}

func robotsTxtHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write([]byte("User-agent: *\nDisallow: /\n"))
@@ -313,6 +317,7 @@ func main() {
http.Handle("/client", SnowflakeHandler{ctx, clientOffers})
http.Handle("/answer", SnowflakeHandler{ctx, proxyAnswers})
http.Handle("/debug", SnowflakeHandler{ctx, debugHandler})
http.Handle("/metrics", SnowflakeHandler{ctx, metricsHandler})

server := http.Server{
Addr: addr,
@@ -169,18 +169,20 @@ func NewMetrics(metricsLogger *log.Logger) (*Metrics, error) {
func (m *Metrics) logMetrics() {
heartbeat := time.Tick(metricsResolution)
for range heartbeat {
m.printMetrics()
m.logger.Print(m.printMetrics())
m.zeroMetrics()
}
}

func (m *Metrics) printMetrics() {
m.logger.Println("snowflake-stats-end", time.Now().UTC().Format("2006-01-02 15:04:05"), fmt.Sprintf("(%d s)", int(metricsResolution.Seconds())))
m.logger.Println("snowflake-ips", m.countryStats.Display())
m.logger.Println("snowflake-ips-total", len(m.countryStats.addrs))
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))
func (m *Metrics) printMetrics() string {
s := fmt.Sprintln("snowflake-stats-end", time.Now().UTC().Format("2006-01-02 15:04:05"), fmt.Sprintf("(%d s)", int(metricsResolution.Seconds())))
s += fmt.Sprintln("snowflake-ips", m.countryStats.Display())
s += fmt.Sprintln("snowflake-ips-total", len(m.countryStats.addrs))
s += fmt.Sprintln("snowflake-idle-count", binCount(m.proxyIdleCount))
s += fmt.Sprintln("client-denied-count", binCount(m.clientDeniedCount))
s += fmt.Sprintln("client-snowflake-match-count", binCount(m.clientProxyMatchCount))

return s
}

// Restores all metrics to original values
@@ -418,7 +418,7 @@ func TestMetrics(t *testing.T) {
p.offerChannel <- nil
<-done

ctx.metrics.printMetrics()
ctx.metrics.logger.Print(ctx.metrics.printMetrics())
So(buf.String(), ShouldResemble, "snowflake-stats-end "+time.Now().UTC().Format("2006-01-02 15:04:05")+" (86400 s)\nsnowflake-ips CA=1\nsnowflake-ips-total 1\nsnowflake-idle-count 8\nclient-denied-count 0\nclient-snowflake-match-count 0\n")
})

@@ -431,13 +431,13 @@ func TestMetrics(t *testing.T) {

clientOffers(ctx, w, r)

ctx.metrics.printMetrics()
ctx.metrics.logger.Print(ctx.metrics.printMetrics())
So(buf.String(), ShouldResemble, "snowflake-stats-end "+time.Now().UTC().Format("2006-01-02 15:04:05")+" (86400 s)\nsnowflake-ips \nsnowflake-ips-total 0\nsnowflake-idle-count 0\nclient-denied-count 8\nclient-snowflake-match-count 0\n")

// Test reset
buf.Reset()
ctx.metrics.zeroMetrics()
ctx.metrics.printMetrics()
ctx.metrics.logger.Print(ctx.metrics.printMetrics())
So(buf.String(), ShouldResemble, "snowflake-stats-end "+time.Now().UTC().Format("2006-01-02 15:04:05")+" (86400 s)\nsnowflake-ips \nsnowflake-ips-total 0\nsnowflake-idle-count 0\nclient-denied-count 0\nclient-snowflake-match-count 0\n")
})
//Test addition of client matches
@@ -458,7 +458,7 @@ func TestMetrics(t *testing.T) {
snowflake.answerChannel <- []byte("fake answer")
<-done

ctx.metrics.printMetrics()
ctx.metrics.logger.Print(ctx.metrics.printMetrics())
So(buf.String(), ShouldResemble, "snowflake-stats-end "+time.Now().UTC().Format("2006-01-02 15:04:05")+" (86400 s)\nsnowflake-ips \nsnowflake-ips-total 0\nsnowflake-idle-count 0\nclient-denied-count 0\nclient-snowflake-match-count 8\n")
})
//Test rounding boundary
@@ -477,12 +477,12 @@ func TestMetrics(t *testing.T) {
clientOffers(ctx, w, r)
clientOffers(ctx, w, r)

ctx.metrics.printMetrics()
ctx.metrics.logger.Print(ctx.metrics.printMetrics())
So(buf.String(), ShouldResemble, "snowflake-stats-end "+time.Now().UTC().Format("2006-01-02 15:04:05")+" (86400 s)\nsnowflake-ips \nsnowflake-ips-total 0\nsnowflake-idle-count 0\nclient-denied-count 8\nclient-snowflake-match-count 0\n")

clientOffers(ctx, w, r)
buf.Reset()
ctx.metrics.printMetrics()
ctx.metrics.logger.Print(ctx.metrics.printMetrics())
So(buf.String(), ShouldResemble, "snowflake-stats-end "+time.Now().UTC().Format("2006-01-02 15:04:05")+" (86400 s)\nsnowflake-ips \nsnowflake-ips-total 0\nsnowflake-idle-count 0\nclient-denied-count 16\nclient-snowflake-match-count 0\n")
})

@@ -514,7 +514,7 @@ func TestMetrics(t *testing.T) {
p.offerChannel <- nil
<-done

ctx.metrics.printMetrics()
ctx.metrics.logger.Print(ctx.metrics.printMetrics())
So(buf.String(), ShouldResemble, "snowflake-stats-end "+time.Now().UTC().Format("2006-01-02 15:04:05")+" (86400 s)\nsnowflake-ips CA=1\nsnowflake-ips-total 1\nsnowflake-idle-count 8\nclient-denied-count 0\nclient-snowflake-match-count 0\n")
})
})

0 comments on commit f0e71c7

Please sign in to comment.