diff options
Diffstat (limited to 'http')
-rw-r--r-- | http/http.go | 44 | ||||
-rw-r--r-- | http/http_test.go | 4 |
2 files changed, 32 insertions, 16 deletions
diff --git a/http/http.go b/http/http.go index 3bf047c..3da9b4f 100644 --- a/http/http.go +++ b/http/http.go @@ -33,10 +33,14 @@ type entry struct { Rcode string `json:"rcode,omitempty"` } +type stats struct { + Summary summary `json:"summary"` + Requests []request `json:"requests"` +} + type summary struct { - Since string `json:"since"` - Total int64 `json:"total"` - Hijacked int64 `json:"hijacked"` + Log logStats `json:"log"` + Cache cacheStats `json:"cache"` } type request struct { @@ -45,8 +49,14 @@ type request struct { } type logStats struct { - Summary summary `json:"summary"` - Requests []request `json:"requests"` + Since string `json:"since"` + Total int64 `json:"total"` + Hijacked int64 `json:"hijacked"` +} + +type cacheStats struct { + Size int `json:"size"` + Capacity int `json:"capacity"` } type httpError struct { @@ -137,26 +147,32 @@ func (s *Server) logHandler(w http.ResponseWriter, r *http.Request) (interface{} } func (s *Server) metricHandler(w http.ResponseWriter, r *http.Request) (interface{}, *httpError) { - stats, err := s.logger.Stats() + lstats, err := s.logger.Stats() if err != nil { return nil, newHTTPError(err) } - requests := make([]request, 0, len(stats.Events)) - for _, e := range stats.Events { + requests := make([]request, 0, len(lstats.Events)) + for _, e := range lstats.Events { requests = append(requests, request{ Time: e.Time.Format(time.RFC3339), Count: e.Count, }) } - logStats := logStats{ + cstats := s.cache.Stats() + return stats{ Summary: summary{ - Since: stats.Since.Format(time.RFC3339), - Total: stats.Total, - Hijacked: stats.Hijacked, + Log: logStats{ + Since: lstats.Since.Format(time.RFC3339), + Total: lstats.Total, + Hijacked: lstats.Hijacked, + }, + Cache: cacheStats{ + Capacity: cstats.Capacity, + Size: cstats.Size, + }, }, Requests: requests, - } - return logStats, nil + }, nil } // Close shuts down the HTTP server. diff --git a/http/http_test.go b/http/http_test.go index 901903f..2909271 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -89,7 +89,7 @@ func TestRequests(t *testing.T) { lr1 := `[{"time":"RFC3339","remote_addr":"127.0.0.254","hijacked":true,"type":"AAAA","question":"example.com.","answers":["2001:db8::1"]},` + `{"time":"RFC3339","remote_addr":"127.0.0.42","hijacked":false,"type":"A","question":"example.com.","answers":["192.0.2.101","192.0.2.100"]}]` lr2 := `[{"time":"RFC3339","remote_addr":"127.0.0.254","hijacked":true,"type":"AAAA","question":"example.com.","answers":["2001:db8::1"]}]` - mr1 := `{"summary":{"since":"RFC3339","total":2,"hijacked":1},"requests":[{"time":"RFC3339","count":2}]}` + mr1 := `{"summary":{"log":{"since":"RFC3339","total":2,"hijacked":1},"cache":{"size":2,"capacity":10}},"requests":[{"time":"RFC3339","count":2}]}` var tests = []struct { method string @@ -104,8 +104,8 @@ func TestRequests(t *testing.T) { {http.MethodGet, "/cache/v1/", cr1, 200}, {http.MethodGet, "/cache/v1/?n=foo", cr1, 200}, {http.MethodGet, "/cache/v1/?n=1", cr2, 200}, - {http.MethodDelete, "/cache/v1/", `{"message":"Cleared cache."}`, 200}, {http.MethodGet, "/metric/v1/", mr1, 200}, + {http.MethodDelete, "/cache/v1/", `{"message":"Cleared cache."}`, 200}, } for i, tt := range tests { |