client_golang/prometheus/go_collector.go

264 lines
7.5 KiB
Go
Raw Normal View History

package prometheus
import (
"fmt"
"runtime"
2015-04-30 07:27:14 +03:00
"runtime/debug"
2015-05-05 02:20:11 +03:00
"time"
)
type goCollector struct {
goroutines Gauge
gcDesc *Desc
2015-10-03 04:10:36 +03:00
// metrics to describe and collect
metrics memStatsMetrics
}
// NewGoCollector returns a collector which exports metrics about the current
// go process.
func NewGoCollector() *goCollector {
return &goCollector{
goroutines: NewGauge(GaugeOpts{
Namespace: "go",
Name: "goroutines",
Help: "Number of goroutines that currently exist.",
}),
gcDesc: NewDesc(
"go_gc_duration_seconds",
"A summary of the GC invocation durations.",
nil, nil),
2015-10-03 04:10:36 +03:00
metrics: memStatsMetrics{
{
desc: NewDesc(
memstatNamespace("alloc_bytes"),
"Number of bytes allocated and still in use.",
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Alloc) },
valType: GaugeValue,
}, {
desc: NewDesc(
memstatNamespace("alloc_bytes_total"),
"Total number of bytes allocated, even if freed.",
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.TotalAlloc) },
valType: CounterValue,
}, {
desc: NewDesc(
memstatNamespace("sys_bytes"),
2015-10-05 19:27:28 +03:00
"Number of bytes obtained by system. Sum of all system allocations.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Sys) },
2015-10-05 19:27:28 +03:00
valType: GaugeValue,
2015-10-03 04:10:36 +03:00
}, {
desc: NewDesc(
memstatNamespace("lookups_total"),
"Total number of pointer lookups.",
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Lookups) },
valType: CounterValue,
}, {
desc: NewDesc(
memstatNamespace("mallocs_total"),
"Total number of mallocs.",
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Mallocs) },
valType: CounterValue,
}, {
desc: NewDesc(
memstatNamespace("frees_total"),
"Total number of frees.",
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Frees) },
valType: CounterValue,
}, {
desc: NewDesc(
memstatNamespace("heap_alloc_bytes"),
2015-10-05 19:27:28 +03:00
"Number of heap bytes allocated and still in use.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapAlloc) },
valType: GaugeValue,
}, {
desc: NewDesc(
memstatNamespace("heap_sys_bytes"),
2015-10-05 19:27:28 +03:00
"Number of heap bytes obtained from system.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapSys) },
valType: GaugeValue,
}, {
desc: NewDesc(
memstatNamespace("heap_idle_bytes"),
2015-10-05 19:27:28 +03:00
"Number of heap bytes waiting to be used.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapIdle) },
valType: GaugeValue,
}, {
desc: NewDesc(
memstatNamespace("heap_inuse_bytes"),
2015-10-05 19:27:28 +03:00
"Number of heap bytes that are in use.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapInuse) },
valType: GaugeValue,
}, {
desc: NewDesc(
memstatNamespace("heap_released_bytes"),
"Number of bytes in heap released to OS.",
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapReleased) },
valType: GaugeValue,
}, {
desc: NewDesc(
memstatNamespace("heap_objects"),
"Number of allocated objects.",
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapObjects) },
valType: GaugeValue,
}, {
desc: NewDesc(
2015-10-05 19:27:28 +03:00
memstatNamespace("stack_inuse_bytes"),
2015-10-03 04:10:36 +03:00
"Number of bytes in use by the stack allocator.",
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackInuse) },
valType: GaugeValue,
}, {
desc: NewDesc(
memstatNamespace("stack_sys_bytes"),
2015-10-05 19:27:28 +03:00
"Number of bytes obtained from system for stack allocator.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackSys) },
valType: GaugeValue,
}, {
desc: NewDesc(
2015-10-05 19:27:28 +03:00
memstatNamespace("mspan_inuse_bytes"),
"Number of bytes in use by mspan structures.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanInuse) },
valType: GaugeValue,
}, {
desc: NewDesc(
2015-10-05 19:27:28 +03:00
memstatNamespace("mspan_sys_bytes"),
"Number of bytes used for mspan structures obtained from system.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanSys) },
valType: GaugeValue,
}, {
desc: NewDesc(
2015-10-05 19:27:28 +03:00
memstatNamespace("mcache_inuse_bytes"),
"Number of bytes in use by mcache structures.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheInuse) },
valType: GaugeValue,
}, {
desc: NewDesc(
2015-10-05 19:27:28 +03:00
memstatNamespace("mcache_sys_bytes"),
"Number of bytes used for mcache structures obtained from system.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheSys) },
valType: GaugeValue,
}, {
desc: NewDesc(
2015-10-05 19:27:28 +03:00
memstatNamespace("buck_hash_sys_bytes"),
"Number of bytes used by the profiling bucket hash table.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.BuckHashSys) },
valType: GaugeValue,
}, {
desc: NewDesc(
2015-10-05 19:27:28 +03:00
memstatNamespace("gc_sys_bytes"),
"Number of bytes used for garbage collection system metadata.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.GCSys) },
valType: GaugeValue,
}, {
desc: NewDesc(
2015-10-05 19:27:28 +03:00
memstatNamespace("other_sys_bytes"),
"Number of bytes used for other system allocations.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.OtherSys) },
valType: GaugeValue,
}, {
desc: NewDesc(
2015-10-05 19:27:28 +03:00
memstatNamespace("next_gc_bytes"),
"Number of heap bytes when next garbage collection will take place.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.NextGC) },
valType: GaugeValue,
}, {
desc: NewDesc(
2015-10-05 19:27:28 +03:00
memstatNamespace("last_gc_time_seconds"),
"Number of seconds since 1970 of last garbage collection.",
2015-10-03 04:10:36 +03:00
nil, nil,
),
2015-10-05 19:27:28 +03:00
eval: func(ms *runtime.MemStats) float64 { return float64(ms.LastGC*10 ^ 9) },
valType: GaugeValue,
},
},
}
}
func memstatNamespace(s string) string {
return fmt.Sprintf("go_memstats_%s", s)
}
// Describe returns all descriptions of the collector.
func (c *goCollector) Describe(ch chan<- *Desc) {
ch <- c.goroutines.Desc()
ch <- c.gcDesc
2015-10-03 04:10:36 +03:00
for _, i := range c.metrics {
ch <- i.desc
}
}
// Collect returns the current state of all metrics of the collector.
func (c *goCollector) Collect(ch chan<- Metric) {
c.goroutines.Set(float64(runtime.NumGoroutine()))
ch <- c.goroutines
var stats debug.GCStats
stats.PauseQuantiles = make([]time.Duration, 5)
debug.ReadGCStats(&stats)
quantiles := make(map[float64]float64)
for idx, pq := range stats.PauseQuantiles[1:] {
quantiles[float64(idx+1)/float64(len(stats.PauseQuantiles)-1)] = pq.Seconds()
}
quantiles[0.0] = stats.PauseQuantiles[0].Seconds()
ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), float64(stats.PauseTotal.Seconds()), quantiles)
2015-10-05 17:37:59 +03:00
ms := &runtime.MemStats{}
runtime.ReadMemStats(ms)
2015-10-03 04:10:36 +03:00
for _, i := range c.metrics {
2015-10-05 17:37:59 +03:00
ch <- MustNewConstMetric(i.desc, i.valType, i.eval(ms))
2015-10-03 04:10:36 +03:00
}
}
2015-10-05 17:37:59 +03:00
// memStatsMetrics provide description, value, and value type for memstat metrics.
type memStatsMetrics []struct {
desc *Desc
eval func(*runtime.MemStats) float64
valType ValueType
}