fixup golang std metrics

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-04-04 16:36:48 +03:00
parent 4cd610a474
commit 1862ee5380
2 changed files with 17 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"hash/fnv" "hash/fnv"
"io" "io"
"regexp"
"sync" "sync"
"time" "time"
@ -280,8 +281,10 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...options.Option) error {
} }
if options.WriteProcessMetrics || options.WriteFDMetrics { if options.WriteProcessMetrics || options.WriteFDMetrics {
c := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}) pc := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})
_ = m.set.Register(c) _ = m.set.Register(pc)
gc := collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")}))
_ = m.set.Register(gc)
} }
g, ok := m.set.(prometheus.Gatherer) g, ok := m.set.(prometheus.Gatherer)

View File

@ -11,6 +11,18 @@ import (
"go.unistack.org/micro/v4/meter/wrapper" "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) { func TestBuildName(t *testing.T) {
m := NewMeter() m := NewMeter()
check := `micro_foo{micro_aaa="b",micro_bar="baz",micro_ccc="d"}` check := `micro_foo{micro_aaa="b",micro_bar="baz",micro_ccc="d"}`