Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: resolve potential data race #520

Merged
merged 2 commits into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions health/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func (h *Handler) SetRoutes(r *httprouter.Router) {
// 204: emptyResponse
// 500: genericError
func (h *Handler) Health(rw http.ResponseWriter, r *http.Request, _ httprouter.Params) {
h.Metrics.Update()

h.Metrics.Lock()
defer h.Metrics.Unlock()

h.Metrics.Update()
h.H.Write(rw, r, h.Metrics)
}
6 changes: 2 additions & 4 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ func (sw *Snapshot) GetUpTime() int64 {
return sw.UpTime
}

func (sw *Snapshot) Update() {
sw.Lock()
defer sw.Unlock()

func (sw *Snapshot) Update() {
var m runtime.MemStats
runtime.ReadMemStats(&m)

Expand All @@ -150,6 +147,7 @@ func (sw *Snapshot) Update() {
HeapObjects: m.HeapObjects,
NumGC: m.NumGC,
}

sw.UpTime = int64(time.Now().Sub(sw.start) / time.Second)

}
Expand Down
2 changes: 2 additions & 0 deletions metrics/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ func TestMiddleware(t *testing.T) {

assert.EqualValues(t, 1, mw.Snapshot.Path("/oauth2/introspect").Requests)

mw.Lock()
mw.Update()
assert.NotEqual(t, 0, mw.UpTime)
mw.Unlock()

out, _ := json.MarshalIndent(mw, "\t", " ")
t.Logf("%s", out)
Expand Down