You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

149 lines
3.9 KiB

  1. // Copyright 2018, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jaeger
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "time"
  20. gen "go.opencensus.io/exporter/jaeger/internal/gen-go/jaeger"
  21. "go.opencensus.io/trace"
  22. "sort"
  23. )
  24. // TODO(jbd): Test export.
  25. func Test_bytesToInt64(t *testing.T) {
  26. type args struct {
  27. }
  28. tests := []struct {
  29. buf []byte
  30. want int64
  31. }{
  32. {
  33. buf: []byte{255, 0, 0, 0, 0, 0, 0, 0},
  34. want: -72057594037927936,
  35. },
  36. {
  37. buf: []byte{0, 0, 0, 0, 0, 0, 0, 1},
  38. want: 1,
  39. },
  40. {
  41. buf: []byte{0, 0, 0, 0, 0, 0, 0, 0},
  42. want: 0,
  43. },
  44. }
  45. for _, tt := range tests {
  46. t.Run(fmt.Sprintf("%d", tt.want), func(t *testing.T) {
  47. if got := bytesToInt64(tt.buf); got != tt.want {
  48. t.Errorf("bytesToInt64() = \n%v, \n want \n%v", got, tt.want)
  49. }
  50. })
  51. }
  52. }
  53. func Test_spanDataToThrift(t *testing.T) {
  54. now := time.Now()
  55. answerValue := int64(42)
  56. keyValue := "value"
  57. resultValue := true
  58. statusCodeValue := int64(2)
  59. doubleValue := float64(123.456)
  60. boolTrue := true
  61. statusMessage := "error"
  62. tests := []struct {
  63. name string
  64. data *trace.SpanData
  65. want *gen.Span
  66. }{
  67. {
  68. name: "no parent",
  69. data: &trace.SpanData{
  70. SpanContext: trace.SpanContext{
  71. TraceID: trace.TraceID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
  72. SpanID: trace.SpanID{1, 2, 3, 4, 5, 6, 7, 8},
  73. },
  74. Name: "/foo",
  75. StartTime: now,
  76. EndTime: now,
  77. Attributes: map[string]interface{}{
  78. "double": doubleValue,
  79. "key": keyValue,
  80. },
  81. Annotations: []trace.Annotation{
  82. {
  83. Time: now,
  84. Message: statusMessage,
  85. Attributes: map[string]interface{}{
  86. "answer": answerValue,
  87. },
  88. },
  89. {
  90. Time: now,
  91. Message: statusMessage,
  92. Attributes: map[string]interface{}{
  93. "result": resultValue,
  94. },
  95. },
  96. },
  97. Status: trace.Status{Code: trace.StatusCodeUnknown, Message: "error"},
  98. },
  99. want: &gen.Span{
  100. TraceIdLow: 651345242494996240,
  101. TraceIdHigh: 72623859790382856,
  102. SpanId: 72623859790382856,
  103. OperationName: "/foo",
  104. StartTime: now.UnixNano() / 1000,
  105. Duration: 0,
  106. Tags: []*gen.Tag{
  107. {Key: "double", VType: gen.TagType_DOUBLE, VDouble: &doubleValue},
  108. {Key: "key", VType: gen.TagType_STRING, VStr: &keyValue},
  109. {Key: "error", VType: gen.TagType_BOOL, VBool: &boolTrue},
  110. {Key: "status.code", VType: gen.TagType_LONG, VLong: &statusCodeValue},
  111. {Key: "status.message", VType: gen.TagType_STRING, VStr: &statusMessage},
  112. },
  113. Logs: []*gen.Log{
  114. {Timestamp: now.UnixNano() / 1000, Fields: []*gen.Tag{
  115. {Key: "answer", VType: gen.TagType_LONG, VLong: &answerValue},
  116. {Key: "message", VType: gen.TagType_STRING, VStr: &statusMessage},
  117. }},
  118. {Timestamp: now.UnixNano() / 1000, Fields: []*gen.Tag{
  119. {Key: "result", VType: gen.TagType_BOOL, VBool: &resultValue},
  120. {Key: "message", VType: gen.TagType_STRING, VStr: &statusMessage},
  121. }},
  122. },
  123. },
  124. },
  125. }
  126. for _, tt := range tests {
  127. t.Run(tt.name, func(t *testing.T) {
  128. got := spanDataToThrift(tt.data)
  129. sort.Slice(got.Tags, func(i, j int) bool {
  130. return got.Tags[i].Key < got.Tags[j].Key
  131. })
  132. sort.Slice(tt.want.Tags, func(i, j int) bool {
  133. return tt.want.Tags[i].Key < tt.want.Tags[j].Key
  134. })
  135. if !reflect.DeepEqual(got, tt.want) {
  136. t.Errorf("spanDataToThrift()\nGot:\n%v\nWant;\n%v", got, tt.want)
  137. }
  138. })
  139. }
  140. }