2019-01-25 17:27:27 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-01-26 12:33:51 +03:00
|
|
|
"testing"
|
|
|
|
|
2019-01-25 17:27:27 +03:00
|
|
|
"github.com/micro/go-micro/client"
|
2019-06-21 17:24:15 +03:00
|
|
|
"github.com/micro/go-micro/client/selector"
|
2019-06-27 11:49:39 +03:00
|
|
|
"github.com/micro/go-micro/registry/memory"
|
2019-01-25 17:27:27 +03:00
|
|
|
"github.com/micro/go-micro/server"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("test error")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrometheusMetrics(t *testing.T) {
|
|
|
|
// setup
|
|
|
|
registry := memory.NewRegistry()
|
|
|
|
sel := selector.NewSelector(selector.Registry(registry))
|
2019-01-26 12:33:51 +03:00
|
|
|
|
2019-01-25 17:27:27 +03:00
|
|
|
name := "test"
|
2019-01-26 12:33:51 +03:00
|
|
|
id := "id-1234567890"
|
|
|
|
version := "1.2.3.4"
|
|
|
|
|
|
|
|
md := make(map[string]string)
|
|
|
|
md["dc"] = "dc1"
|
|
|
|
md["node"] = "node1"
|
2019-01-25 17:27:27 +03:00
|
|
|
|
|
|
|
c := client.NewClient(client.Selector(sel))
|
|
|
|
s := server.NewServer(
|
|
|
|
server.Name(name),
|
2019-01-26 12:33:51 +03:00
|
|
|
server.Version(version),
|
|
|
|
server.Id(id),
|
2019-01-25 17:27:27 +03:00
|
|
|
server.Registry(registry),
|
2019-01-26 12:33:51 +03:00
|
|
|
server.WrapHandler(
|
|
|
|
NewHandlerWrapper(
|
|
|
|
server.Metadata(md),
|
|
|
|
server.Name(name),
|
|
|
|
server.Version(version),
|
|
|
|
server.Id(id),
|
|
|
|
),
|
|
|
|
),
|
2019-01-25 17:27:27 +03:00
|
|
|
)
|
|
|
|
|
2019-01-25 18:45:41 +03:00
|
|
|
defer s.Stop()
|
|
|
|
|
2019-01-25 17:27:27 +03:00
|
|
|
type Test struct {
|
|
|
|
*testHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Handle(
|
|
|
|
s.NewHandler(&Test{new(testHandler)}),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
t.Fatalf("Unexpected error starting server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := c.NewRequest(name, "Test.Method", &TestRequest{IsError: false}, client.WithContentType("application/json"))
|
|
|
|
rsp := TestResponse{}
|
|
|
|
|
|
|
|
assert.NoError(t, c.Call(context.TODO(), req, &rsp))
|
|
|
|
|
|
|
|
req = c.NewRequest(name, "Test.Method", &TestRequest{IsError: true}, client.WithContentType("application/json"))
|
|
|
|
assert.Error(t, c.Call(context.TODO(), req, &rsp))
|
|
|
|
|
|
|
|
list, _ := prometheus.DefaultGatherer.Gather()
|
|
|
|
|
2019-01-28 17:47:32 +03:00
|
|
|
metric := findMetricByName(list, dto.MetricType_SUMMARY, "micro_upstream_latency_microseconds")
|
2019-01-26 12:33:51 +03:00
|
|
|
|
2019-02-04 01:26:31 +03:00
|
|
|
if metric == nil || metric.Metric == nil || len(metric.Metric) == 0 {
|
|
|
|
t.Fatalf("no metrics returned")
|
|
|
|
}
|
|
|
|
|
2019-01-26 12:33:51 +03:00
|
|
|
for _, v := range metric.Metric[0].Label {
|
|
|
|
switch *v.Name {
|
|
|
|
case "micro_dc":
|
|
|
|
assert.Equal(t, "dc1", *v.Value)
|
|
|
|
case "micro_node":
|
|
|
|
assert.Equal(t, "node1", *v.Value)
|
|
|
|
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 "method":
|
|
|
|
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)
|
2019-01-25 17:27:27 +03:00
|
|
|
assert.True(t, *metric.Metric[0].Summary.SampleSum > 0)
|
|
|
|
|
2019-01-28 17:47:32 +03:00
|
|
|
metric = findMetricByName(list, dto.MetricType_HISTOGRAM, "micro_request_duration_seconds")
|
|
|
|
|
|
|
|
for _, v := range metric.Metric[0].Label {
|
|
|
|
switch *v.Name {
|
|
|
|
case "micro_dc":
|
|
|
|
assert.Equal(t, "dc1", *v.Value)
|
|
|
|
case "micro_node":
|
|
|
|
assert.Equal(t, "node1", *v.Value)
|
|
|
|
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 "method":
|
|
|
|
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)
|
|
|
|
|
2019-01-25 19:12:57 +03:00
|
|
|
metric = findMetricByName(list, dto.MetricType_COUNTER, "micro_request_total")
|
2019-01-25 17:27:27 +03:00
|
|
|
|
2019-01-26 12:33:51 +03:00
|
|
|
for _, v := range metric.Metric[0].Label {
|
|
|
|
switch *v.Name {
|
|
|
|
case "micro_dc":
|
|
|
|
assert.Equal(t, "dc1", *v.Value)
|
|
|
|
case "micro_node":
|
|
|
|
assert.Equal(t, "node1", *v.Value)
|
|
|
|
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 "method":
|
|
|
|
assert.Equal(t, "Test.Method", *v.Value)
|
|
|
|
case "status":
|
|
|
|
assert.Equal(t, "fail", *v.Value)
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 17:27:27 +03:00
|
|
|
assert.Equal(t, *metric.Metric[0].Counter.Value, float64(1))
|
|
|
|
|
2019-01-26 12:33:51 +03:00
|
|
|
for _, v := range metric.Metric[1].Label {
|
|
|
|
switch *v.Name {
|
|
|
|
case "dc":
|
|
|
|
assert.Equal(t, "dc1", *v.Value)
|
|
|
|
case "node":
|
|
|
|
assert.Equal(t, "node1", *v.Value)
|
|
|
|
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 "method":
|
|
|
|
assert.Equal(t, "Test.Method", *v.Value)
|
|
|
|
case "status":
|
|
|
|
assert.Equal(t, "success", *v.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 17:27:27 +03:00
|
|
|
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
|
|
|
|
}
|