rcrowley / go-metrics

Go port of Coda Hale's Metrics library
Other
3.43k stars 493 forks source link

Zero values are reported when RegisterDebugGCStats and RegisterRuntimeMemStats are called more than once #252

Closed eranharel closed 5 years ago

eranharel commented 5 years ago

When RegisterDebugGCStats and RegisterRuntimeMemStats are accidentally called more than once the CaptureXxx function will report zero values due unprotected override of the metrics stored in debugMetrics and runtimeMetrics

Example test case:

func TestDebugGCStatsDoubleRegister(t *testing.T) {
    r := NewRegistry()
    RegisterDebugGCStats(r)
    runtime.GC()
    CaptureDebugGCStatsOnce(r)
    if numGC := debugMetrics.GCStats.NumGC.Value(); 1 != numGC {
        t.Errorf("NumGC got %d, expected 1", numGC)
    }

    RegisterDebugGCStats(r)
    if numGC := debugMetrics.GCStats.NumGC.Value(); 1 != numGC {
        t.Errorf("NumGC got %d, expected 1", numGC)
    }
}