span_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package trace
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tal-tech/go-zero/core/stringx"
  7. "github.com/tal-tech/go-zero/core/trace/tracespec"
  8. "google.golang.org/grpc/metadata"
  9. )
  10. func TestClientSpan(t *testing.T) {
  11. span := newServerSpan(nil, "service", "operation")
  12. ctx := context.WithValue(context.Background(), tracespec.TracingKey, span)
  13. ctx, span = StartClientSpan(ctx, "entrance", "operation")
  14. defer span.Finish()
  15. assert.Equal(t, span, ctx.Value(tracespec.TracingKey))
  16. const serviceName = "authorization"
  17. const operationName = "verification"
  18. ctx, childSpan := span.Fork(ctx, serviceName, operationName)
  19. defer childSpan.Finish()
  20. assert.Equal(t, childSpan, ctx.Value(tracespec.TracingKey))
  21. assert.Equal(t, getSpan(span).TraceId(), getSpan(childSpan).TraceId())
  22. assert.Equal(t, "0.1.1", getSpan(childSpan).SpanId())
  23. assert.Equal(t, serviceName, childSpan.(*Span).serviceName)
  24. assert.Equal(t, operationName, childSpan.(*Span).operationName)
  25. assert.Equal(t, clientFlag, childSpan.(*Span).flag)
  26. }
  27. func TestClientSpan_WithoutTrace(t *testing.T) {
  28. ctx, span := StartClientSpan(context.Background(), "entrance", "operation")
  29. defer span.Finish()
  30. assert.Equal(t, emptyNoopSpan, span)
  31. assert.Equal(t, context.Background(), ctx)
  32. }
  33. func TestServerSpan(t *testing.T) {
  34. ctx, span := StartServerSpan(context.Background(), nil, "service", "operation")
  35. defer span.Finish()
  36. assert.Equal(t, span, ctx.Value(tracespec.TracingKey))
  37. const serviceName = "authorization"
  38. const operationName = "verification"
  39. ctx, childSpan := span.Fork(ctx, serviceName, operationName)
  40. defer childSpan.Finish()
  41. assert.Equal(t, childSpan, ctx.Value(tracespec.TracingKey))
  42. assert.Equal(t, getSpan(span).TraceId(), getSpan(childSpan).TraceId())
  43. assert.Equal(t, "0.1", getSpan(childSpan).SpanId())
  44. assert.Equal(t, serviceName, childSpan.(*Span).serviceName)
  45. assert.Equal(t, operationName, childSpan.(*Span).operationName)
  46. assert.Equal(t, clientFlag, childSpan.(*Span).flag)
  47. }
  48. func TestServerSpan_WithCarrier(t *testing.T) {
  49. md := metadata.New(map[string]string{
  50. traceIdKey: "a",
  51. spanIdKey: "0.1",
  52. })
  53. ctx, span := StartServerSpan(context.Background(), grpcCarrier(md), "service", "operation")
  54. defer span.Finish()
  55. assert.Equal(t, span, ctx.Value(tracespec.TracingKey))
  56. const serviceName = "authorization"
  57. const operationName = "verification"
  58. ctx, childSpan := span.Fork(ctx, serviceName, operationName)
  59. defer childSpan.Finish()
  60. assert.Equal(t, childSpan, ctx.Value(tracespec.TracingKey))
  61. assert.Equal(t, getSpan(span).TraceId(), getSpan(childSpan).TraceId())
  62. assert.Equal(t, "0.1.1", getSpan(childSpan).SpanId())
  63. assert.Equal(t, serviceName, childSpan.(*Span).serviceName)
  64. assert.Equal(t, operationName, childSpan.(*Span).operationName)
  65. assert.Equal(t, clientFlag, childSpan.(*Span).flag)
  66. }
  67. func TestSpan_Follow(t *testing.T) {
  68. tests := []struct {
  69. span string
  70. expectSpan string
  71. }{
  72. {
  73. "0.1",
  74. "0.2",
  75. },
  76. {
  77. "0",
  78. "1",
  79. },
  80. {
  81. "a",
  82. "a",
  83. },
  84. }
  85. for _, test := range tests {
  86. t.Run(stringx.RandId(), func(t *testing.T) {
  87. md := metadata.New(map[string]string{
  88. traceIdKey: "a",
  89. spanIdKey: test.span,
  90. })
  91. ctx, span := StartServerSpan(context.Background(), grpcCarrier(md),
  92. "service", "operation")
  93. defer span.Finish()
  94. assert.Equal(t, span, ctx.Value(tracespec.TracingKey))
  95. const serviceName = "authorization"
  96. const operationName = "verification"
  97. ctx, childSpan := span.Follow(ctx, serviceName, operationName)
  98. defer childSpan.Finish()
  99. assert.Equal(t, childSpan, ctx.Value(tracespec.TracingKey))
  100. assert.Equal(t, getSpan(span).TraceId(), getSpan(childSpan).TraceId())
  101. assert.Equal(t, test.expectSpan, getSpan(childSpan).SpanId())
  102. assert.Equal(t, serviceName, childSpan.(*Span).serviceName)
  103. assert.Equal(t, operationName, childSpan.(*Span).operationName)
  104. assert.Equal(t, span.(*Span).flag, childSpan.(*Span).flag)
  105. })
  106. }
  107. }
  108. func TestSpan_Visit(t *testing.T) {
  109. var run bool
  110. span := newServerSpan(nil, "service", "operation")
  111. span.Visit(func(key, val string) bool {
  112. assert.True(t, len(key) > 0)
  113. assert.True(t, len(val) > 0)
  114. run = true
  115. return true
  116. })
  117. assert.True(t, run)
  118. }
  119. func getSpan(span tracespec.Trace) tracespec.Trace {
  120. return span.(*Span)
  121. }