utils_test.go 3.5 KB

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