client_golang/prometheus/go_collector.go

51 lines
1.3 KiB
Go
Raw Normal View History

package prometheus
import (
"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
2015-04-30 07:27:14 +03:00
gcDesc *Desc
}
// NewGoCollector returns a collector which exports metrics about the current
// go process.
func NewGoCollector() *goCollector {
return &goCollector{
goroutines: NewGauge(GaugeOpts{
Name: "go_goroutines",
Help: "Number of goroutines that currently exist.",
}),
2015-04-30 07:27:14 +03:00
gcDesc: NewDesc(
"go_gc_duration_seconds",
"A summary of the GC invocation durations.",
nil, nil),
}
}
// Describe returns all descriptions of the collector.
func (c *goCollector) Describe(ch chan<- *Desc) {
ch <- c.goroutines.Desc()
2015-04-30 07:27:14 +03:00
ch <- c.gcDesc
}
// 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
2015-04-30 07:27:14 +03:00
var stats debug.GCStats
2015-05-05 02:20:11 +03:00
stats.PauseQuantiles = make([]time.Duration, 5)
2015-04-30 07:27:14 +03:00
debug.ReadGCStats(&stats)
2015-05-05 02:20:11 +03:00
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)
}