2021-01-27 00:56:50 +03:00
|
|
|
package victoriametrics
|
|
|
|
|
|
|
|
import (
|
2021-07-21 16:37:51 +03:00
|
|
|
"bytes"
|
2021-02-18 16:01:13 +03:00
|
|
|
"context"
|
2021-01-27 00:56:50 +03:00
|
|
|
"testing"
|
|
|
|
|
2023-05-07 19:26:50 +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-01-27 00:56:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildName(t *testing.T) {
|
|
|
|
m := NewMeter()
|
|
|
|
im := m.(*victoriametricsMeter)
|
2021-10-31 14:46:40 +03:00
|
|
|
check := `micro_foo{micro_aaa="b",micro_bar="baz",micro_ccc="d"}`
|
|
|
|
name := im.buildName("foo", "bar", "baz", "aaa", "b", "ccc", "d")
|
2021-01-27 00:56:50 +03:00
|
|
|
if name != check {
|
|
|
|
t.Fatalf("metric name error: %s != %s", name, check)
|
|
|
|
}
|
|
|
|
|
2021-04-28 01:13:08 +03:00
|
|
|
cnt := m.Counter("counter", "key", "val")
|
2021-01-27 00:56:50 +03:00
|
|
|
cnt.Inc()
|
2021-02-18 16:01:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrapper(t *testing.T) {
|
2021-10-10 00:35:16 +03:00
|
|
|
m := NewMeter() // meter.Labels("test_key", "test_val"))
|
2021-02-18 16:01:13 +03:00
|
|
|
|
|
|
|
w := wrapper.NewClientWrapper(
|
|
|
|
wrapper.ServiceName("svc1"),
|
|
|
|
wrapper.ServiceVersion("0.0.1"),
|
|
|
|
wrapper.ServiceID("12345"),
|
|
|
|
wrapper.Meter(m),
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
c := client.NewClient(client.Wrap(w))
|
|
|
|
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
|
2021-07-21 16:37:51 +03:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2021-10-10 00:35:16 +03:00
|
|
|
_ = m.Write(buf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
|
2021-07-21 16:37:51 +03:00
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`micro_client_request_inflight{micro_endpoint="svc2.Service.Method"} 0`)) {
|
|
|
|
t.Fatalf("invalid metrics output: %s", buf.Bytes())
|
|
|
|
}
|
2021-01-27 00:56:50 +03:00
|
|
|
}
|