2020-09-29 11:15:29 +03:00
|
|
|
package opentracing_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-18 10:11:41 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-09-29 11:15:29 +03:00
|
|
|
"testing"
|
2023-01-18 10:11:41 +03:00
|
|
|
"time"
|
2020-09-29 11:15:29 +03:00
|
|
|
|
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/opentracing/opentracing-go/mocktracer"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-01-18 10:11:41 +03:00
|
|
|
"github.com/uber/jaeger-client-go"
|
|
|
|
"github.com/uber/jaeger-client-go/config"
|
2021-10-27 01:24:35 +03:00
|
|
|
cli "go.unistack.org/micro-client-grpc/v3"
|
|
|
|
jsoncodec "go.unistack.org/micro-codec-json/v3"
|
|
|
|
rrouter "go.unistack.org/micro-router-register/v3"
|
|
|
|
srv "go.unistack.org/micro-server-grpc/v3"
|
2023-01-18 10:11:41 +03:00
|
|
|
ot "go.unistack.org/micro-tracer-opentracing/v3"
|
2021-10-27 01:24:35 +03:00
|
|
|
"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"
|
2023-01-18 10:11:41 +03:00
|
|
|
mt "go.unistack.org/micro/v3/tracer"
|
2020-09-29 11:15:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Test interface {
|
|
|
|
Method(ctx context.Context, in *TestRequest, opts ...client.CallOption) (*TestResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestRequest struct {
|
|
|
|
IsError bool
|
|
|
|
}
|
2021-10-27 01:24:35 +03:00
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
type TestResponse struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
type testHandler struct{}
|
|
|
|
|
|
|
|
func (t *testHandler) Method(ctx context.Context, req *TestRequest, rsp *TestResponse) error {
|
2023-01-18 10:11:41 +03:00
|
|
|
var span mt.Span
|
|
|
|
ctx, span = mt.DefaultTracer.Start(ctx, "internal", mt.WithSpanKind(mt.SpanKindInternal))
|
|
|
|
defer span.Finish()
|
|
|
|
span.AddLabels("some key", "some val")
|
2020-09-29 11:15:29 +03:00
|
|
|
if req.IsError {
|
|
|
|
return errors.BadRequest("bad", "test error")
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp.Message = "passed"
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
func initJaeger(service string) (opentracing.Tracer, io.Closer) {
|
|
|
|
cfg := &config.Configuration{
|
|
|
|
ServiceName: service,
|
|
|
|
Sampler: &config.SamplerConfig{
|
|
|
|
Type: "const",
|
|
|
|
Param: 1,
|
|
|
|
},
|
|
|
|
Reporter: &config.ReporterConfig{
|
|
|
|
LogSpans: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tracer, closer, err := cfg.NewTracer(config.Logger(jaeger.StdLogger))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
|
|
|
|
}
|
|
|
|
return tracer, closer
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
func TestClient(t *testing.T) {
|
|
|
|
// setup
|
|
|
|
assert := assert.New(t)
|
|
|
|
for name, tt := range map[string]struct {
|
|
|
|
message string
|
|
|
|
isError bool
|
|
|
|
wantMessage string
|
|
|
|
wantStatus string
|
|
|
|
}{
|
|
|
|
"OK": {
|
|
|
|
message: "passed",
|
|
|
|
isError: false,
|
|
|
|
wantMessage: "passed",
|
|
|
|
wantStatus: "OK",
|
|
|
|
},
|
|
|
|
"Invalid": {
|
|
|
|
message: "",
|
|
|
|
isError: true,
|
|
|
|
wantMessage: "",
|
|
|
|
wantStatus: "InvalidArgument",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2023-01-18 10:11:41 +03:00
|
|
|
var tr opentracing.Tracer
|
|
|
|
tr = mocktracer.New()
|
|
|
|
|
|
|
|
_ = tr
|
|
|
|
var cl io.Closer
|
|
|
|
tr, cl = initJaeger(fmt.Sprintf("Test tracing %s", time.Now().Format(time.RFC1123Z)))
|
|
|
|
defer cl.Close()
|
|
|
|
opentracing.SetGlobalTracer(tr)
|
2020-09-29 11:15:29 +03:00
|
|
|
|
2021-02-12 20:50:02 +03:00
|
|
|
reg := register.NewRegister()
|
|
|
|
brk := broker.NewBroker(broker.Register(reg))
|
2020-09-29 11:15:29 +03:00
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
serverName := "service"
|
|
|
|
serverID := "1234567890"
|
2020-09-29 11:15:29 +03:00
|
|
|
serverVersion := "1.0.0"
|
|
|
|
|
2021-01-29 16:47:26 +03:00
|
|
|
rt := rrouter.NewRouter(router.Register(reg))
|
2020-09-29 11:15:29 +03:00
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
mtr := ot.NewTracer(ot.Tracer(tr))
|
|
|
|
if err := mtr.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
mt.DefaultTracer = mtr
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
c := cli.NewClient(
|
2020-11-26 08:57:54 +03:00
|
|
|
client.Codec("application/grpc+json", jsoncodec.NewCodec()),
|
|
|
|
client.Codec("application/json", jsoncodec.NewCodec()),
|
2020-09-29 11:15:29 +03:00
|
|
|
client.Router(rt),
|
|
|
|
)
|
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
if err := c.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
s := srv.NewServer(
|
2020-11-26 08:57:54 +03:00
|
|
|
server.Codec("application/grpc+json", jsoncodec.NewCodec()),
|
|
|
|
server.Codec("application/json", jsoncodec.NewCodec()),
|
2020-09-29 11:15:29 +03:00
|
|
|
server.Name(serverName),
|
|
|
|
server.Version(serverVersion),
|
2021-04-29 22:51:46 +03:00
|
|
|
server.ID(serverID),
|
2021-01-29 16:47:26 +03:00
|
|
|
server.Register(reg),
|
2020-09-29 11:15:29 +03:00
|
|
|
server.Broker(brk),
|
2024-12-11 02:00:29 +03:00
|
|
|
server.Tracer(mtr),
|
2021-07-17 02:30:58 +06:00
|
|
|
server.Address("127.0.0.1:0"),
|
2020-09-29 11:15:29 +03:00
|
|
|
)
|
|
|
|
if err := s.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-01 12:51:38 +03:00
|
|
|
defer func() {
|
|
|
|
_ = s.Stop()
|
|
|
|
}()
|
2020-09-29 11:15:29 +03:00
|
|
|
|
|
|
|
type Test struct {
|
|
|
|
*testHandler
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
nopts := []server.HandlerOption{
|
2023-02-22 00:12:01 +03:00
|
|
|
server.EndpointMetadata("Test", map[string]string{
|
|
|
|
"Name": "Test.Method",
|
|
|
|
"Method": "POST",
|
|
|
|
"Handler": "rpc",
|
|
|
|
},
|
|
|
|
),
|
2023-01-18 10:11:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Handle(s.NewHandler(&Test{new(testHandler)}, nopts...)); err != nil {
|
2021-02-12 20:50:02 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-09-29 11:15:29 +03:00
|
|
|
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
t.Fatalf("Unexpected error starting server: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
ctx, span := mtr.Start(context.Background(), "root", mt.WithSpanKind(mt.SpanKindClient))
|
|
|
|
var err error
|
|
|
|
req := c.NewRequest("service", "Test.Method", &TestRequest{IsError: tt.isError}, client.RequestContentType("application/json"))
|
|
|
|
fmt.Printf("%s.%s\n", req.Service(), req.Endpoint())
|
2020-09-29 11:15:29 +03:00
|
|
|
rsp := TestResponse{}
|
|
|
|
err = c.Call(ctx, req, &rsp)
|
|
|
|
if tt.isError {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
assert.Equal(rsp.Message, tt.message)
|
|
|
|
|
|
|
|
span.Finish()
|
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
return
|
|
|
|
/*
|
|
|
|
spans := tr.FinishedSpans()
|
|
|
|
assert.Len(spans, 3)
|
|
|
|
|
|
|
|
var rootSpan opentracing.Span
|
|
|
|
for _, s := range spans {
|
|
|
|
// order of traces in buffer is not garanteed
|
|
|
|
switch s.OperationName {
|
|
|
|
case "root":
|
|
|
|
rootSpan = s
|
|
|
|
}
|
2020-09-29 11:15:29 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
for _, s := range spans {
|
|
|
|
fmt.Printf("root %#+v\ncheck span %#+v\n", rootSpan, s)
|
|
|
|
assert.Equal(rootSpan.Context().(mocktracer.MockSpanContext).TraceID, s.Context().(mocktracer.MockSpanContext).TraceID)
|
|
|
|
}
|
|
|
|
*/
|
2020-09-29 11:15:29 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|