Browse Source

Make monitor work again

Ask Bjørn Hansen 8 years ago
parent
commit
f558f30fee
6 changed files with 93 additions and 84 deletions
  1. 2 2
      Makefile
  2. 0 4
      dns/geodns.conf.sample
  3. 7 3
      geodns.go
  4. 31 31
      http.go
  5. 53 0
      http_test.go
  6. 0 44
      monitor/monitor_test.go

+ 2 - 2
Makefile

@@ -5,10 +5,10 @@ templates.go: templates/*.html monitor.go
 	go generate
 	go generate
 
 
 test: .PHONY
 test: .PHONY
-	go test $(shell go list ./... | grep -v /vendor/)
+	go test -v $(shell go list ./... | grep -v /vendor/)
 
 
 testrace: .PHONY
 testrace: .PHONY
-	go test -race $(shell go list ./... | grep -v /vendor/)
+	go test -v -race $(shell go list ./... | grep -v /vendor/)
 
 
 devel:
 devel:
 	go build -tags devel
 	go build -tags devel

+ 0 - 4
dns/geodns.conf.sample

@@ -15,10 +15,6 @@ path = log/queries.log
 ;; keep up to this many rotated log files (default 1)
 ;; keep up to this many rotated log files (default 1)
 ; keep = 2
 ; keep = 2
 
 
-[stathat]
-;; Add an API key to send query counts and other metrics to stathat
-;apikey=abc123
-
 [http]
 [http]
 ; require basic HTTP authentication; not encrypted or safe over the public internet
 ; require basic HTTP authentication; not encrypted or safe over the public internet
 ; user = stats
 ; user = stats

+ 7 - 3
geodns.go

@@ -78,6 +78,7 @@ func init() {
 	log.SetFlags(log.Lmicroseconds | log.Lshortfile)
 	log.SetFlags(log.Lmicroseconds | log.Lshortfile)
 
 
 	serverInfo = &monitor.ServerInfo{}
 	serverInfo = &monitor.ServerInfo{}
+	serverInfo.Version = VERSION
 	serverInfo.UUID = uuid.New()
 	serverInfo.UUID = uuid.New()
 	serverInfo.Started = time.Now()
 	serverInfo.Started = time.Now()
 
 
@@ -217,9 +218,12 @@ func main() {
 		go srv.ListenAndServe(host)
 		go srv.ListenAndServe(host)
 	}
 	}
 
 
-	go func() {
-		// setup metrics httpd stuff
-	}()
+	if len(*flaghttp) > 0 {
+		go func() {
+			hs := NewHTTPServer(muxm, serverInfo)
+			hs.Run(*flaghttp)
+		}()
+	}
 
 
 	terminate := make(chan os.Signal)
 	terminate := make(chan os.Signal)
 	signal.Notify(terminate, os.Interrupt)
 	signal.Notify(terminate, os.Interrupt)

+ 31 - 31
http.go

@@ -19,8 +19,8 @@ import (
 
 
 type httpServer struct {
 type httpServer struct {
 	mux        *http.ServeMux
 	mux        *http.ServeMux
-	zones      zones.ZoneList
-	serverInfo monitor.ServerInfo
+	zones      *zones.MuxManager
+	serverInfo *monitor.ServerInfo
 }
 }
 
 
 type rate struct {
 type rate struct {
@@ -28,18 +28,18 @@ type rate struct {
 	Count   int64
 	Count   int64
 	Metrics zones.ZoneMetrics
 	Metrics zones.ZoneMetrics
 }
 }
-type Rates []*rate
+type rates []*rate
 
 
-func (s Rates) Len() int      { return len(s) }
-func (s Rates) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+func (s rates) Len() int      { return len(s) }
+func (s rates) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
 
 
-type RatesByCount struct{ Rates }
+type ratesByCount struct{ rates }
 
 
-func (s RatesByCount) Less(i, j int) bool {
-	ic := s.Rates[i].Count
-	jc := s.Rates[j].Count
+func (s ratesByCount) Less(i, j int) bool {
+	ic := s.rates[i].Count
+	jc := s.rates[j].Count
 	if ic == jc {
 	if ic == jc {
-		return s.Rates[i].Name < s.Rates[j].Name
+		return s.rates[i].Name < s.rates[j].Name
 	}
 	}
 	return ic > jc
 	return ic > jc
 }
 }
@@ -82,11 +82,11 @@ func topParam(req *http.Request, def int) int {
 	return topOption
 	return topOption
 }
 }
 
 
-func NewHTTPServer(zones zones.ZoneList) *httpServer {
+func NewHTTPServer(mm *zones.MuxManager, serverInfo *monitor.ServerInfo) *httpServer {
 	hs := &httpServer{
 	hs := &httpServer{
-		// todo: zones.MuxManager instead of zones?
-		zones: zones,
-		mux:   &http.ServeMux{},
+		zones:      mm,
+		mux:        &http.ServeMux{},
+		serverInfo: serverInfo,
 	}
 	}
 	hs.mux.HandleFunc("/status", hs.StatusHandler())
 	hs.mux.HandleFunc("/status", hs.StatusHandler())
 	hs.mux.HandleFunc("/status.json", hs.StatusJSONHandler())
 	hs.mux.HandleFunc("/status.json", hs.StatusJSONHandler())
@@ -112,7 +112,7 @@ func (hs *httpServer) StatusJSONHandler() func(http.ResponseWriter, *http.Reques
 
 
 		zonemetrics := make(map[string]metrics.Registry)
 		zonemetrics := make(map[string]metrics.Registry)
 
 
-		for name, zone := range hs.zones {
+		for name, zone := range hs.zones.Zones() {
 			zone.Lock()
 			zone.Lock()
 			zonemetrics[name] = zone.Metrics.Registry
 			zonemetrics[name] = zone.Metrics.Registry
 			zone.Unlock()
 			zone.Unlock()
@@ -159,13 +159,26 @@ func (hs *httpServer) StatusJSONHandler() func(http.ResponseWriter, *http.Reques
 
 
 func (hs *httpServer) StatusHandler() func(http.ResponseWriter, *http.Request) {
 func (hs *httpServer) StatusHandler() func(http.ResponseWriter, *http.Request) {
 
 
+	type statusData struct {
+		Version  string
+		Zones    rates
+		Uptime   DayDuration
+		Platform string
+		Global   struct {
+			Queries         metrics.Meter
+			Histogram       histogramData
+			HistogramRecent histogramData
+		}
+		TopOption int
+	}
+
 	return func(w http.ResponseWriter, req *http.Request) {
 	return func(w http.ResponseWriter, req *http.Request) {
 
 
 		topOption := topParam(req, 10)
 		topOption := topParam(req, 10)
 
 
-		rates := make(Rates, 0)
+		rates := make(rates, 0)
 
 
-		for name, zone := range hs.zones {
+		for name, zone := range hs.zones.Zones() {
 			count := zone.Metrics.Queries.Count()
 			count := zone.Metrics.Queries.Count()
 			rates = append(rates, &rate{
 			rates = append(rates, &rate{
 				Name:    name,
 				Name:    name,
@@ -174,20 +187,7 @@ func (hs *httpServer) StatusHandler() func(http.ResponseWriter, *http.Request) {
 			})
 			})
 		}
 		}
 
 
-		sort.Sort(RatesByCount{rates})
-
-		type statusData struct {
-			Version  string
-			Zones    Rates
-			Uptime   DayDuration
-			Platform string
-			Global   struct {
-				Queries         metrics.Meter
-				Histogram       histogramData
-				HistogramRecent histogramData
-			}
-			TopOption int
-		}
+		sort.Sort(ratesByCount{rates})
 
 
 		uptime := DayDuration{time.Since(hs.serverInfo.Started)}
 		uptime := DayDuration{time.Since(hs.serverInfo.Started)}
 
 

+ 53 - 0
http_test.go

@@ -0,0 +1,53 @@
+package main
+
+import (
+	"bytes"
+	"io/ioutil"
+	"net/http"
+	"net/http/httptest"
+	"testing"
+
+	"github.com/stretchr/testify/require"
+
+	"github.com/abh/geodns/server"
+	"github.com/abh/geodns/zones"
+)
+
+func TestHTTP(t *testing.T) {
+
+	// todo: less global metrics ...
+	server.NewMetrics()
+
+	mm, err := zones.NewMuxManager("dns", &zones.NilReg{})
+	if err != nil {
+		t.Fatalf("loading zones: %s", err)
+	}
+	hs := NewHTTPServer(mm, serverInfo)
+
+	srv := httptest.NewServer(hs.Mux())
+
+	baseurl := srv.URL
+	t.Logf("server base url: '%s'", baseurl)
+
+	// metrics := NewMetrics()
+	// go metrics.Updater()
+
+	res, err := http.Get(baseurl + "/version")
+	require.Nil(t, err)
+	page, _ := ioutil.ReadAll(res.Body)
+
+	if !bytes.Contains(page, []byte("<title>GeoDNS")) {
+		t.Log("/version didn't include '<title>GeoDNS'")
+		t.Fail()
+	}
+
+	res, err = http.Get(baseurl + "/status")
+	require.Nil(t, err)
+	page, _ = ioutil.ReadAll(res.Body)
+
+	// just check that template basically works
+	if !bytes.Contains(page, []byte("<html>")) {
+		t.Log("/status didn't include <html>")
+		t.Fail()
+	}
+}

+ 0 - 44
monitor/monitor_test.go

@@ -1,44 +0,0 @@
-package monitor
-
-import "testing"
-
-func TestMonitor(t *testing.T) {
-
-	// mux := dns.NewServeMux()
-	// mm := zones.NewMuxManager("dns", mux)
-
-	// // s.zones = make(zones.Zones)
-	// metrics := NewMetrics()
-	// go metrics.Updater()
-
-	// *flaghttp = ":8881"
-
-	// fmt.Println("Starting http server")
-
-	// // TODO: use httptest
-	// // https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/Jk785WB7F8I
-
-	// srv := Server{}
-	// srv.
-
-	// todo: this isn't right, it should probably just take the mux?
-	// go httpHandler(mm.Zones())
-	// time.Sleep(500 * time.Millisecond)
-
-	// c.Check(true, DeepEquals, true)
-
-	// res, err := http.Get("http://localhost:8881/version")
-	// c.Assert(err, IsNil)
-	// page, _ := ioutil.ReadAll(res.Body)
-	// c.Check(string(page), Matches, ".*<title>GeoDNS [0-9].*")
-
-	// res, err = http.Get("http://localhost:8881/status")
-	// c.Assert(err, IsNil)
-	// page, _ = ioutil.ReadAll(res.Body)
-	// // just check that template basically works
-
-	// isOk := strings.Contains(string(page), "<html>")
-	// // page has <html>
-	// c.Check(isOk, Equals, true)
-
-}