2019-12-04 01:49:07 +03:00
|
|
|
package victoriametrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
metrics "github.com/VictoriaMetrics/metrics"
|
2020-01-31 01:26:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/registry/memory"
|
2020-07-17 01:29:28 +03:00
|
|
|
"github.com/micro/go-micro/v2/router"
|
|
|
|
rrouter "github.com/micro/go-micro/v2/router/registry"
|
2020-01-31 01:26:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/server"
|
2019-12-04 01:49:07 +03:00
|
|
|
"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 TestVictoriametrics(t *testing.T) {
|
|
|
|
// setup
|
|
|
|
registry := memory.NewRegistry()
|
|
|
|
|
|
|
|
name := "test"
|
|
|
|
id := "id-1234567890"
|
|
|
|
version := "1.2.3.4"
|
|
|
|
|
2020-07-17 01:29:28 +03:00
|
|
|
c := client.NewClient(
|
|
|
|
client.Router(rrouter.NewRouter(router.Registry(registry))),
|
|
|
|
)
|
2019-12-04 01:49:07 +03:00
|
|
|
s := server.NewServer(
|
|
|
|
server.Name(name),
|
|
|
|
server.Version(version),
|
|
|
|
server.Id(id),
|
|
|
|
server.Registry(registry),
|
|
|
|
server.WrapHandler(
|
|
|
|
NewHandlerWrapper(
|
2020-06-20 17:09:22 +03:00
|
|
|
ServiceName(name),
|
|
|
|
ServiceVersion(version),
|
|
|
|
ServiceID(id),
|
2019-12-04 01:49:07 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
metrics.WritePrometheus(buf, false)
|
|
|
|
|
|
|
|
metric, err := findMetricByName(buf, "sum", "micro_request_total")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := metric[0]["labels"].(map[string]string)
|
|
|
|
for k, v := range labels {
|
|
|
|
switch k {
|
|
|
|
case "micro_version":
|
|
|
|
assert.Equal(t, version, v)
|
|
|
|
case "micro_id":
|
|
|
|
assert.Equal(t, id, v)
|
|
|
|
case "micro_name":
|
|
|
|
assert.Equal(t, name, v)
|
2020-06-20 17:09:22 +03:00
|
|
|
case "micro_endpoint":
|
2019-12-04 01:49:07 +03:00
|
|
|
assert.Equal(t, "Test.Method", v)
|
2020-06-20 17:09:22 +03:00
|
|
|
case "micro_status":
|
2019-12-04 01:49:07 +03:00
|
|
|
continue
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown %v with %v", k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func findMetricByName(buf io.Reader, tp string, name string) ([]map[string]interface{}, error) {
|
|
|
|
var metrics []map[string]interface{}
|
|
|
|
scanner := bufio.NewScanner(buf)
|
|
|
|
for scanner.Scan() {
|
|
|
|
txt := scanner.Text()
|
|
|
|
if strings.HasPrefix(txt, name) {
|
|
|
|
mt := make(map[string]interface{})
|
|
|
|
v := txt[strings.LastIndex(txt, " "):]
|
|
|
|
k := ""
|
|
|
|
if idx := strings.Index(txt, "{"); idx > 0 {
|
|
|
|
labels := make(map[string]string)
|
|
|
|
lb := strings.Split(txt[idx+1:strings.Index(txt, "}")], ",")
|
|
|
|
for _, l := range lb {
|
|
|
|
p := strings.Split(l, "=")
|
|
|
|
labels[strings.Trim(p[0], `"`)] = strings.Trim(p[1], `"`)
|
|
|
|
}
|
|
|
|
mt["labels"] = labels
|
|
|
|
k = txt[:idx]
|
|
|
|
} else {
|
|
|
|
k = txt[:strings.Index(txt, " ")]
|
|
|
|
}
|
|
|
|
mt["name"] = k
|
|
|
|
mt["value"] = v
|
|
|
|
metrics = append(metrics, mt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(metrics) == 0 {
|
|
|
|
return nil, fmt.Errorf("%s %s not found", tp, name)
|
|
|
|
}
|
|
|
|
return metrics, nil
|
|
|
|
}
|