span_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package trace
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestSpan(t *testing.T) {
  11. report := &mockReport{}
  12. t1 := newTracer("service1", report, &Config{DisableSample: true})
  13. t.Run("test span string", func(t *testing.T) {
  14. sp1 := t1.New("testfinish").(*span)
  15. assert.NotEmpty(t, fmt.Sprint(sp1))
  16. })
  17. t.Run("test fork", func(t *testing.T) {
  18. sp1 := t1.New("testfork").(*span)
  19. sp2 := sp1.Fork("xxx", "opt_2").(*span)
  20. assert.Equal(t, sp1.context.traceID, sp2.context.traceID)
  21. assert.Equal(t, sp1.context.spanID, sp2.context.parentID)
  22. t.Run("test max fork", func(t *testing.T) {
  23. sp3 := sp2.Fork("xx", "xxx")
  24. for i := 0; i < 100; i++ {
  25. sp3 = sp3.Fork("", "xxx")
  26. }
  27. assert.Equal(t, noopspan{}, sp3)
  28. })
  29. t.Run("test max childs", func(t *testing.T) {
  30. sp3 := sp2.Fork("xx", "xxx")
  31. for i := 0; i < 4096; i++ {
  32. sp3.Fork("", "xxx")
  33. }
  34. assert.Equal(t, noopspan{}, sp3.Fork("xx", "xx"))
  35. })
  36. })
  37. t.Run("test finish", func(t *testing.T) {
  38. t.Run("test finish ok", func(t *testing.T) {
  39. sp1 := t1.New("testfinish").(*span)
  40. time.Sleep(time.Millisecond)
  41. sp1.Finish(nil)
  42. assert.True(t, sp1.startTime.Unix() > 0)
  43. assert.True(t, sp1.duration > time.Microsecond)
  44. })
  45. t.Run("test finish error", func(t *testing.T) {
  46. sp1 := t1.New("testfinish").(*span)
  47. time.Sleep(time.Millisecond)
  48. err := fmt.Errorf("🍻")
  49. sp1.Finish(&err)
  50. assert.True(t, sp1.startTime.Unix() > 0)
  51. assert.True(t, sp1.duration > time.Microsecond)
  52. errorTag := false
  53. for _, tag := range sp1.tags {
  54. if tag.Key == TagError && tag.Value != nil {
  55. errorTag = true
  56. }
  57. }
  58. assert.True(t, errorTag)
  59. messageLog := false
  60. for _, log := range sp1.logs {
  61. assert.True(t, log.Timestamp != 0)
  62. for _, field := range log.Fields {
  63. if field.Key == LogMessage && len(field.Value) != 0 {
  64. messageLog = true
  65. }
  66. }
  67. }
  68. assert.True(t, messageLog)
  69. })
  70. t.Run("test finish error stack", func(t *testing.T) {
  71. sp1 := t1.New("testfinish").(*span)
  72. time.Sleep(time.Millisecond)
  73. err := fmt.Errorf("🍻")
  74. err = errors.WithStack(err)
  75. sp1.Finish(&err)
  76. ok := false
  77. for _, log := range sp1.logs {
  78. for _, field := range log.Fields {
  79. if field.Key == LogStack && len(field.Value) != 0 {
  80. ok = true
  81. }
  82. }
  83. }
  84. assert.True(t, ok, "LogStack set")
  85. })
  86. t.Run("test too many tags", func(t *testing.T) {
  87. sp1 := t1.New("testfinish").(*span)
  88. for i := 0; i < 1024; i++ {
  89. sp1.SetTag(Tag{Key: strconv.Itoa(i), Value: "hello"})
  90. }
  91. assert.Len(t, sp1.tags, _maxTags+1)
  92. assert.Equal(t, sp1.tags[_maxTags].Key, "trace.error")
  93. assert.Equal(t, sp1.tags[_maxTags].Value, "too many tags")
  94. })
  95. t.Run("test too many logs", func(t *testing.T) {
  96. sp1 := t1.New("testfinish").(*span)
  97. for i := 0; i < 1024; i++ {
  98. sp1.SetLog(LogField{Key: strconv.Itoa(i), Value: "hello"})
  99. }
  100. assert.Len(t, sp1.logs, _maxLogs+1)
  101. assert.Equal(t, sp1.logs[_maxLogs].Fields[0].Key, "trace.error")
  102. assert.Equal(t, sp1.logs[_maxLogs].Fields[0].Value, []byte("too many logs"))
  103. })
  104. })
  105. }