2021-11-03 11:21:54 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-07 19:35:39 +03:00
|
|
|
"go.unistack.org/micro/v4/client"
|
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/meter"
|
|
|
|
"go.unistack.org/micro/v4/meter/wrapper"
|
2021-11-03 11:21:54 +03:00
|
|
|
)
|
|
|
|
|
2024-04-04 16:36:48 +03:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func TestBuildName(t *testing.T) {
|
|
|
|
m := NewMeter()
|
|
|
|
check := `micro_foo{micro_aaa="b",micro_bar="baz",micro_ccc="d"}`
|
2022-03-11 01:24:14 +03:00
|
|
|
name := m.buildMetric("foo", "bar", "baz", "aaa", "b", "ccc", "d")
|
2021-11-03 11:21:54 +03:00
|
|
|
if name != check {
|
|
|
|
t.Fatalf("metric name error: %s != %s", name, check)
|
|
|
|
}
|
|
|
|
|
|
|
|
cnt := m.Counter("counter", "key", "val")
|
|
|
|
cnt.Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrapper(t *testing.T) {
|
|
|
|
m := NewMeter() // meter.Labels("test_key", "test_val"))
|
|
|
|
|
|
|
|
w := wrapper.NewClientWrapper(
|
|
|
|
wrapper.ServiceName("svc1"),
|
|
|
|
wrapper.ServiceVersion("0.0.1"),
|
|
|
|
wrapper.ServiceID("12345"),
|
|
|
|
wrapper.Meter(m),
|
|
|
|
)
|
2024-03-27 11:42:49 +03:00
|
|
|
_ = w
|
2021-11-03 11:21:54 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-03-27 11:42:49 +03:00
|
|
|
c := client.NewClient()
|
2021-11-03 11:21:54 +03:00
|
|
|
if err := c.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rsp := &codec.Frame{}
|
|
|
|
req := &codec.Frame{}
|
|
|
|
err := c.Call(ctx, c.NewRequest("svc2", "Service.Method", req), rsp)
|
|
|
|
_, _ = rsp, err
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
_ = m.Write(buf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`micro_client_request_inflight{micro_endpoint="svc2.Service.Method"} 0`)) {
|
|
|
|
t.Fatalf("invalid metrics output: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 19:09:31 +03:00
|
|
|
|
|
|
|
func TestMultiple(t *testing.T) {
|
|
|
|
m := NewMeter() // meter.Labels("test_key", "test_val"))
|
|
|
|
|
|
|
|
m.Counter("server", "endpoint", "ep1", "path", "/path1").Inc()
|
|
|
|
m.Counter("server", "endpoint", "ep1", "path", "/path1").Inc()
|
|
|
|
|
|
|
|
m.Counter("server", "endpoint", "ep2", "path", "/path2").Inc()
|
|
|
|
m.Counter("server", "endpoint", "ep2", "path", "/path2").Inc()
|
|
|
|
|
|
|
|
m.Counter("server", "endpoint", "ep3", "path", "/path3", "status", "success").Inc()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
_ = m.Write(buf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`micro_server{micro_endpoint="ep1",micro_path="/path1"} 2`)) {
|
|
|
|
// t.Fatal("XXXX")
|
|
|
|
t.Fatalf("invalid metrics output: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|