tracinginterceptor_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package serverinterceptors
  2. import (
  3. "context"
  4. "errors"
  5. "io"
  6. "sync"
  7. "sync/atomic"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. ztrace "github.com/zeromicro/go-zero/core/trace"
  11. "github.com/zeromicro/go-zero/core/trace/tracetest"
  12. "go.opentelemetry.io/otel/attribute"
  13. tcodes "go.opentelemetry.io/otel/codes"
  14. "go.opentelemetry.io/otel/sdk/trace"
  15. semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
  16. "google.golang.org/grpc"
  17. "google.golang.org/grpc/codes"
  18. "google.golang.org/grpc/metadata"
  19. "google.golang.org/grpc/status"
  20. )
  21. func TestUnaryOpenTracingInterceptor_Disable(t *testing.T) {
  22. _, err := UnaryTracingInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{
  23. FullMethod: "/",
  24. }, func(ctx context.Context, req any) (any, error) {
  25. return nil, nil
  26. })
  27. assert.Nil(t, err)
  28. }
  29. func TestUnaryOpenTracingInterceptor_Enabled(t *testing.T) {
  30. ztrace.StartAgent(ztrace.Config{
  31. Name: "go-zero-test",
  32. Endpoint: "http://localhost:14268/api/traces",
  33. Batcher: "jaeger",
  34. Sampler: 1.0,
  35. })
  36. defer ztrace.StopAgent()
  37. _, err := UnaryTracingInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{
  38. FullMethod: "/package.TestService.GetUser",
  39. }, func(ctx context.Context, req any) (any, error) {
  40. return nil, nil
  41. })
  42. assert.Nil(t, err)
  43. }
  44. func TestUnaryTracingInterceptor(t *testing.T) {
  45. t.Run("normal", func(t *testing.T) {
  46. var run int32
  47. me := tracetest.NewInMemoryExporter(t)
  48. _, err := UnaryTracingInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{
  49. FullMethod: "/proto.Hello/Echo",
  50. }, func(ctx context.Context, req any) (any, error) {
  51. atomic.AddInt32(&run, 1)
  52. return nil, nil
  53. })
  54. assert.Nil(t, err)
  55. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  56. assert.Equal(t, 1, len(me.GetSpans()))
  57. span := me.GetSpans()[0].Snapshot()
  58. assert.Equal(t, 2, len(span.Events()))
  59. assert.ElementsMatch(t, []attribute.KeyValue{
  60. ztrace.RPCSystemGRPC,
  61. semconv.RPCServiceKey.String("proto.Hello"),
  62. semconv.RPCMethodKey.String("Echo"),
  63. ztrace.StatusCodeAttr(codes.OK),
  64. }, span.Attributes())
  65. })
  66. t.Run("grpc error status", func(t *testing.T) {
  67. me := tracetest.NewInMemoryExporter(t)
  68. _, err := UnaryTracingInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{
  69. FullMethod: "/proto.Hello/Echo",
  70. }, func(ctx context.Context, req any) (any, error) {
  71. return nil, status.Errorf(codes.Unknown, "test")
  72. })
  73. assert.Error(t, err)
  74. assert.Equal(t, 1, len(me.GetSpans()))
  75. span := me.GetSpans()[0].Snapshot()
  76. assert.Equal(t, trace.Status{
  77. Code: tcodes.Error,
  78. Description: "test",
  79. }, span.Status())
  80. assert.Equal(t, 2, len(span.Events()))
  81. assert.ElementsMatch(t, []attribute.KeyValue{
  82. ztrace.RPCSystemGRPC,
  83. semconv.RPCServiceKey.String("proto.Hello"),
  84. semconv.RPCMethodKey.String("Echo"),
  85. ztrace.StatusCodeAttr(codes.Unknown),
  86. }, span.Attributes())
  87. })
  88. t.Run("non grpc status error", func(t *testing.T) {
  89. me := tracetest.NewInMemoryExporter(t)
  90. _, err := UnaryTracingInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{
  91. FullMethod: "/proto.Hello/Echo",
  92. }, func(ctx context.Context, req any) (any, error) {
  93. return nil, errors.New("test")
  94. })
  95. assert.Error(t, err)
  96. assert.Equal(t, 1, len(me.GetSpans()))
  97. span := me.GetSpans()[0].Snapshot()
  98. assert.Equal(t, trace.Status{
  99. Code: tcodes.Error,
  100. Description: "test",
  101. }, span.Status())
  102. assert.Equal(t, 1, len(span.Events()))
  103. assert.ElementsMatch(t, []attribute.KeyValue{
  104. ztrace.RPCSystemGRPC,
  105. semconv.RPCServiceKey.String("proto.Hello"),
  106. semconv.RPCMethodKey.String("Echo"),
  107. }, span.Attributes())
  108. })
  109. }
  110. func TestUnaryTracingInterceptor_WithError(t *testing.T) {
  111. tests := []struct {
  112. name string
  113. err error
  114. }{
  115. {
  116. name: "normal error",
  117. err: errors.New("dummy"),
  118. },
  119. {
  120. name: "grpc error",
  121. err: status.Error(codes.DataLoss, "dummy"),
  122. },
  123. }
  124. for _, test := range tests {
  125. test := test
  126. t.Run(test.name, func(t *testing.T) {
  127. t.Parallel()
  128. var wg sync.WaitGroup
  129. wg.Add(1)
  130. var md metadata.MD
  131. ctx := metadata.NewIncomingContext(context.Background(), md)
  132. _, err := UnaryTracingInterceptor(ctx, nil, &grpc.UnaryServerInfo{
  133. FullMethod: "/",
  134. }, func(ctx context.Context, req any) (any, error) {
  135. defer wg.Done()
  136. return nil, test.err
  137. })
  138. wg.Wait()
  139. assert.Equal(t, test.err, err)
  140. })
  141. }
  142. }
  143. func TestStreamTracingInterceptor_GrpcFormat(t *testing.T) {
  144. var run int32
  145. var wg sync.WaitGroup
  146. wg.Add(1)
  147. var md metadata.MD
  148. ctx := metadata.NewIncomingContext(context.Background(), md)
  149. stream := mockedServerStream{ctx: ctx}
  150. err := StreamTracingInterceptor(nil, &stream, &grpc.StreamServerInfo{
  151. FullMethod: "/foo",
  152. }, func(svr any, stream grpc.ServerStream) error {
  153. defer wg.Done()
  154. atomic.AddInt32(&run, 1)
  155. return nil
  156. })
  157. wg.Wait()
  158. assert.Nil(t, err)
  159. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  160. }
  161. func TestStreamTracingInterceptor_FinishWithGrpcError(t *testing.T) {
  162. tests := []struct {
  163. name string
  164. err error
  165. }{
  166. {
  167. name: "receive event",
  168. err: status.Error(codes.DataLoss, "dummy"),
  169. },
  170. {
  171. name: "error event",
  172. err: status.Error(codes.DataLoss, "dummy"),
  173. },
  174. }
  175. for _, test := range tests {
  176. test := test
  177. t.Run(test.name, func(t *testing.T) {
  178. t.Parallel()
  179. var wg sync.WaitGroup
  180. wg.Add(1)
  181. var md metadata.MD
  182. ctx := metadata.NewIncomingContext(context.Background(), md)
  183. stream := mockedServerStream{ctx: ctx}
  184. err := StreamTracingInterceptor(nil, &stream, &grpc.StreamServerInfo{
  185. FullMethod: "/foo",
  186. }, func(svr any, stream grpc.ServerStream) error {
  187. defer wg.Done()
  188. return test.err
  189. })
  190. wg.Wait()
  191. assert.Equal(t, test.err, err)
  192. })
  193. }
  194. }
  195. func TestStreamTracingInterceptor_WithError(t *testing.T) {
  196. tests := []struct {
  197. name string
  198. err error
  199. }{
  200. {
  201. name: "normal error",
  202. err: errors.New("dummy"),
  203. },
  204. {
  205. name: "grpc error",
  206. err: status.Error(codes.DataLoss, "dummy"),
  207. },
  208. }
  209. for _, test := range tests {
  210. test := test
  211. t.Run(test.name, func(t *testing.T) {
  212. t.Parallel()
  213. var wg sync.WaitGroup
  214. wg.Add(1)
  215. var md metadata.MD
  216. ctx := metadata.NewIncomingContext(context.Background(), md)
  217. stream := mockedServerStream{ctx: ctx}
  218. err := StreamTracingInterceptor(nil, &stream, &grpc.StreamServerInfo{
  219. FullMethod: "/foo",
  220. }, func(svr any, stream grpc.ServerStream) error {
  221. defer wg.Done()
  222. return test.err
  223. })
  224. wg.Wait()
  225. assert.Equal(t, test.err, err)
  226. })
  227. }
  228. }
  229. func TestClientStream_RecvMsg(t *testing.T) {
  230. tests := []struct {
  231. name string
  232. err error
  233. }{
  234. {
  235. name: "nil error",
  236. },
  237. {
  238. name: "EOF",
  239. err: io.EOF,
  240. },
  241. {
  242. name: "dummy error",
  243. err: errors.New("dummy"),
  244. },
  245. }
  246. for _, test := range tests {
  247. test := test
  248. t.Run(test.name, func(t *testing.T) {
  249. t.Parallel()
  250. stream := wrapServerStream(context.Background(), &mockedServerStream{
  251. ctx: context.Background(),
  252. err: test.err,
  253. })
  254. assert.Equal(t, test.err, stream.RecvMsg(nil))
  255. })
  256. }
  257. }
  258. func TestServerStream_SendMsg(t *testing.T) {
  259. tests := []struct {
  260. name string
  261. err error
  262. }{
  263. {
  264. name: "nil error",
  265. },
  266. {
  267. name: "with error",
  268. err: errors.New("dummy"),
  269. },
  270. }
  271. for _, test := range tests {
  272. test := test
  273. t.Run(test.name, func(t *testing.T) {
  274. t.Parallel()
  275. stream := wrapServerStream(context.Background(), &mockedServerStream{
  276. ctx: context.Background(),
  277. err: test.err,
  278. })
  279. assert.Equal(t, test.err, stream.SendMsg(nil))
  280. })
  281. }
  282. }
  283. type mockedServerStream struct {
  284. ctx context.Context
  285. err error
  286. }
  287. func (m *mockedServerStream) SetHeader(md metadata.MD) error {
  288. panic("implement me")
  289. }
  290. func (m *mockedServerStream) SendHeader(md metadata.MD) error {
  291. panic("implement me")
  292. }
  293. func (m *mockedServerStream) SetTrailer(md metadata.MD) {
  294. panic("implement me")
  295. }
  296. func (m *mockedServerStream) Context() context.Context {
  297. if m.ctx == nil {
  298. return context.Background()
  299. }
  300. return m.ctx
  301. }
  302. func (m *mockedServerStream) SendMsg(v any) error {
  303. return m.err
  304. }
  305. func (m *mockedServerStream) RecvMsg(v any) error {
  306. return m.err
  307. }