2021-11-03 11:21:54 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2024-10-06 13:45:33 +03:00
|
|
|
"fmt"
|
2021-11-03 11:21:54 +03:00
|
|
|
"testing"
|
|
|
|
|
2024-10-06 13:45:33 +03:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
"github.com/prometheus/common/expfmt"
|
2021-11-03 11:21:54 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/meter"
|
|
|
|
)
|
|
|
|
|
2024-11-06 00:00:17 +03:00
|
|
|
func TestHash(t *testing.T) {
|
|
|
|
t.Skip()
|
|
|
|
h1 := newHash("micro_server_request_total", []string{"code", "16", "endpoint", "/clientprofile.ClientProfileService/GetClientProfile", "status", "failure"})
|
|
|
|
h2 := newHash("micro_server_request_total", []string{"code", "16", "endpoint", "/clientproduct.ClientProductService/GetDepositProducts", "status", "failure"})
|
|
|
|
h3 := newHash("micro_server_request_total", []string{"code", "16", "endpoint", "/operationsinfo.OperationsInfoService/GetOperations", "status", "failure"})
|
|
|
|
t.Logf("h1: %v\nh2: %v\nh3: %v\n", h1, h2, h3)
|
|
|
|
}
|
|
|
|
|
2024-10-06 13:45:33 +03:00
|
|
|
func TestHistogram(t *testing.T) {
|
|
|
|
m := NewMeter()
|
|
|
|
name := "test"
|
|
|
|
m.Histogram(name, "endpoint").Update(1)
|
|
|
|
m.Histogram(name, "endpoint").Update(1)
|
|
|
|
m.Histogram(name, "endpoint").Update(5)
|
|
|
|
m.Histogram(name, "endpoint").Update(10)
|
|
|
|
m.Histogram(name, "endpoint").Update(10)
|
|
|
|
m.Histogram(name, "endpoint").Update(30)
|
|
|
|
mbuf := bytes.NewBuffer(nil)
|
|
|
|
_ = m.Write(mbuf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
|
|
|
|
|
|
|
|
/*
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`micro_server_sum{endpoint="ep1",path="/path1"} 20`)) {
|
|
|
|
t.Fatalf("invalid metrics output: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`micro_server_count{endpoint="ep1",path="/path1"} 2`)) {
|
|
|
|
t.Fatalf("invalid metrics output: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
p := prometheus.NewHistogram(prometheus.HistogramOpts{Name: name})
|
|
|
|
p.Observe(1)
|
|
|
|
p.Observe(1)
|
|
|
|
p.Observe(5)
|
|
|
|
p.Observe(10)
|
|
|
|
p.Observe(10)
|
|
|
|
p.Observe(30)
|
|
|
|
mdto := &dto.Metric{}
|
|
|
|
p.Write(mdto)
|
|
|
|
pbuf := bytes.NewBuffer(nil)
|
|
|
|
enc := expfmt.NewEncoder(pbuf, expfmt.NewFormat(expfmt.TypeTextPlain))
|
|
|
|
mf := &dto.MetricFamily{Name: &name, Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{mdto}}
|
|
|
|
_ = enc.Encode(mf)
|
|
|
|
|
|
|
|
if !bytes.Equal(mbuf.Bytes(), pbuf.Bytes()) {
|
|
|
|
fmt.Printf("m\n%s\n", mbuf.Bytes())
|
|
|
|
fmt.Printf("m\n%s\n", pbuf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSummary(t *testing.T) {
|
2024-11-06 00:00:17 +03:00
|
|
|
t.Skip()
|
2024-10-06 13:45:33 +03:00
|
|
|
name := "micro_server"
|
|
|
|
m := NewMeter()
|
|
|
|
m.Summary("micro_server").Update(1)
|
|
|
|
m.Summary("micro_server").Update(1)
|
|
|
|
m.Summary("micro_server").Update(5)
|
|
|
|
m.Summary("micro_server").Update(10)
|
|
|
|
m.Summary("micro_server").Update(10)
|
|
|
|
m.Summary("micro_server").Update(30)
|
|
|
|
mbuf := bytes.NewBuffer(nil)
|
|
|
|
_ = m.Write(mbuf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
|
|
|
|
|
|
|
|
if !bytes.Contains(mbuf.Bytes(), []byte(`micro_server_sum 57`)) {
|
|
|
|
t.Fatalf("invalid metrics output: %s", mbuf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Contains(mbuf.Bytes(), []byte(`micro_server_count 6`)) {
|
|
|
|
t.Fatalf("invalid metrics output: %s", mbuf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
objectives := make(map[float64]float64)
|
|
|
|
for _, c := range meter.DefaultSummaryQuantiles {
|
|
|
|
objectives[c] = c
|
|
|
|
}
|
|
|
|
p := prometheus.NewSummary(prometheus.SummaryOpts{Name: name, Objectives: objectives, MaxAge: meter.DefaultSummaryWindow})
|
|
|
|
p.Observe(1)
|
|
|
|
p.Observe(1)
|
|
|
|
p.Observe(5)
|
|
|
|
p.Observe(10)
|
|
|
|
p.Observe(10)
|
|
|
|
p.Observe(30)
|
|
|
|
mdto := &dto.Metric{}
|
2024-11-06 00:00:17 +03:00
|
|
|
_ = p.Write(mdto)
|
2024-10-06 13:45:33 +03:00
|
|
|
pbuf := bytes.NewBuffer(nil)
|
|
|
|
enc := expfmt.NewEncoder(pbuf, expfmt.NewFormat(expfmt.TypeTextPlain))
|
|
|
|
mf := &dto.MetricFamily{Name: &name, Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{mdto}}
|
|
|
|
_ = enc.Encode(mf)
|
|
|
|
|
|
|
|
if !bytes.Equal(mbuf.Bytes(), pbuf.Bytes()) {
|
|
|
|
fmt.Printf("m\n%s\n", mbuf.Bytes())
|
|
|
|
fmt.Printf("m\n%s\n", pbuf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 TestWrapper(t *testing.T) {
|
|
|
|
m := NewMeter() // meter.Labels("test_key", "test_val"))
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-09-20 18:50:08 +03:00
|
|
|
c := client.NewClient(client.Meter(m))
|
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))
|
2024-09-20 18:50:08 +03:00
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`micro_client_request_inflight{endpoint="Service.Method"} 0`)) {
|
2021-11-03 11:21:54 +03:00
|
|
|
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"))
|
|
|
|
|
2024-09-20 18:50:08 +03:00
|
|
|
m.Counter("micro_server", "endpoint", "ep1", "path", "/path1").Inc()
|
|
|
|
m.Counter("micro_server", "endpoint", "ep1", "path", "/path1").Inc()
|
2022-03-05 19:09:31 +03:00
|
|
|
|
2024-09-20 18:50:08 +03:00
|
|
|
m.Counter("micro_server", "endpoint", "ep2", "path", "/path2").Inc()
|
|
|
|
m.Counter("micro_server", "endpoint", "ep2", "path", "/path2").Inc()
|
2022-03-05 19:09:31 +03:00
|
|
|
|
2024-09-20 18:50:08 +03:00
|
|
|
m.Counter("micro_server", "endpoint", "ep3", "path", "/path3", "status", "success").Inc()
|
2022-03-05 19:09:31 +03:00
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
_ = m.Write(buf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
|
2024-09-20 18:50:08 +03:00
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`micro_server{endpoint="ep1",path="/path1"} 2`)) {
|
2024-10-06 13:45:33 +03:00
|
|
|
t.Fatalf("invalid metrics output: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`micro_server{endpoint="ep3",path="/path3",status="success"} 1`)) {
|
2022-03-05 19:09:31 +03:00
|
|
|
t.Fatalf("invalid metrics output: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|