Skip to content

Commit 21b132f

Browse files
committed
record quantiles as well
1 parent ff586ea commit 21b132f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

prometheus/go_collector.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package prometheus
33
import (
44
"runtime"
55
"runtime/debug"
6+
"time"
67
)
78

89
type goCollector struct {
@@ -37,7 +38,13 @@ func (c *goCollector) Collect(ch chan<- Metric) {
3738
ch <- c.goroutines
3839

3940
var stats debug.GCStats
41+
stats.PauseQuantiles = make([]time.Duration, 5)
4042
debug.ReadGCStats(&stats)
4143

42-
ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), float64(stats.PauseTotal.Seconds()), nil)
44+
quantiles := make(map[float64]float64)
45+
for idx, pq := range stats.PauseQuantiles[1:] {
46+
quantiles[float64(idx+1)/float64(len(stats.PauseQuantiles)-1)] = pq.Seconds()
47+
}
48+
quantiles[0.0] = stats.PauseQuantiles[0].Seconds()
49+
ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), float64(stats.PauseTotal.Seconds()), quantiles)
4350
}

prometheus/go_collector_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package prometheus
22

33
import (
4-
"runtime/debug"
4+
"runtime"
55
"testing"
66
"time"
77

@@ -72,7 +72,7 @@ func TestGCCollector(t *testing.T) {
7272
go func() {
7373
c.Collect(ch)
7474
// force GC
75-
debug.FreeOSMemory()
75+
runtime.GC()
7676
<-waitc
7777
c.Collect(ch)
7878
}()
@@ -89,6 +89,14 @@ func TestGCCollector(t *testing.T) {
8989
continue
9090
}
9191

92+
if len(pb.GetSummary().Quantile) != 5 {
93+
t.Errorf("expected 4 buckets, got %d", len(pb.GetSummary().Quantile))
94+
}
95+
for idx, want := range []float64{0.0, 0.25, 0.5, 0.75, 1.0} {
96+
if *pb.GetSummary().Quantile[idx].Quantile != want {
97+
t.Errorf("bucket #%d is off, got %f, want %f", idx, *pb.GetSummary().Quantile[idx].Quantile, want)
98+
}
99+
}
92100
if first {
93101
first = false
94102
oldGC = *pb.GetSummary().SampleCount

0 commit comments

Comments
 (0)