aboutsummaryrefslogtreecommitdiffstats
path: root/http
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2020-01-12 13:12:41 +0100
committerMartin Polden <mpolden@mpolden.no>2020-01-12 13:12:41 +0100
commitbde6ca13208165bf35a2a86fdb625de42cf91ba7 (patch)
treee86a5434fe41aca318aee760b3fe6d5909bdf6b0 /http
parent8b7dbf9a9bf62fb451e949c4a1951b8191326653 (diff)
Add metric for pending tasks
Diffstat (limited to 'http')
-rw-r--r--http/http.go43
-rw-r--r--http/http_test.go5
2 files changed, 32 insertions, 16 deletions
diff --git a/http/http.go b/http/http.go
index b7f1a86..d6db139 100644
--- a/http/http.go
+++ b/http/http.go
@@ -18,9 +18,10 @@ import (
// A Server defines paramaters for running an HTTP server. The HTTP server serves an API for inspecting cache contents
// and request log.
type Server struct {
- cache *cache.Cache
- logger *sql.Logger
- server *http.Server
+ cache *cache.Cache
+ logger *sql.Logger
+ sqlCache *sql.Cache
+ server *http.Server
}
type entry struct {
@@ -50,14 +51,21 @@ type request struct {
}
type logStats struct {
- Since string `json:"since"`
- Total int64 `json:"total"`
- Hijacked int64 `json:"hijacked"`
+ Since string `json:"since"`
+ Total int64 `json:"total"`
+ Hijacked int64 `json:"hijacked"`
+ PendingTasks int `json:"pending_tasks"`
}
type cacheStats struct {
- Size int `json:"size"`
- Capacity int `json:"capacity"`
+ Size int `json:"size"`
+ Capacity int `json:"capacity"`
+ PendingTasks int `json:"pending_tasks"`
+ BackendStats *backendStats `json:"backend,omitempty"`
+}
+
+type backendStats struct {
+ PendingTasks int `json:"pending_tasks"`
}
type httpError struct {
@@ -81,12 +89,13 @@ func newHTTPBadRequest(err error) *httpError {
}
// NewServer creates a new HTTP server, serving logs from the given logger and listening on addr.
-func NewServer(cache *cache.Cache, logger *sql.Logger, addr string) *Server {
+func NewServer(cache *cache.Cache, logger *sql.Logger, sqlCache *sql.Cache, addr string) *Server {
server := &http.Server{Addr: addr}
s := &Server{
- cache: cache,
- logger: logger,
- server: server,
+ cache: cache,
+ logger: logger,
+ sqlCache: sqlCache,
+ server: server,
}
s.server.Handler = s.handler()
return s
@@ -189,6 +198,10 @@ func (s *Server) metricHandler(w http.ResponseWriter, r *http.Request) (interfac
})
}
cstats := s.cache.Stats()
+ var bstats *backendStats
+ if s.sqlCache != nil {
+ bstats = &backendStats{PendingTasks: s.sqlCache.Stats().PendingTasks}
+ }
return stats{
Summary: summary{
Log: logStats{
@@ -197,8 +210,10 @@ func (s *Server) metricHandler(w http.ResponseWriter, r *http.Request) (interfac
Hijacked: lstats.Hijacked,
},
Cache: cacheStats{
- Capacity: cstats.Capacity,
- Size: cstats.Size,
+ Capacity: cstats.Capacity,
+ Size: cstats.Size,
+ PendingTasks: cstats.PendingTasks,
+ BackendStats: bstats,
},
},
Requests: requests,
diff --git a/http/http_test.go b/http/http_test.go
index 6fc53d8..9608fcd 100644
--- a/http/http_test.go
+++ b/http/http_test.go
@@ -35,8 +35,9 @@ func testServer() (*httptest.Server, *Server) {
panic(err)
}
logger := sql.NewLogger(sqlClient, sql.LogAll, 0)
+ sqlCache := sql.NewCache(sqlClient)
cache := cache.New(10, nil)
- server := Server{logger: logger, cache: cache}
+ server := Server{logger: logger, cache: cache, sqlCache: sqlCache}
return httptest.NewServer(server.handler()), &server
}
@@ -89,7 +90,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":{"log":{"since":"RFC3339","total":2,"hijacked":1},"cache":{"size":2,"capacity":10}},"requests":[{"time":"RFC3339","count":2}]}`
+ mr1 := `{"summary":{"log":{"since":"RFC3339","total":2,"hijacked":1,"pending_tasks":0},"cache":{"size":2,"capacity":10,"pending_tasks":0,"backend":{"pending_tasks":0}}},"requests":[{"time":"RFC3339","count":2}]}`
var tests = []struct {
method string