github.com/lingyao2333/mo-zero@v1.4.1/core/trace/utils_test.go (about) 1 package trace 2 3 import ( 4 "context" 5 "net" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "go.opentelemetry.io/otel/attribute" 10 semconv "go.opentelemetry.io/otel/semconv/v1.4.0" 11 "google.golang.org/grpc/peer" 12 ) 13 14 func TestPeerFromContext(t *testing.T) { 15 addrs, err := net.InterfaceAddrs() 16 assert.Nil(t, err) 17 assert.NotEmpty(t, addrs) 18 tests := []struct { 19 name string 20 ctx context.Context 21 empty bool 22 }{ 23 { 24 name: "empty", 25 ctx: context.Background(), 26 empty: true, 27 }, 28 { 29 name: "nil", 30 ctx: peer.NewContext(context.Background(), nil), 31 empty: true, 32 }, 33 { 34 name: "with value", 35 ctx: peer.NewContext(context.Background(), &peer.Peer{ 36 Addr: addrs[0], 37 }), 38 }, 39 } 40 41 for _, test := range tests { 42 test := test 43 t.Run(test.name, func(t *testing.T) { 44 t.Parallel() 45 addr := PeerFromCtx(test.ctx) 46 assert.Equal(t, test.empty, len(addr) == 0) 47 }) 48 } 49 } 50 51 func TestParseFullMethod(t *testing.T) { 52 tests := []struct { 53 fullMethod string 54 name string 55 attr []attribute.KeyValue 56 }{ 57 { 58 fullMethod: "/grpc.test.EchoService/Echo", 59 name: "grpc.test.EchoService/Echo", 60 attr: []attribute.KeyValue{ 61 semconv.RPCServiceKey.String("grpc.test.EchoService"), 62 semconv.RPCMethodKey.String("Echo"), 63 }, 64 }, { 65 fullMethod: "/com.example.ExampleRmiService/exampleMethod", 66 name: "com.example.ExampleRmiService/exampleMethod", 67 attr: []attribute.KeyValue{ 68 semconv.RPCServiceKey.String("com.example.ExampleRmiService"), 69 semconv.RPCMethodKey.String("exampleMethod"), 70 }, 71 }, { 72 fullMethod: "/MyCalcService.Calculator/Add", 73 name: "MyCalcService.Calculator/Add", 74 attr: []attribute.KeyValue{ 75 semconv.RPCServiceKey.String("MyCalcService.Calculator"), 76 semconv.RPCMethodKey.String("Add"), 77 }, 78 }, { 79 fullMethod: "/MyServiceReference.ICalculator/Add", 80 name: "MyServiceReference.ICalculator/Add", 81 attr: []attribute.KeyValue{ 82 semconv.RPCServiceKey.String("MyServiceReference.ICalculator"), 83 semconv.RPCMethodKey.String("Add"), 84 }, 85 }, { 86 fullMethod: "/MyServiceWithNoPackage/theMethod", 87 name: "MyServiceWithNoPackage/theMethod", 88 attr: []attribute.KeyValue{ 89 semconv.RPCServiceKey.String("MyServiceWithNoPackage"), 90 semconv.RPCMethodKey.String("theMethod"), 91 }, 92 }, { 93 fullMethod: "/pkg.svr", 94 name: "pkg.svr", 95 attr: []attribute.KeyValue(nil), 96 }, { 97 fullMethod: "/pkg.svr/", 98 name: "pkg.svr/", 99 attr: []attribute.KeyValue{ 100 semconv.RPCServiceKey.String("pkg.svr"), 101 }, 102 }, 103 } 104 105 for _, test := range tests { 106 n, a := ParseFullMethod(test.fullMethod) 107 assert.Equal(t, test.name, n) 108 assert.Equal(t, test.attr, a) 109 } 110 } 111 112 func TestSpanInfo(t *testing.T) { 113 val, kvs := SpanInfo("/fullMethod", "remote") 114 assert.Equal(t, "fullMethod", val) 115 assert.NotEmpty(t, kvs) 116 } 117 118 func TestPeerAttr(t *testing.T) { 119 tests := []struct { 120 name string 121 addr string 122 expect []attribute.KeyValue 123 }{ 124 { 125 name: "empty", 126 }, 127 { 128 name: "port only", 129 addr: ":8080", 130 expect: []attribute.KeyValue{ 131 semconv.NetPeerIPKey.String(localhost), 132 semconv.NetPeerPortKey.String("8080"), 133 }, 134 }, 135 { 136 name: "port only", 137 addr: "192.168.0.2:8080", 138 expect: []attribute.KeyValue{ 139 semconv.NetPeerIPKey.String("192.168.0.2"), 140 semconv.NetPeerPortKey.String("8080"), 141 }, 142 }, 143 } 144 145 for _, test := range tests { 146 test := test 147 t.Run(test.name, func(t *testing.T) { 148 t.Parallel() 149 kvs := PeerAttr(test.addr) 150 assert.EqualValues(t, test.expect, kvs) 151 }) 152 } 153 }