unembed memstats collector

This commit is contained in:
Kevin Pike 2015-10-02 18:10:36 -07:00
parent c945ed62c1
commit 8a031ee219
1 changed files with 216 additions and 230 deletions

View File

@ -11,13 +11,17 @@ type goCollector struct {
goroutines Gauge goroutines Gauge
gcDesc *Desc gcDesc *Desc
memstats *memStatCollector // memstats object to reuse
ms *runtime.MemStats
// metrics to describe and collect
metrics memStatsMetrics
} }
// NewGoCollector returns a collector which exports metrics about the current // NewGoCollector returns a collector which exports metrics about the current
// go process. // go process.
func NewGoCollector() *goCollector { func NewGoCollector() *goCollector {
return &goCollector{ return &goCollector{
ms: new(runtime.MemStats),
goroutines: NewGauge(GaugeOpts{ goroutines: NewGauge(GaugeOpts{
Namespace: "go", Namespace: "go",
Name: "goroutines", Name: "goroutines",
@ -27,210 +31,207 @@ func NewGoCollector() *goCollector {
"go_gc_duration_seconds", "go_gc_duration_seconds",
"A summary of the GC invocation durations.", "A summary of the GC invocation durations.",
nil, nil), nil, nil),
memstats: &memStatCollector{ metrics: memStatsMetrics{
ms: new(runtime.MemStats), {
metrics: memStatsMetrics{ desc: NewDesc(
{ memstatNamespace("alloc_bytes"),
desc: NewDesc( "Number of bytes allocated and still in use.",
memstatNamespace("alloc_bytes"), nil, nil,
"Number of bytes allocated and still in use.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.Alloc) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Alloc) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("alloc_bytes_total"),
desc: NewDesc( "Total number of bytes allocated, even if freed.",
memstatNamespace("alloc_bytes_total"), nil, nil,
"Total number of bytes allocated, even if freed.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.TotalAlloc) },
), valType: CounterValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.TotalAlloc) }, }, {
valType: CounterValue, desc: NewDesc(
}, { memstatNamespace("sys_bytes"),
desc: NewDesc( "Number of bytes obtained from system",
memstatNamespace("sys_bytes"), nil, nil,
"Number of bytes obtained from system", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.Sys) },
), valType: CounterValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Sys) }, }, {
valType: CounterValue, desc: NewDesc(
}, { memstatNamespace("lookups_total"),
desc: NewDesc( "Total number of pointer lookups.",
memstatNamespace("lookups_total"), nil, nil,
"Total number of pointer lookups.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.Lookups) },
), valType: CounterValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Lookups) }, }, {
valType: CounterValue, desc: NewDesc(
}, { memstatNamespace("mallocs_total"),
desc: NewDesc( "Total number of mallocs.",
memstatNamespace("mallocs_total"), nil, nil,
"Total number of mallocs.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.Mallocs) },
), valType: CounterValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Mallocs) }, }, {
valType: CounterValue, desc: NewDesc(
}, { memstatNamespace("frees_total"),
desc: NewDesc( "Total number of frees.",
memstatNamespace("frees_total"), nil, nil,
"Total number of frees.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.Frees) },
), valType: CounterValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Frees) }, }, {
valType: CounterValue, desc: NewDesc(
}, { memstatNamespace("heap_alloc_bytes"),
desc: NewDesc( "Number heap bytes allocated and still in use.",
memstatNamespace("heap_alloc_bytes"), nil, nil,
"Number heap bytes allocated and still in use.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapAlloc) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapAlloc) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("heap_sys_bytes"),
desc: NewDesc( "Total bytes in heap obtained from system.",
memstatNamespace("heap_sys_bytes"), nil, nil,
"Total bytes in heap obtained from system.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapSys) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapSys) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("heap_idle_bytes"),
desc: NewDesc( "Number bytes in heap waiting to be used.",
memstatNamespace("heap_idle_bytes"), nil, nil,
"Number bytes in heap waiting to be used.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapIdle) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapIdle) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("heap_inuse_bytes"),
desc: NewDesc( "Number of bytes in heap that are in use.",
memstatNamespace("heap_inuse_bytes"), nil, nil,
"Number of bytes in heap that are in use.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapInuse) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapInuse) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("heap_released_bytes"),
desc: NewDesc( "Number of bytes in heap released to OS.",
memstatNamespace("heap_released_bytes"), nil, nil,
"Number of bytes in heap released to OS.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapReleased) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapReleased) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("heap_objects"),
desc: NewDesc( "Number of allocated objects.",
memstatNamespace("heap_objects"), nil, nil,
"Number of allocated objects.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapObjects) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapObjects) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("stack_bytes_inuse"),
desc: NewDesc( "Number of bytes in use by the stack allocator.",
memstatNamespace("stack_bytes_inuse"), nil, nil,
"Number of bytes in use by the stack allocator.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackInuse) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackInuse) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("stack_sys_bytes"),
desc: NewDesc( "Number of bytes in obtained from system for stack allocator.",
memstatNamespace("stack_sys_bytes"), nil, nil,
"Number of bytes in obtained from system for stack allocator.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackSys) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackSys) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("mspan_inuse"),
desc: NewDesc( "Number of mspan structures in use.",
memstatNamespace("mspan_inuse"), nil, nil,
"Number of mspan structures in use.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanInuse) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanInuse) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("mspan_sys"),
desc: NewDesc( "Number of mspan structures obtained from system.",
memstatNamespace("mspan_sys"), nil, nil,
"Number of mspan structures obtained from system.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanSys) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanSys) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("mcache_inuse"),
desc: NewDesc( "Number of mcache structures in use.",
memstatNamespace("mcache_inuse"), nil, nil,
"Number of mcache structures in use.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheInuse) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheInuse) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("mcache_sys"),
desc: NewDesc( "Number of mcache structures obtained from system.",
memstatNamespace("mcache_sys"), nil, nil,
"Number of mcache structures obtained from system.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheSys) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheSys) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("buck_hash_sys"),
desc: NewDesc( "Profiling bucket hash table.",
memstatNamespace("buck_hash_sys"), nil, nil,
"Profiling bucket hash table.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.BuckHashSys) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.BuckHashSys) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("gc_metadata"),
desc: NewDesc( "GC metadata.",
memstatNamespace("gc_metadata"), nil, nil,
"GC metadata.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.GCSys) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.GCSys) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("other_sys"),
desc: NewDesc( "Other system allocations.",
memstatNamespace("other_sys"), nil, nil,
"Other system allocations.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.OtherSys) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.OtherSys) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("next_gc"),
desc: NewDesc( "Next collection will happen when HeapAlloc ≥ this amount.",
memstatNamespace("next_gc"), nil, nil,
"Next collection will happen when HeapAlloc ≥ this amount.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.NextGC) },
), valType: GaugeValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.NextGC) }, }, {
valType: GaugeValue, desc: NewDesc(
}, { memstatNamespace("last_gc"),
desc: NewDesc( "End time of last garbage collection (nanoseconds since 1970).",
memstatNamespace("last_gc"), nil, nil,
"End time of last garbage collection (nanoseconds since 1970).", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.LastGC) },
), valType: CounterValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.LastGC) }, }, {
valType: CounterValue, desc: NewDesc(
}, { memstatNamespace("pause_total"),
desc: NewDesc( "Total garbage collection pauses for all collections.",
memstatNamespace("pause_total"), nil, nil,
"Total garbage collection pauses for all collections.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.PauseTotalNs) },
), valType: CounterValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.PauseTotalNs) }, }, {
valType: CounterValue, desc: NewDesc(
}, { memstatNamespace("gc_total"),
desc: NewDesc( "Number of garbage collection.",
memstatNamespace("gc_total"), nil, nil,
"Number of garbage collection.", ),
nil, nil, eval: func(ms *runtime.MemStats) float64 { return float64(ms.NumGC) },
), valType: CounterValue,
eval: func(ms *runtime.MemStats) float64 { return float64(ms.NumGC) },
valType: CounterValue,
},
}, },
}, },
} }
@ -245,7 +246,9 @@ func (c *goCollector) Describe(ch chan<- *Desc) {
ch <- c.goroutines.Desc() ch <- c.goroutines.Desc()
ch <- c.gcDesc ch <- c.gcDesc
c.memstats.Describe(ch) for _, i := range c.metrics {
ch <- i.desc
}
} }
// Collect returns the current state of all metrics of the collector. // Collect returns the current state of all metrics of the collector.
@ -264,32 +267,15 @@ func (c *goCollector) Collect(ch chan<- Metric) {
quantiles[0.0] = stats.PauseQuantiles[0].Seconds() quantiles[0.0] = stats.PauseQuantiles[0].Seconds()
ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), float64(stats.PauseTotal.Seconds()), quantiles) ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), float64(stats.PauseTotal.Seconds()), quantiles)
c.memstats.Collect(ch)
}
// metrics that provide description, value, and value type for memstat metrics
type memStatsMetrics []struct {
desc *Desc
eval func(*runtime.MemStats) float64
valType ValueType
}
type memStatCollector struct {
// memstats object to reuse
ms *runtime.MemStats
// metrics to describe and collect
metrics memStatsMetrics
}
func (c *memStatCollector) Describe(ch chan<- *Desc) {
for _, i := range c.metrics {
ch <- i.desc
}
}
func (c *memStatCollector) Collect(ch chan<- Metric) {
runtime.ReadMemStats(c.ms) runtime.ReadMemStats(c.ms)
for _, i := range c.metrics { for _, i := range c.metrics {
ch <- MustNewConstMetric(i.desc, i.valType, i.eval(c.ms)) ch <- MustNewConstMetric(i.desc, i.valType, i.eval(c.ms))
} }
} }
// memStatsMetrics provide description, value, and value type for memstat metrics
type memStatsMetrics []struct {
desc *Desc
eval func(*runtime.MemStats) float64
valType ValueType
}