tracelogger_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package logx
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "sync/atomic"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "go.opentelemetry.io/otel"
  12. sdktrace "go.opentelemetry.io/otel/sdk/trace"
  13. )
  14. func TestTraceLog(t *testing.T) {
  15. SetLevel(InfoLevel)
  16. w := new(mockWriter)
  17. old := writer.Swap(w)
  18. writer.lock.RLock()
  19. defer func() {
  20. writer.lock.RUnlock()
  21. writer.Store(old)
  22. }()
  23. otp := otel.GetTracerProvider()
  24. tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
  25. otel.SetTracerProvider(tp)
  26. defer otel.SetTracerProvider(otp)
  27. ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
  28. defer span.End()
  29. WithContext(ctx).Info(testlog)
  30. validate(t, w.String(), true, true)
  31. }
  32. func TestTraceError(t *testing.T) {
  33. w := new(mockWriter)
  34. old := writer.Swap(w)
  35. writer.lock.RLock()
  36. defer func() {
  37. writer.lock.RUnlock()
  38. writer.Store(old)
  39. }()
  40. otp := otel.GetTracerProvider()
  41. tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
  42. otel.SetTracerProvider(tp)
  43. defer otel.SetTracerProvider(otp)
  44. ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
  45. defer span.End()
  46. var nilCtx context.Context
  47. l := WithContext(context.Background())
  48. l = l.WithContext(nilCtx)
  49. l = l.WithContext(ctx)
  50. SetLevel(ErrorLevel)
  51. l.WithDuration(time.Second).Error(testlog)
  52. validate(t, w.String(), true, true)
  53. w.Reset()
  54. l.WithDuration(time.Second).Errorf(testlog)
  55. validate(t, w.String(), true, true)
  56. w.Reset()
  57. l.WithDuration(time.Second).Errorv(testlog)
  58. fmt.Println(w.String())
  59. validate(t, w.String(), true, true)
  60. w.Reset()
  61. l.WithDuration(time.Second).Errorw(testlog, Field("foo", "bar"))
  62. fmt.Println(w.String())
  63. validate(t, w.String(), true, true)
  64. assert.True(t, strings.Contains(w.String(), "foo"), w.String())
  65. assert.True(t, strings.Contains(w.String(), "bar"), w.String())
  66. }
  67. func TestTraceInfo(t *testing.T) {
  68. w := new(mockWriter)
  69. old := writer.Swap(w)
  70. writer.lock.RLock()
  71. defer func() {
  72. writer.lock.RUnlock()
  73. writer.Store(old)
  74. }()
  75. otp := otel.GetTracerProvider()
  76. tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
  77. otel.SetTracerProvider(tp)
  78. defer otel.SetTracerProvider(otp)
  79. ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
  80. defer span.End()
  81. SetLevel(InfoLevel)
  82. l := WithContext(ctx)
  83. l.WithDuration(time.Second).Info(testlog)
  84. validate(t, w.String(), true, true)
  85. w.Reset()
  86. l.WithDuration(time.Second).Infof(testlog)
  87. validate(t, w.String(), true, true)
  88. w.Reset()
  89. l.WithDuration(time.Second).Infov(testlog)
  90. validate(t, w.String(), true, true)
  91. w.Reset()
  92. l.WithDuration(time.Second).Infow(testlog, Field("foo", "bar"))
  93. validate(t, w.String(), true, true)
  94. assert.True(t, strings.Contains(w.String(), "foo"), w.String())
  95. assert.True(t, strings.Contains(w.String(), "bar"), w.String())
  96. }
  97. func TestTraceInfoConsole(t *testing.T) {
  98. old := atomic.SwapUint32(&encoding, jsonEncodingType)
  99. defer atomic.StoreUint32(&encoding, old)
  100. w := new(mockWriter)
  101. o := writer.Swap(w)
  102. writer.lock.RLock()
  103. defer func() {
  104. writer.lock.RUnlock()
  105. writer.Store(o)
  106. }()
  107. otp := otel.GetTracerProvider()
  108. tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
  109. otel.SetTracerProvider(tp)
  110. defer otel.SetTracerProvider(otp)
  111. ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
  112. defer span.End()
  113. l := WithContext(ctx)
  114. SetLevel(InfoLevel)
  115. l.WithDuration(time.Second).Info(testlog)
  116. validate(t, w.String(), true, true)
  117. w.Reset()
  118. l.WithDuration(time.Second).Infof(testlog)
  119. validate(t, w.String(), true, true)
  120. w.Reset()
  121. l.WithDuration(time.Second).Infov(testlog)
  122. validate(t, w.String(), true, true)
  123. }
  124. func TestTraceSlow(t *testing.T) {
  125. w := new(mockWriter)
  126. old := writer.Swap(w)
  127. writer.lock.RLock()
  128. defer func() {
  129. writer.lock.RUnlock()
  130. writer.Store(old)
  131. }()
  132. otp := otel.GetTracerProvider()
  133. tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
  134. otel.SetTracerProvider(tp)
  135. defer otel.SetTracerProvider(otp)
  136. ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
  137. defer span.End()
  138. l := WithContext(ctx)
  139. SetLevel(InfoLevel)
  140. l.WithDuration(time.Second).Slow(testlog)
  141. assert.True(t, strings.Contains(w.String(), traceKey))
  142. assert.True(t, strings.Contains(w.String(), spanKey))
  143. w.Reset()
  144. l.WithDuration(time.Second).Slowf(testlog)
  145. fmt.Println("buf:", w.String())
  146. validate(t, w.String(), true, true)
  147. w.Reset()
  148. l.WithDuration(time.Second).Slowv(testlog)
  149. validate(t, w.String(), true, true)
  150. w.Reset()
  151. l.WithDuration(time.Second).Sloww(testlog, Field("foo", "bar"))
  152. validate(t, w.String(), true, true)
  153. assert.True(t, strings.Contains(w.String(), "foo"), w.String())
  154. assert.True(t, strings.Contains(w.String(), "bar"), w.String())
  155. }
  156. func TestTraceWithoutContext(t *testing.T) {
  157. w := new(mockWriter)
  158. old := writer.Swap(w)
  159. writer.lock.RLock()
  160. defer func() {
  161. writer.lock.RUnlock()
  162. writer.Store(old)
  163. }()
  164. l := WithContext(context.Background())
  165. SetLevel(InfoLevel)
  166. l.WithDuration(time.Second).Info(testlog)
  167. validate(t, w.String(), false, false)
  168. w.Reset()
  169. l.WithDuration(time.Second).Infof(testlog)
  170. validate(t, w.String(), false, false)
  171. }
  172. func validate(t *testing.T, body string, expectedTrace, expectedSpan bool) {
  173. var val mockValue
  174. assert.Nil(t, json.Unmarshal([]byte(body), &val), body)
  175. assert.Equal(t, expectedTrace, len(val.Trace) > 0, body)
  176. assert.Equal(t, expectedSpan, len(val.Span) > 0, body)
  177. }
  178. type mockValue struct {
  179. Trace string `json:"trace"`
  180. Span string `json:"span"`
  181. }