dapper_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package trace
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "google.golang.org/grpc/metadata"
  8. )
  9. type mockReport struct {
  10. sps []*span
  11. }
  12. func (m *mockReport) WriteSpan(sp *span) error {
  13. m.sps = append(m.sps, sp)
  14. return nil
  15. }
  16. func (m *mockReport) Close() error {
  17. return nil
  18. }
  19. func TestDapperPropagation(t *testing.T) {
  20. t.Run("test HTTP progagation", func(t *testing.T) {
  21. report := &mockReport{}
  22. t1 := newTracer("service1", report, &Config{DisableSample: true})
  23. t2 := newTracer("service2", report, &Config{DisableSample: true})
  24. sp1 := t1.New("opt_1")
  25. sp2 := sp1.Fork("", "opt_client")
  26. header := make(http.Header)
  27. t1.Inject(sp2, HTTPFormat, header)
  28. sp3, err := t2.Extract(HTTPFormat, header)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. sp3.Finish(nil)
  33. sp2.Finish(nil)
  34. sp1.Finish(nil)
  35. assert.Len(t, report.sps, 3)
  36. assert.Equal(t, report.sps[2].context.parentID, uint64(0))
  37. assert.Equal(t, report.sps[0].context.traceID, report.sps[1].context.traceID)
  38. assert.Equal(t, report.sps[2].context.traceID, report.sps[1].context.traceID)
  39. assert.Equal(t, report.sps[1].context.parentID, report.sps[2].context.spanID)
  40. assert.Equal(t, report.sps[0].context.parentID, report.sps[1].context.spanID)
  41. })
  42. t.Run("test gRPC progagation", func(t *testing.T) {
  43. report := &mockReport{}
  44. t1 := newTracer("service1", report, &Config{DisableSample: true})
  45. t2 := newTracer("service2", report, &Config{DisableSample: true})
  46. sp1 := t1.New("opt_1")
  47. sp2 := sp1.Fork("", "opt_client")
  48. md := make(metadata.MD)
  49. t1.Inject(sp2, GRPCFormat, md)
  50. sp3, err := t2.Extract(GRPCFormat, md)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. sp3.Finish(nil)
  55. sp2.Finish(nil)
  56. sp1.Finish(nil)
  57. assert.Len(t, report.sps, 3)
  58. assert.Equal(t, report.sps[2].context.parentID, uint64(0))
  59. assert.Equal(t, report.sps[0].context.traceID, report.sps[1].context.traceID)
  60. assert.Equal(t, report.sps[2].context.traceID, report.sps[1].context.traceID)
  61. assert.Equal(t, report.sps[1].context.parentID, report.sps[2].context.spanID)
  62. assert.Equal(t, report.sps[0].context.parentID, report.sps[1].context.spanID)
  63. })
  64. t.Run("test normal", func(t *testing.T) {
  65. report := &mockReport{}
  66. t1 := newTracer("service1", report, &Config{Probability: 0.000000001})
  67. sp1 := t1.New("test123")
  68. sp1.Finish(nil)
  69. })
  70. t.Run("test debug progagation", func(t *testing.T) {
  71. report := &mockReport{}
  72. t1 := newTracer("service1", report, &Config{})
  73. t2 := newTracer("service2", report, &Config{})
  74. sp1 := t1.New("opt_1", EnableDebug())
  75. sp2 := sp1.Fork("", "opt_client")
  76. header := make(http.Header)
  77. t1.Inject(sp2, HTTPFormat, header)
  78. sp3, err := t2.Extract(HTTPFormat, header)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. sp3.Finish(nil)
  83. sp2.Finish(nil)
  84. sp1.Finish(nil)
  85. assert.Len(t, report.sps, 3)
  86. assert.Equal(t, report.sps[2].context.parentID, uint64(0))
  87. assert.Equal(t, report.sps[0].context.traceID, report.sps[1].context.traceID)
  88. assert.Equal(t, report.sps[2].context.traceID, report.sps[1].context.traceID)
  89. assert.Equal(t, report.sps[1].context.parentID, report.sps[2].context.spanID)
  90. assert.Equal(t, report.sps[0].context.parentID, report.sps[1].context.spanID)
  91. })
  92. }
  93. func BenchmarkSample(b *testing.B) {
  94. err := fmt.Errorf("test error")
  95. report := &mockReport{}
  96. t1 := newTracer("service1", report, &Config{})
  97. for i := 0; i < b.N; i++ {
  98. sp1 := t1.New("test_opt1")
  99. sp1.SetTag(TagString("test", "123"))
  100. sp2 := sp1.Fork("", "opt2")
  101. sp3 := sp2.Fork("", "opt3")
  102. sp3.SetTag(TagString("test", "123"))
  103. sp3.Finish(nil)
  104. sp2.Finish(&err)
  105. sp1.Finish(nil)
  106. }
  107. }
  108. func BenchmarkDisableSample(b *testing.B) {
  109. err := fmt.Errorf("test error")
  110. report := &mockReport{}
  111. t1 := newTracer("service1", report, &Config{DisableSample: true})
  112. for i := 0; i < b.N; i++ {
  113. sp1 := t1.New("test_opt1")
  114. sp1.SetTag(TagString("test", "123"))
  115. sp2 := sp1.Fork("", "opt2")
  116. sp3 := sp2.Fork("", "opt3")
  117. sp3.SetTag(TagString("test", "123"))
  118. sp3.Finish(nil)
  119. sp2.Finish(&err)
  120. sp1.Finish(nil)
  121. }
  122. }