tracelogger_test.go 5.3 KB

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