span_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package span
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "testing"
  6. )
  7. func TestNewRootSpanID(t *testing.T) {
  8. id := NewRootSpanID()
  9. if id.Parent != 0 {
  10. t.Errorf("unexpected parent: %+v", id)
  11. }
  12. if id.Span == 0 {
  13. t.Errorf("zero Span: %+v", id)
  14. }
  15. if id.Trace == 0 {
  16. t.Errorf("zero root: %+v", id)
  17. }
  18. if id.Trace == id.Span {
  19. t.Errorf("duplicate IDs: %+v", id)
  20. }
  21. }
  22. func TestNewSpanID(t *testing.T) {
  23. root := NewRootSpanID()
  24. id := NewSpanID(root)
  25. if id.Parent != root.Span {
  26. t.Errorf("unexpected parent: %+v", id)
  27. }
  28. if id.Span == 0 {
  29. t.Errorf("zero Span: %+v", id)
  30. }
  31. if id.Trace != root.Trace {
  32. t.Errorf("mismatched root: %+v", id)
  33. }
  34. }
  35. func TestSpanIDString(t *testing.T) {
  36. id := SpanID{
  37. Trace: 100,
  38. Span: 300,
  39. }
  40. got := id.String()
  41. want := "0000000000000064/000000000000012c"
  42. if got != want {
  43. t.Errorf("got %#v, want %#v", got, want)
  44. }
  45. }
  46. func TestSpanIDStringWithParent(t *testing.T) {
  47. id := SpanID{
  48. Trace: 100,
  49. Parent: 200,
  50. Span: 300,
  51. }
  52. actual := id.String()
  53. expected := "0000000000000064/000000000000012c/00000000000000c8"
  54. if actual != expected {
  55. t.Errorf("Was %#v, but expected %#v", actual, expected)
  56. }
  57. }
  58. func TestSpanIDFormat(t *testing.T) {
  59. id := SpanID{
  60. Trace: 100,
  61. Span: 300,
  62. }
  63. got := id.Format("/* %s */ %s", "SELECT 1")
  64. want := "/* 0000000000000064/000000000000012c */ SELECT 1"
  65. if got != want {
  66. t.Errorf("got %#v, want %#v", got, want)
  67. }
  68. }
  69. func ExampleSpanID_Format() {
  70. // Assume we're connected to a database.
  71. var (
  72. event SpanID
  73. db *sql.DB
  74. userID int
  75. )
  76. // This passes the root ID and the parent event ID to the database, which
  77. // allows us to correlate, for example, slow queries with the web requests
  78. // which caused them.
  79. query := event.Format(`/* %s/%s */ %s`, `SELECT email FROM users WHERE id = ?`)
  80. r := db.QueryRow(query, userID)
  81. if r == nil {
  82. panic("user not found")
  83. }
  84. var email string
  85. if err := r.Scan(&email); err != nil {
  86. panic("couldn't read email")
  87. }
  88. fmt.Printf("User's email: %s\n", email)
  89. }
  90. func TestParseSpanID(t *testing.T) {
  91. id, err := ParseSpanID("0000000000000064/000000000000012c")
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. if id.Trace != 100 || id.Span != 300 {
  96. t.Errorf("unexpected ID: %+v", id)
  97. }
  98. }
  99. func TestParseSpanIDWithParent(t *testing.T) {
  100. id, err := ParseSpanID("0000000000000064/000000000000012c/0000000000000096")
  101. if err != nil {
  102. t.Fatalf("unexpected error: %v", err)
  103. }
  104. if id.Trace != 100 || id.Parent != 150 || id.Span != 300 {
  105. t.Errorf("unexpected event ID: %+v", id)
  106. }
  107. }
  108. func TestParseSpanIDMalformed(t *testing.T) {
  109. id, err := ParseSpanID(`0000000000000064000000000000012c`)
  110. if id != nil {
  111. t.Errorf("unexpected ID: %+v", id)
  112. }
  113. if err != ErrBadSpanID {
  114. t.Error(err)
  115. }
  116. }
  117. func TestParseSpanIDBadTrace(t *testing.T) {
  118. id, err := ParseSpanID("0000000000g000064/000000000000012c")
  119. if id != nil {
  120. t.Errorf("unexpected ID: %+v", id)
  121. }
  122. if err != ErrBadSpanID {
  123. t.Error(err)
  124. }
  125. }
  126. func TestParseSpanIDBadID(t *testing.T) {
  127. id, err := ParseSpanID("0000000000000064/0000000000g00012c")
  128. if id != nil {
  129. t.Errorf("unexpected ID: %+v", id)
  130. }
  131. if err != ErrBadSpanID {
  132. t.Error(err)
  133. }
  134. }
  135. func TestParseSpanIDBadParent(t *testing.T) {
  136. id, err := ParseSpanID("0000000000000064/000000000000012c/00000000000g0096")
  137. if id != nil {
  138. t.Errorf("unexpected event ID: %+v", id)
  139. }
  140. if err != ErrBadSpanID {
  141. t.Errorf("unexpected error: %v", err)
  142. }
  143. }
  144. func BenchmarkNewRootSpanID(b *testing.B) {
  145. for i := 0; i < b.N; i++ {
  146. NewRootSpanID()
  147. }
  148. }
  149. func BenchmarkNewSpanID(b *testing.B) {
  150. root := NewRootSpanID()
  151. for i := 0; i < b.N; i++ {
  152. NewSpanID(root)
  153. }
  154. }
  155. func BenchmarkSpanIDString(b *testing.B) {
  156. id := SpanID{
  157. Trace: 100,
  158. Parent: 200,
  159. Span: 300,
  160. }
  161. for i := 0; i < b.N; i++ {
  162. _ = id.String()
  163. }
  164. }
  165. func BenchmarkParseSpanID(b *testing.B) {
  166. for i := 0; i < b.N; i++ {
  167. _, err := ParseSpanID("0000000000000064/000000000000012c")
  168. if err != nil {
  169. b.Fatal(err)
  170. }
  171. }
  172. }