utils_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package trace
  2. import (
  3. "context"
  4. "net"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "go.opentelemetry.io/otel"
  8. "go.opentelemetry.io/otel/attribute"
  9. "go.opentelemetry.io/otel/sdk/resource"
  10. sdktrace "go.opentelemetry.io/otel/sdk/trace"
  11. semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
  12. "go.opentelemetry.io/otel/trace"
  13. "google.golang.org/grpc/peer"
  14. )
  15. func TestPeerFromContext(t *testing.T) {
  16. addrs, err := net.InterfaceAddrs()
  17. assert.Nil(t, err)
  18. assert.NotEmpty(t, addrs)
  19. tests := []struct {
  20. name string
  21. ctx context.Context
  22. empty bool
  23. }{
  24. {
  25. name: "empty",
  26. ctx: context.Background(),
  27. empty: true,
  28. },
  29. {
  30. name: "nil",
  31. ctx: peer.NewContext(context.Background(), nil),
  32. empty: true,
  33. },
  34. {
  35. name: "with value",
  36. ctx: peer.NewContext(context.Background(), &peer.Peer{
  37. Addr: addrs[0],
  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. func TestParseFullMethod(t *testing.T) {
  51. tests := []struct {
  52. fullMethod string
  53. name string
  54. attr []attribute.KeyValue
  55. }{
  56. {
  57. fullMethod: "/grpc.test.EchoService/Echo",
  58. name: "grpc.test.EchoService/Echo",
  59. attr: []attribute.KeyValue{
  60. semconv.RPCServiceKey.String("grpc.test.EchoService"),
  61. semconv.RPCMethodKey.String("Echo"),
  62. },
  63. }, {
  64. fullMethod: "/com.example.ExampleRmiService/exampleMethod",
  65. name: "com.example.ExampleRmiService/exampleMethod",
  66. attr: []attribute.KeyValue{
  67. semconv.RPCServiceKey.String("com.example.ExampleRmiService"),
  68. semconv.RPCMethodKey.String("exampleMethod"),
  69. },
  70. }, {
  71. fullMethod: "/MyCalcService.Calculator/Add",
  72. name: "MyCalcService.Calculator/Add",
  73. attr: []attribute.KeyValue{
  74. semconv.RPCServiceKey.String("MyCalcService.Calculator"),
  75. semconv.RPCMethodKey.String("Add"),
  76. },
  77. }, {
  78. fullMethod: "/MyServiceReference.ICalculator/Add",
  79. name: "MyServiceReference.ICalculator/Add",
  80. attr: []attribute.KeyValue{
  81. semconv.RPCServiceKey.String("MyServiceReference.ICalculator"),
  82. semconv.RPCMethodKey.String("Add"),
  83. },
  84. }, {
  85. fullMethod: "/MyServiceWithNoPackage/theMethod",
  86. name: "MyServiceWithNoPackage/theMethod",
  87. attr: []attribute.KeyValue{
  88. semconv.RPCServiceKey.String("MyServiceWithNoPackage"),
  89. semconv.RPCMethodKey.String("theMethod"),
  90. },
  91. }, {
  92. fullMethod: "/pkg.svr",
  93. name: "pkg.svr",
  94. attr: []attribute.KeyValue(nil),
  95. }, {
  96. fullMethod: "/pkg.svr/",
  97. name: "pkg.svr/",
  98. attr: []attribute.KeyValue{
  99. semconv.RPCServiceKey.String("pkg.svr"),
  100. },
  101. },
  102. }
  103. for _, test := range tests {
  104. n, a := ParseFullMethod(test.fullMethod)
  105. assert.Equal(t, test.name, n)
  106. assert.Equal(t, test.attr, a)
  107. }
  108. }
  109. func TestSpanInfo(t *testing.T) {
  110. val, kvs := SpanInfo("/fullMethod", "remote")
  111. assert.Equal(t, "fullMethod", val)
  112. assert.NotEmpty(t, kvs)
  113. }
  114. func TestPeerAttr(t *testing.T) {
  115. tests := []struct {
  116. name string
  117. addr string
  118. expect []attribute.KeyValue
  119. }{
  120. {
  121. name: "empty",
  122. },
  123. {
  124. name: "port only",
  125. addr: ":8080",
  126. expect: []attribute.KeyValue{
  127. semconv.NetPeerIPKey.String(localhost),
  128. semconv.NetPeerPortKey.String("8080"),
  129. },
  130. },
  131. {
  132. name: "port only",
  133. addr: "192.168.0.2:8080",
  134. expect: []attribute.KeyValue{
  135. semconv.NetPeerIPKey.String("192.168.0.2"),
  136. semconv.NetPeerPortKey.String("8080"),
  137. },
  138. },
  139. }
  140. for _, test := range tests {
  141. test := test
  142. t.Run(test.name, func(t *testing.T) {
  143. t.Parallel()
  144. kvs := PeerAttr(test.addr)
  145. assert.EqualValues(t, test.expect, kvs)
  146. })
  147. }
  148. }
  149. func TestTracerFromContext(t *testing.T) {
  150. traceFn := func(ctx context.Context, hasTraceId bool) {
  151. spanContext := trace.SpanContextFromContext(ctx)
  152. assert.Equal(t, spanContext.IsValid(), hasTraceId)
  153. parentTraceId := spanContext.TraceID().String()
  154. tracer := TracerFromContext(ctx)
  155. _, span := tracer.Start(ctx, "b")
  156. defer span.End()
  157. spanContext = span.SpanContext()
  158. assert.True(t, spanContext.IsValid())
  159. if hasTraceId {
  160. assert.Equal(t, parentTraceId, spanContext.TraceID().String())
  161. }
  162. }
  163. t.Run("context", func(t *testing.T) {
  164. opts := []sdktrace.TracerProviderOption{
  165. // Set the sampling rate based on the parent span to 100%
  166. sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(1))),
  167. // Record information about this application in a Resource.
  168. sdktrace.WithResource(resource.NewSchemaless(semconv.ServiceNameKey.String("test"))),
  169. }
  170. tp = sdktrace.NewTracerProvider(opts...)
  171. otel.SetTracerProvider(tp)
  172. ctx, span := tp.Tracer(TraceName).Start(context.Background(), "a")
  173. defer span.End()
  174. traceFn(ctx, true)
  175. })
  176. t.Run("global", func(t *testing.T) {
  177. opts := []sdktrace.TracerProviderOption{
  178. // Set the sampling rate based on the parent span to 100%
  179. sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(1))),
  180. // Record information about this application in a Resource.
  181. sdktrace.WithResource(resource.NewSchemaless(semconv.ServiceNameKey.String("test"))),
  182. }
  183. tp = sdktrace.NewTracerProvider(opts...)
  184. otel.SetTracerProvider(tp)
  185. traceFn(context.Background(), false)
  186. })
  187. }