10 type goCollector struct {
14 // metrics to describe and collect
15 metrics memStatsMetrics
18 // NewGoCollector returns a collector which exports metrics about the current
20 func NewGoCollector() *goCollector {
22 goroutines: NewGauge(GaugeOpts{
25 Help: "Number of goroutines that currently exist.",
28 "go_gc_duration_seconds",
29 "A summary of the GC invocation durations.",
31 metrics: memStatsMetrics{
34 memstatNamespace("alloc_bytes"),
35 "Number of bytes allocated and still in use.",
38 eval: func(ms *runtime.MemStats) float64 { return float64(ms.Alloc) },
42 memstatNamespace("alloc_bytes_total"),
43 "Total number of bytes allocated, even if freed.",
46 eval: func(ms *runtime.MemStats) float64 { return float64(ms.TotalAlloc) },
47 valType: CounterValue,
50 memstatNamespace("sys_bytes"),
51 "Number of bytes obtained by system. Sum of all system allocations.",
54 eval: func(ms *runtime.MemStats) float64 { return float64(ms.Sys) },
58 memstatNamespace("lookups_total"),
59 "Total number of pointer lookups.",
62 eval: func(ms *runtime.MemStats) float64 { return float64(ms.Lookups) },
63 valType: CounterValue,
66 memstatNamespace("mallocs_total"),
67 "Total number of mallocs.",
70 eval: func(ms *runtime.MemStats) float64 { return float64(ms.Mallocs) },
71 valType: CounterValue,
74 memstatNamespace("frees_total"),
75 "Total number of frees.",
78 eval: func(ms *runtime.MemStats) float64 { return float64(ms.Frees) },
79 valType: CounterValue,
82 memstatNamespace("heap_alloc_bytes"),
83 "Number of heap bytes allocated and still in use.",
86 eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapAlloc) },
90 memstatNamespace("heap_sys_bytes"),
91 "Number of heap bytes obtained from system.",
94 eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapSys) },
98 memstatNamespace("heap_idle_bytes"),
99 "Number of heap bytes waiting to be used.",
102 eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapIdle) },
106 memstatNamespace("heap_inuse_bytes"),
107 "Number of heap bytes that are in use.",
110 eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapInuse) },
114 memstatNamespace("heap_released_bytes_total"),
115 "Total number of heap bytes released to OS.",
118 eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapReleased) },
119 valType: CounterValue,
122 memstatNamespace("heap_objects"),
123 "Number of allocated objects.",
126 eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapObjects) },
130 memstatNamespace("stack_inuse_bytes"),
131 "Number of bytes in use by the stack allocator.",
134 eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackInuse) },
138 memstatNamespace("stack_sys_bytes"),
139 "Number of bytes obtained from system for stack allocator.",
142 eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackSys) },
146 memstatNamespace("mspan_inuse_bytes"),
147 "Number of bytes in use by mspan structures.",
150 eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanInuse) },
154 memstatNamespace("mspan_sys_bytes"),
155 "Number of bytes used for mspan structures obtained from system.",
158 eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanSys) },
162 memstatNamespace("mcache_inuse_bytes"),
163 "Number of bytes in use by mcache structures.",
166 eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheInuse) },
170 memstatNamespace("mcache_sys_bytes"),
171 "Number of bytes used for mcache structures obtained from system.",
174 eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheSys) },
178 memstatNamespace("buck_hash_sys_bytes"),
179 "Number of bytes used by the profiling bucket hash table.",
182 eval: func(ms *runtime.MemStats) float64 { return float64(ms.BuckHashSys) },
186 memstatNamespace("gc_sys_bytes"),
187 "Number of bytes used for garbage collection system metadata.",
190 eval: func(ms *runtime.MemStats) float64 { return float64(ms.GCSys) },
194 memstatNamespace("other_sys_bytes"),
195 "Number of bytes used for other system allocations.",
198 eval: func(ms *runtime.MemStats) float64 { return float64(ms.OtherSys) },
202 memstatNamespace("next_gc_bytes"),
203 "Number of heap bytes when next garbage collection will take place.",
206 eval: func(ms *runtime.MemStats) float64 { return float64(ms.NextGC) },
210 memstatNamespace("last_gc_time_seconds"),
211 "Number of seconds since 1970 of last garbage collection.",
214 eval: func(ms *runtime.MemStats) float64 { return float64(ms.LastGC) / 1e9 },
221 func memstatNamespace(s string) string {
222 return fmt.Sprintf("go_memstats_%s", s)
225 // Describe returns all descriptions of the collector.
226 func (c *goCollector) Describe(ch chan<- *Desc) {
227 ch <- c.goroutines.Desc()
230 for _, i := range c.metrics {
235 // Collect returns the current state of all metrics of the collector.
236 func (c *goCollector) Collect(ch chan<- Metric) {
237 c.goroutines.Set(float64(runtime.NumGoroutine()))
240 var stats debug.GCStats
241 stats.PauseQuantiles = make([]time.Duration, 5)
242 debug.ReadGCStats(&stats)
244 quantiles := make(map[float64]float64)
245 for idx, pq := range stats.PauseQuantiles[1:] {
246 quantiles[float64(idx+1)/float64(len(stats.PauseQuantiles)-1)] = pq.Seconds()
248 quantiles[0.0] = stats.PauseQuantiles[0].Seconds()
249 ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), float64(stats.PauseTotal.Seconds()), quantiles)
251 ms := &runtime.MemStats{}
252 runtime.ReadMemStats(ms)
253 for _, i := range c.metrics {
254 ch <- MustNewConstMetric(i.desc, i.valType, i.eval(ms))
258 // memStatsMetrics provide description, value, and value type for memstat metrics.
259 type memStatsMetrics []struct {
261 eval func(*runtime.MemStats) float64