From f9588208d454f15db398452f40b839f055a50c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ask=20Bj=C3=B8rn=20Hansen?= Date: Mon, 7 Sep 2015 01:59:59 -0700 Subject: [PATCH] Add more data to status json Fix another potential race --- monitor.go | 12 ++++++++++-- zone.go | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/monitor.go b/monitor.go index 9c534ef7..2525dedc 100644 --- a/monitor.go +++ b/monitor.go @@ -271,15 +271,20 @@ func StatusJSONHandler(zones Zones) func(http.ResponseWriter, *http.Request) { zonemetrics := make(map[string]metrics.Registry) for name, zone := range zones { + zone.Lock() zonemetrics[name] = zone.Metrics.Registry + zone.Unlock() } type statusData struct { Version string Uptime int64 Platform string - Metrics map[string]metrics.Registry + Zones map[string]metrics.Registry Global metrics.Registry + ID string + IP string + Groups []string } uptime := int64(time.Since(timeStarted).Seconds()) @@ -288,8 +293,11 @@ func StatusJSONHandler(zones Zones) func(http.ResponseWriter, *http.Request) { Version: VERSION, Uptime: uptime, Platform: runtime.GOARCH + "-" + runtime.GOOS, - Metrics: zonemetrics, + Zones: zonemetrics, Global: metrics.DefaultRegistry, + ID: serverID, + IP: serverIP, + Groups: serverGroups, } b, err := json.Marshal(status) diff --git a/zone.go b/zone.go index 156faad6..a98c17dd 100644 --- a/zone.go +++ b/zone.go @@ -2,6 +2,7 @@ package main import ( "strings" + "sync" "github.com/abh/geodns/Godeps/_workspace/src/github.com/miekg/dns" "github.com/abh/geodns/Godeps/_workspace/src/github.com/rcrowley/go-metrics" @@ -59,6 +60,8 @@ type Zone struct { Options ZoneOptions Logging *ZoneLogging Metrics ZoneMetrics + + sync.RWMutex } type qTypes []uint16 @@ -79,6 +82,9 @@ func NewZone(name string) *Zone { } func (z *Zone) SetupMetrics(old *Zone) { + z.Lock() + defer z.Unlock() + if old != nil { z.Metrics = old.Metrics }