2021-10-27 01:24:35 +03:00
|
|
|
//go:build ignore
|
2021-08-01 12:51:38 +03:00
|
|
|
// +build ignore
|
|
|
|
|
2020-11-26 08:57:54 +03:00
|
|
|
package prometheus_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-10-27 01:24:35 +03:00
|
|
|
cli "go.unistack.org/micro-client-grpc/v3"
|
|
|
|
jsoncodec "go.unistack.org/micro-codec-json/v3"
|
|
|
|
promwrapper "go.unistack.org/micro-metrics-prometheus/v3"
|
|
|
|
rrouter "go.unistack.org/micro-router-register/v3"
|
|
|
|
srv "go.unistack.org/micro-server-grpc/v3"
|
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/errors"
|
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/router"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2020-11-26 08:57:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Test interface {
|
|
|
|
Method(ctx context.Context, in *TestRequest, opts ...client.CallOption) (*TestResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestRequest struct {
|
|
|
|
IsError bool
|
|
|
|
}
|
|
|
|
type TestResponse struct{}
|
|
|
|
|
|
|
|
type testHandler struct{}
|
|
|
|
|
|
|
|
func (t *testHandler) Method(ctx context.Context, req *TestRequest, rsp *TestResponse) error {
|
|
|
|
if req.IsError {
|
2021-04-29 22:51:46 +03:00
|
|
|
return &errors.Error{Id: "prometheus test error"}
|
2020-11-26 08:57:54 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrometheusMetrics(t *testing.T) {
|
|
|
|
// setup
|
2021-02-12 20:50:02 +03:00
|
|
|
reg := register.NewRegister()
|
|
|
|
brk := broker.NewBroker(broker.Register(reg))
|
2020-11-26 08:57:54 +03:00
|
|
|
|
|
|
|
name := "test"
|
|
|
|
id := "id-1234567890"
|
|
|
|
version := "1.2.3.4"
|
2021-01-29 16:47:26 +03:00
|
|
|
rt := rrouter.NewRouter(router.Register(reg))
|
2020-11-26 08:57:54 +03:00
|
|
|
|
|
|
|
c := cli.NewClient(
|
|
|
|
client.Codec("application/grpc+json", jsoncodec.NewCodec()),
|
|
|
|
client.Codec("application/json", jsoncodec.NewCodec()),
|
|
|
|
client.Router(rt),
|
|
|
|
)
|
|
|
|
s := srv.NewServer(
|
|
|
|
server.Codec("application/grpc+json", jsoncodec.NewCodec()),
|
|
|
|
server.Codec("application/json", jsoncodec.NewCodec()),
|
|
|
|
server.Name(name),
|
|
|
|
server.Version(version),
|
2021-04-29 22:51:46 +03:00
|
|
|
server.ID(id),
|
2021-01-29 16:47:26 +03:00
|
|
|
server.Register(reg),
|
2020-11-26 08:57:54 +03:00
|
|
|
server.Broker(brk),
|
|
|
|
server.WrapHandler(
|
|
|
|
promwrapper.NewHandlerWrapper(
|
|
|
|
promwrapper.ServiceName(name),
|
|
|
|
promwrapper.ServiceVersion(version),
|
|
|
|
promwrapper.ServiceID(id),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := s.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
type Test struct {
|
|
|
|
*testHandler
|
|
|
|
}
|
|
|
|
|
2021-08-01 12:51:38 +03:00
|
|
|
if err := s.Handle(s.NewHandler(&Test{new(testHandler)})); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-26 08:57:54 +03:00
|
|
|
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
t.Fatalf("Unexpected error starting server: %v", err)
|
|
|
|
}
|
2021-08-01 12:51:38 +03:00
|
|
|
defer func() {
|
|
|
|
_ = s.Stop()
|
|
|
|
}()
|
2020-11-26 08:57:54 +03:00
|
|
|
|
2021-04-10 01:32:25 +03:00
|
|
|
req := c.NewRequest(name, "Test.Method", &TestRequest{IsError: false}, client.RequestContentType("application/json"))
|
2020-11-26 08:57:54 +03:00
|
|
|
rsp := TestResponse{}
|
|
|
|
|
|
|
|
assert.NoError(t, c.Call(context.TODO(), req, &rsp))
|
|
|
|
|
2021-04-10 01:32:25 +03:00
|
|
|
req = c.NewRequest(name, "Test.Method", &TestRequest{IsError: true}, client.RequestContentType("application/json"))
|
2020-11-26 08:57:54 +03:00
|
|
|
assert.Error(t, c.Call(context.TODO(), req, &rsp))
|
|
|
|
|
|
|
|
list, _ := prometheus.DefaultGatherer.Gather()
|
|
|
|
|
|
|
|
metric := findMetricByName(list, dto.MetricType_SUMMARY, "micro_server_latency_microseconds")
|
|
|
|
|
|
|
|
if metric == nil || metric.Metric == nil || len(metric.Metric) == 0 {
|
|
|
|
t.Fatalf("no metrics returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range metric.Metric[0].Label {
|
|
|
|
switch *v.Name {
|
|
|
|
case "micro_version":
|
|
|
|
assert.Equal(t, version, *v.Value)
|
|
|
|
case "micro_id":
|
|
|
|
assert.Equal(t, id, *v.Value)
|
|
|
|
case "micro_name":
|
|
|
|
assert.Equal(t, name, *v.Value)
|
|
|
|
case "micro_endpoint":
|
|
|
|
assert.Equal(t, "Test.Method", *v.Value)
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown %v with %v", *v.Name, *v.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, uint64(2), *metric.Metric[0].Summary.SampleCount)
|
|
|
|
assert.True(t, *metric.Metric[0].Summary.SampleSum > 0)
|
|
|
|
|
|
|
|
metric = findMetricByName(list, dto.MetricType_HISTOGRAM, "micro_server_request_duration_seconds")
|
|
|
|
|
|
|
|
for _, v := range metric.Metric[0].Label {
|
|
|
|
switch *v.Name {
|
|
|
|
case "micro_version":
|
|
|
|
assert.Equal(t, version, *v.Value)
|
|
|
|
case "micro_id":
|
|
|
|
assert.Equal(t, id, *v.Value)
|
|
|
|
case "micro_name":
|
|
|
|
assert.Equal(t, name, *v.Value)
|
|
|
|
case "micro_endpoint":
|
|
|
|
assert.Equal(t, "Test.Method", *v.Value)
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown %v with %v", *v.Name, *v.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, uint64(2), *metric.Metric[0].Histogram.SampleCount)
|
|
|
|
assert.True(t, *metric.Metric[0].Histogram.SampleSum > 0)
|
|
|
|
|
|
|
|
metric = findMetricByName(list, dto.MetricType_COUNTER, "micro_server_request_total")
|
|
|
|
|
|
|
|
for _, v := range metric.Metric[0].Label {
|
|
|
|
switch *v.Name {
|
|
|
|
case "micro_version":
|
|
|
|
assert.Equal(t, version, *v.Value)
|
|
|
|
case "micro_id":
|
|
|
|
assert.Equal(t, id, *v.Value)
|
|
|
|
case "micro_name":
|
|
|
|
assert.Equal(t, name, *v.Value)
|
|
|
|
case "micro_endpoint":
|
|
|
|
assert.Equal(t, "Test.Method", *v.Value)
|
|
|
|
case "micro_status":
|
|
|
|
assert.Equal(t, "failure", *v.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, *metric.Metric[0].Counter.Value, float64(1))
|
|
|
|
|
|
|
|
for _, v := range metric.Metric[1].Label {
|
|
|
|
switch *v.Name {
|
|
|
|
case "micro_version":
|
|
|
|
assert.Equal(t, version, *v.Value)
|
|
|
|
case "micro_id":
|
|
|
|
assert.Equal(t, id, *v.Value)
|
|
|
|
case "micro_name":
|
|
|
|
assert.Equal(t, name, *v.Value)
|
|
|
|
case "micro_endpoint":
|
|
|
|
assert.Equal(t, "Test.Method", *v.Value)
|
|
|
|
case "micro_status":
|
|
|
|
assert.Equal(t, "success", *v.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, *metric.Metric[1].Counter.Value, float64(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
func findMetricByName(list []*dto.MetricFamily, tp dto.MetricType, name string) *dto.MetricFamily {
|
|
|
|
for _, metric := range list {
|
|
|
|
if *metric.Name == name && *metric.Type == tp {
|
|
|
|
return metric
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|