goCollector: add thread count gauge in goCollector

Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
This commit is contained in:
Peng Gao 2017-02-13 19:34:45 +08:00
parent c317fb7474
commit fa1cd67d1e
2 changed files with 15 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import (
type goCollector struct { type goCollector struct {
goroutines Gauge goroutines Gauge
threads Gauge
gcDesc *Desc gcDesc *Desc
// metrics to describe and collect // metrics to describe and collect
@ -24,6 +25,11 @@ func NewGoCollector() Collector {
Name: "goroutines", Name: "goroutines",
Help: "Number of goroutines that currently exist.", Help: "Number of goroutines that currently exist.",
}), }),
threads: NewGauge(GaugeOpts{
Namespace: "go",
Name: "threads",
Help: "Number of threads created.",
}),
gcDesc: NewDesc( gcDesc: NewDesc(
"go_gc_duration_seconds", "go_gc_duration_seconds",
"A summary of the GC invocation durations.", "A summary of the GC invocation durations.",
@ -225,8 +231,8 @@ func memstatNamespace(s string) string {
// Describe returns all descriptions of the collector. // Describe returns all descriptions of the collector.
func (c *goCollector) Describe(ch chan<- *Desc) { func (c *goCollector) Describe(ch chan<- *Desc) {
ch <- c.goroutines.Desc() ch <- c.goroutines.Desc()
ch <- c.threads.Desc()
ch <- c.gcDesc ch <- c.gcDesc
for _, i := range c.metrics { for _, i := range c.metrics {
ch <- i.desc ch <- i.desc
} }
@ -236,6 +242,9 @@ func (c *goCollector) Describe(ch chan<- *Desc) {
func (c *goCollector) Collect(ch chan<- Metric) { func (c *goCollector) Collect(ch chan<- Metric) {
c.goroutines.Set(float64(runtime.NumGoroutine())) c.goroutines.Set(float64(runtime.NumGoroutine()))
ch <- c.goroutines ch <- c.goroutines
n, _ := runtime.ThreadCreateProfile(nil)
c.threads.Set(float64(n))
ch <- c.threads
var stats debug.GCStats var stats debug.GCStats
stats.PauseQuantiles = make([]time.Duration, 5) stats.PauseQuantiles = make([]time.Duration, 5)

View File

@ -33,6 +33,9 @@ func TestGoCollector(t *testing.T) {
switch m := metric.(type) { switch m := metric.(type) {
// Attention, this also catches Counter... // Attention, this also catches Counter...
case Gauge: case Gauge:
if m.Desc().fqName != "go_goroutines" {
continue
}
pb := &dto.Metric{} pb := &dto.Metric{}
m.Write(pb) m.Write(pb)
if pb.GetGauge() == nil { if pb.GetGauge() == nil {
@ -50,10 +53,11 @@ func TestGoCollector(t *testing.T) {
t.Errorf("want 1 new goroutine, got %d", diff) t.Errorf("want 1 new goroutine, got %d", diff)
} }
// GoCollector performs two sends per call. // GoCollector performs three sends per call.
// On line 27 we need to receive the second send // On line 27 we need to receive the second send
// to shut down cleanly. // to shut down cleanly.
<-ch <-ch
<-ch
return return
} }
case <-time.After(1 * time.Second): case <-time.After(1 * time.Second):