Repository: go-kratos/kratos
Pull request #1490 (2021-09-16 15:43:17)

fix typo


Patch:
diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml
index 0e5e2684..ae4f7da6 100644
--- a/.github/workflows/go.yml
+++ b/.github/workflows/go.yml
@@ -32,7 +32,7 @@ jobs:
       - name: Set up Go
         uses: actions/setup-go@v2
         with:
-          go-version: 1.16
+          go-version: 1.17
 
       - name: Build
         run: go build ./...
diff --git a/app.go b/app.go
index 10a43dbc..2a11f845 100644
--- a/app.go
+++ b/app.go
@@ -116,7 +116,7 @@ func (a *App) Run() error {
 			case <-c:
 				err := a.Stop()
 				if err != nil {
-					a.opts.logger.Errorf("failed to app stop: %v", err)
+					a.opts.logger.Errorf("failed to stop app: %v", err)
 				}
 			}
 		}
diff --git a/transport/grpc/server.go b/transport/grpc/server.go
index 748302c2..31efebbf 100644
--- a/transport/grpc/server.go
+++ b/transport/grpc/server.go
@@ -89,6 +89,13 @@ func Options(opts ...grpc.ServerOption) ServerOption {
 	}
 }
 
+// Endpoint with server endpoint.
+func Endpoint(endpoint *url.URL) ServerOption {
+	return func(o *Server) {
+		o.endpoint = endpoint
+	}
+}
+
 // Server is a gRPC server wrapper.
 type Server struct {
 	*grpc.Server
@@ -151,6 +158,9 @@ func NewServer(opts ...ServerOption) *Server {
 //   grpc://127.0.0.1:9000?isSecure=false
 func (s *Server) Endpoint() (*url.URL, error) {
 	s.once.Do(func() {
+		if s.endpoint != nil {
+			return
+		}
 		lis, err := net.Listen(s.network, s.address)
 		if err != nil {
 			s.err = err


Test patch:
diff --git a/config/env/env_test.go b/config/env/env_test.go
index 0ddb7c93..6ccf76d7 100644
--- a/config/env/env_test.go
+++ b/config/env/env_test.go
@@ -55,9 +55,7 @@ func TestEnvWithPrefix(t *testing.T) {
 	}
 
 	for k, v := range envs {
-		if err := os.Setenv(k, v); err != nil {
-			t.Fatal(err)
-		}
+		t.Setenv(k, v)
 	}
 
 	c := config.New(config.WithSource(
@@ -163,9 +161,7 @@ func TestEnvWithoutPrefix(t *testing.T) {
 	}
 
 	for k, v := range envs {
-		if err := os.Setenv(k, v); err != nil {
-			t.Fatal(err)
-		}
+		t.Setenv(k, v)
 	}
 
 	c := config.New(config.WithSource(
diff --git a/middleware/tracing/span_test.go b/middleware/tracing/span_test.go
new file mode 100644
index 00000000..c930154f
--- /dev/null
+++ b/middleware/tracing/span_test.go
@@ -0,0 +1,158 @@
+package tracing
+
+import (
+	"reflect"
+	"testing"
+
+	"go.opentelemetry.io/otel/attribute"
+	semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
+)
+
+func Test_parseFullMethod(t *testing.T) {
+	tests := []struct {
+		name       string
+		fullMethod string
+		want       string
+		wantAttr   []attribute.KeyValue
+	}{
+		{
+			name:       "/foo.bar/hello",
+			fullMethod: "/foo.bar/hello",
+			want:       "foo.bar/hello",
+			wantAttr: []attribute.KeyValue{
+				semconv.RPCServiceKey.String("foo.bar"),
+				semconv.RPCMethodKey.String("hello"),
+			},
+		},
+		{
+			name:       "/foo.bar/hello/world",
+			fullMethod: "/foo.bar/hello/world",
+			want:       "foo.bar/hello/world",
+			wantAttr: []attribute.KeyValue{
+				semconv.RPCServiceKey.String("foo.bar"),
+				semconv.RPCMethodKey.String("hello/world"),
+			},
+		},
+		{
+			name:       "/hello",
+			fullMethod: "/hello",
+			want:       "hello",
+			wantAttr:   []attribute.KeyValue{attribute.Key("rpc.operation").String("/hello")},
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			got, got1 := parseFullMethod(tt.fullMethod)
+			if got != tt.want {
+				t.Errorf("parseFullMethod() got = %v, want %v", got, tt.want)
+			}
+			if !reflect.DeepEqual(got1, tt.wantAttr) {
+				t.Errorf("parseFullMethod() got1 = %v, want %v", got1, tt.wantAttr)
+			}
+		})
+	}
+}
+
+func Test_peerAttr(t *testing.T) {
+	tests := []struct {
+		name string
+		addr string
+		want []attribute.KeyValue
+	}{
+		{
+			name: "nil addr",
+			addr: ":8080",
+			want: []attribute.KeyValue{
+				semconv.NetPeerIPKey.String("127.0.0.1"),
+				semconv.NetPeerPortKey.String("8080"),
+			},
+		},
+		{
+			name: "normal addr without port",
+			addr: "192.168.0.1",
+			want: []attribute.KeyValue(nil),
+		},
+		{
+			name: "normal addr with port",
+			addr: "192.168.0.1:8080",
+			want: []attribute.KeyValue{
+				semconv.NetPeerIPKey.String("192.168.0.1"),
+				semconv.NetPeerPortKey.String("8080"),
+			},
+		},
+		{
+			name: "dns addr",
+			addr: "foo:8080",
+			want: []attribute.KeyValue{
+				semconv.NetPeerIPKey.String("foo"),
+				semconv.NetPeerPortKey.String("8080"),
+			},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := peerAttr(tt.addr); !reflect.DeepEqual(got, tt.want) {
+				t.Errorf("peerAttr() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
+
+func Test_parseTarget(t *testing.T) {
+	tests := []struct {
+		name        string
+		endpoint    string
+		wantAddress string
+		wantErr     bool
+	}{
+		{
+			name:        "http",
+			endpoint:    "http://foo.bar:8080",
+			wantAddress: "http://foo.bar:8080",
+			wantErr:     false,
+		},
+		{
+			name:        "http",
+			endpoint:    "http://127.0.0.1:8080",
+			wantAddress: "http://127.0.0.1:8080",
+			wantErr:     false,
+		},
+		{
+			name:        "without protocol",
+			endpoint:    "foo.bar:8080",
+			wantAddress: "foo.bar:8080",
+			wantErr:     false,
+		},
+		{
+			name:        "grpc",
+			endpoint:    "grpc://foo.bar",
+			wantAddress: "grpc://foo.bar",
+			wantErr:     false,
+		},
+		{
+			name:        "with path",
+			endpoint:    "/foo",
+			wantAddress: "foo",
+			wantErr:     false,
+		},
+		{
+			name:        "with path",
+			endpoint:    "http://127.0.0.1/hello",
+			wantAddress: "hello",
+			wantErr:     false,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			gotAddress, err := parseTarget(tt.endpoint)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("parseTarget() error = %v, wantErr %v", err, tt.wantErr)
+				return
+			}
+			if gotAddress != tt.wantAddress {
+				t.Errorf("parseTarget() = %v, want %v", gotAddress, tt.wantAddress)
+			}
+		})
+	}
+}
diff --git a/middleware/tracing/tracing_test.go b/middleware/tracing/tracing_test.go
index 40ada4da..7593d1d3 100644
--- a/middleware/tracing/tracing_test.go
+++ b/middleware/tracing/tracing_test.go
@@ -3,15 +3,18 @@ package tracing
 import (
 	"context"
 	"net/http"
+	"os"
 	"testing"
 
+	"github.com/go-kratos/kratos/v2/log"
 	"github.com/go-kratos/kratos/v2/transport"
+	"github.com/stretchr/testify/assert"
 	"go.opentelemetry.io/otel/propagation"
 	tracesdk "go.opentelemetry.io/otel/sdk/trace"
 	"go.opentelemetry.io/otel/trace"
 )
 
-var _ transport.Transporter = &Transport{}
+var _ transport.Transporter = &mockTransport{}
 
 type headerCarrier http.Header
 
@@ -34,25 +37,25 @@ func (hc headerCarrier) Keys() []string {
 	return keys
 }
 
-type Transport struct {
+type mockTransport struct {
 	kind      transport.Kind
 	endpoint  string
 	operation string
 	header    headerCarrier
 }
 
-func (tr *Transport) Kind() transport.Kind            { return tr.kind }
-func (tr *Transport) Endpoint() string                { return tr.endpoint }
-func (tr *Transport) Operation() string               { return tr.operation }
-func (tr *Transport) RequestHeader() transport.Header { return tr.header }
-func (tr *Transport) ReplyHeader() transport.Header   { return tr.header }
+func (tr *mockTransport) Kind() transport.Kind            { return tr.kind }
+func (tr *mockTransport) Endpoint() string                { return tr.endpoint }
+func (tr *mockTransport) Operation() string               { return tr.operation }
+func (tr *mockTransport) RequestHeader() transport.Header { return tr.header }
+func (tr *mockTransport) ReplyHeader() transport.Header   { return tr.header }
 
-func TestTracing(t *testing.T) {
+func TestTracer(t *testing.T) {
 	carrier := headerCarrier{}
 	tp := tracesdk.NewTracerProvider(tracesdk.WithSampler(tracesdk.TraceIDRatioBased(0)))
 
 	// caller use Inject
-	tracer := NewTracer(
+	cliTracer := NewTracer(
 		trace.SpanKindClient,
 		WithTracerProvider(tp),
 		WithPropagator(
@@ -60,17 +63,17 @@ func TestTracing(t *testing.T) {
 		),
 	)
 
-	ts := &Transport{kind: transport.KindHTTP, header: carrier}
+	ts := &mockTransport{kind: transport.KindHTTP, header: carrier}
 
-	ctx, aboveSpan := tracer.Start(transport.NewClientContext(context.Background(), ts), ts.Operation(), ts.RequestHeader())
-	defer tracer.End(ctx, aboveSpan, nil, nil)
+	ctx, aboveSpan := cliTracer.Start(transport.NewClientContext(context.Background(), ts), ts.Operation(), ts.RequestHeader())
+	defer cliTracer.End(ctx, aboveSpan, nil, nil)
 
 	// server use Extract fetch traceInfo from carrier
-	tracer = NewTracer(trace.SpanKindServer, WithPropagator(propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{})))
-	ts = &Transport{kind: transport.KindHTTP, header: carrier}
+	svrTracer := NewTracer(trace.SpanKindServer, WithPropagator(propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{})))
+	ts = &mockTransport{kind: transport.KindHTTP, header: carrier}
 
-	ctx, span := tracer.Start(transport.NewServerContext(ctx, ts), ts.Operation(), ts.RequestHeader())
-	defer tracer.End(ctx, span, nil, nil)
+	ctx, span := svrTracer.Start(transport.NewServerContext(ctx, ts), ts.Operation(), ts.RequestHeader())
+	defer svrTracer.End(ctx, span, nil, nil)
 
 	if aboveSpan.SpanContext().TraceID() != span.SpanContext().TraceID() {
 		t.Fatalf("TraceID failed to deliver")
@@ -80,3 +83,110 @@ func TestTracing(t *testing.T) {
 		t.Fatalf("traceHeader failed to deliver")
 	}
 }
+
+func TestServer(t *testing.T) {
+	tr := &mockTransport{
+		kind:      transport.KindHTTP,
+		endpoint:  "server:2233",
+		operation: "/test.server/hello",
+		header:    headerCarrier{},
+	}
+
+	tracer := NewTracer(
+		trace.SpanKindClient,
+		WithTracerProvider(tracesdk.NewTracerProvider()),
+	)
+
+	logger := log.NewStdLogger(os.Stdout)
+	logger = log.With(logger, "span_id", SpanID())
+	logger = log.With(logger, "trace_id", TraceID())
+
+	var (
+		childSpanID  string
+		childTraceID string
+	)
+	next := func(ctx context.Context, req interface{}) (interface{}, error) {
+		_ = log.WithContext(ctx, logger).Log(log.LevelInfo,
+			"kind", "server",
+		)
+		childSpanID = SpanID()(ctx).(string)
+		childTraceID = TraceID()(ctx).(string)
+		return req.(string) + "https://go-kratos.dev", nil
+	}
+
+	var ctx context.Context
+	ctx, span := tracer.Start(
+		transport.NewServerContext(context.Background(), tr),
+		tr.Operation(),
+		tr.RequestHeader(),
+	)
+
+	_, err := Server(
+		WithTracerProvider(tracesdk.NewTracerProvider()),
+		WithPropagator(propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{})),
+	)(next)(ctx, "test server: ")
+
+	span.End()
+	assert.NoError(t, err)
+	assert.NotEmpty(t, childSpanID)
+	assert.NotEqual(t, span.SpanContext().SpanID().String(), childSpanID)
+	assert.Equal(t, span.SpanContext().TraceID().String(), childTraceID)
+
+	_, err = Server(
+		WithTracerProvider(tracesdk.NewTracerProvider()),
+		WithPropagator(propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{})),
+	)(next)(context.Background(), "test server: ")
+
+	assert.NoError(t, err)
+	assert.Empty(t, childSpanID)
+	assert.Empty(t, childTraceID)
+}
+
+func TestClient(t *testing.T) {
+	tr := &mockTransport{
+		kind:      transport.KindHTTP,
+		endpoint:  "server:2233",
+		operation: "/test.server/hello",
+		header:    headerCarrier{},
+	}
+
+	tracer := NewTracer(
+		trace.SpanKindClient,
+		WithTracerProvider(tracesdk.NewTracerProvider()),
+	)
+
+	logger := log.NewStdLogger(os.Stdout)
+	logger = log.With(logger, "span_id", SpanID())
+	logger = log.With(logger, "trace_id", TraceID())
+
+	var (
+		childSpanID  string
+		childTraceID string
+	)
+	next := func(ctx context.Context, req interface{}) (interface{}, error) {
+		_ = log.WithContext(ctx, logger).Log(log.LevelInfo,
+			"kind", "client",
+		)
+		childSpanID = SpanID()(ctx).(string)
+		childTraceID = TraceID()(ctx).(string)
+		return req.(string) + "https://go-kratos.dev", nil
+	}
+
+	var ctx context.Context
+	ctx, span := tracer.Start(
+		transport.NewClientContext(context.Background(), tr),
+		tr.Operation(),
+		tr.RequestHeader(),
+	)
+
+	_, err := Client(
+		WithTracerProvider(tracesdk.NewTracerProvider()),
+		WithPropagator(propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{})),
+	)(next)(ctx, "test client: ")
+
+	span.End()
+	assert.NoError(t, err)
+	assert.NotEmpty(t, childSpanID)
+	assert.NotEqual(t, span.SpanContext().SpanID().String(), childSpanID)
+	assert.Equal(t, span.SpanContext().TraceID().String(), childTraceID)
+}
diff --git a/transport/grpc/server_test.go b/transport/grpc/server_test.go
index 3b9f1ede..10a3df25 100644
--- a/transport/grpc/server_test.go
+++ b/transport/grpc/server_test.go
@@ -82,6 +82,15 @@ func TestMiddleware(t *testing.T) {
 	assert.Equal(t, v, o.middleware)
 }
 
+func TestEndpoint(t *testing.T) {
+	u, err := url.Parse("grpc://hello/world")
+	assert.NoError(t, err)
+	o := &Server{}
+	Endpoint(u)(o)
+	assert.Equal(t, "hello", o.endpoint.Host)
+	assert.Equal(t, "grpc", o.endpoint.Scheme)
+}
+
 type mockLogger struct {
 	level log.Level
 	key   string