From 1862ee5380786230ec4a6c7a5d6fc2283058ce1f Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 4 Apr 2024 16:36:48 +0300 Subject: [PATCH] fixup golang std metrics Signed-off-by: Vasiliy Tolstov --- prometheus.go | 7 +++++-- prometheus_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/prometheus.go b/prometheus.go index 0e18ab0..f041c91 100644 --- a/prometheus.go +++ b/prometheus.go @@ -4,6 +4,7 @@ import ( "fmt" "hash/fnv" "io" + "regexp" "sync" "time" @@ -280,8 +281,10 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...options.Option) error { } if options.WriteProcessMetrics || options.WriteFDMetrics { - c := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}) - _ = m.set.Register(c) + pc := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}) + _ = m.set.Register(pc) + gc := collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")})) + _ = m.set.Register(gc) } g, ok := m.set.(prometheus.Gatherer) diff --git a/prometheus_test.go b/prometheus_test.go index 0f1c2ac..3fc2505 100644 --- a/prometheus_test.go +++ b/prometheus_test.go @@ -11,6 +11,18 @@ import ( "go.unistack.org/micro/v4/meter/wrapper" ) +func TestStd(t *testing.T) { + m := NewMeter(meter.WriteProcessMetrics(true), meter.WriteFDMetrics(true)) + if err := m.Init(); err != nil { + t.Fatal(err) + } + buf := bytes.NewBuffer(nil) + _ = m.Write(buf) + if !bytes.Contains(buf.Bytes(), []byte(`go_goroutine`)) { + t.Fatalf("invalid metrics output: %s", buf.Bytes()) + } +} + func TestBuildName(t *testing.T) { m := NewMeter() check := `micro_foo{micro_aaa="b",micro_bar="baz",micro_ccc="d"}`