tracelogger_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. defer writer.Store(old)
  19. otp := otel.GetTracerProvider()
  20. tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
  21. otel.SetTracerProvider(tp)
  22. defer otel.SetTracerProvider(otp)
  23. ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
  24. defer span.End()
  25. WithContext(ctx).Info(testlog)
  26. validate(t, w.String(), true, true)
  27. }
  28. func TestTraceError(t *testing.T) {
  29. w := new(mockWriter)
  30. old := writer.Swap(w)
  31. defer writer.Store(old)
  32. otp := otel.GetTracerProvider()
  33. tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
  34. otel.SetTracerProvider(tp)
  35. defer otel.SetTracerProvider(otp)
  36. ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
  37. defer span.End()
  38. var nilCtx context.Context
  39. l := WithContext(context.Background())
  40. l = l.WithContext(nilCtx)
  41. l = l.WithContext(ctx)
  42. SetLevel(InfoLevel)
  43. l.WithDuration(time.Second).Error(testlog)
  44. validate(t, w.String(), true, true)
  45. w.Reset()
  46. l.WithDuration(time.Second).Errorf(testlog)
  47. validate(t, w.String(), true, true)
  48. w.Reset()
  49. l.WithDuration(time.Second).Errorv(testlog)
  50. fmt.Println(w.String())
  51. validate(t, w.String(), true, true)
  52. w.Reset()
  53. l.WithDuration(time.Second).Errorw(testlog, Field("foo", "bar"))
  54. validate(t, w.String(), true, true)
  55. assert.True(t, strings.Contains(w.String(), "foo"), w.String())
  56. assert.True(t, strings.Contains(w.String(), "bar"), w.String())
  57. }
  58. func TestTraceInfo(t *testing.T) {
  59. w := new(mockWriter)
  60. old := writer.Swap(w)
  61. defer writer.Store(old)
  62. otp := otel.GetTracerProvider()
  63. tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
  64. otel.SetTracerProvider(tp)
  65. defer otel.SetTracerProvider(otp)
  66. ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
  67. defer span.End()
  68. SetLevel(InfoLevel)
  69. l := WithContext(ctx)
  70. l.WithDuration(time.Second).Info(testlog)
  71. validate(t, w.String(), true, true)
  72. w.Reset()
  73. l.WithDuration(time.Second).Infof(testlog)
  74. validate(t, w.String(), true, true)
  75. w.Reset()
  76. l.WithDuration(time.Second).Infov(testlog)
  77. validate(t, w.String(), true, true)
  78. w.Reset()
  79. l.WithDuration(time.Second).Infow(testlog, Field("foo", "bar"))
  80. validate(t, w.String(), true, true)
  81. assert.True(t, strings.Contains(w.String(), "foo"), w.String())
  82. assert.True(t, strings.Contains(w.String(), "bar"), w.String())
  83. }
  84. func TestTraceInfoConsole(t *testing.T) {
  85. old := atomic.SwapUint32(&encoding, jsonEncodingType)
  86. defer atomic.StoreUint32(&encoding, old)
  87. w := new(mockWriter)
  88. o := writer.Swap(w)
  89. defer writer.Store(o)
  90. otp := otel.GetTracerProvider()
  91. tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
  92. otel.SetTracerProvider(tp)
  93. defer otel.SetTracerProvider(otp)
  94. ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
  95. defer span.End()
  96. l := WithContext(ctx)
  97. SetLevel(InfoLevel)
  98. l.WithDuration(time.Second).Info(testlog)
  99. validate(t, w.String(), true, true)
  100. w.Reset()
  101. l.WithDuration(time.Second).Infof(testlog)
  102. validate(t, w.String(), true, true)
  103. w.Reset()
  104. l.WithDuration(time.Second).Infov(testlog)
  105. validate(t, w.String(), true, true)
  106. }
  107. func TestTraceSlow(t *testing.T) {
  108. w := new(mockWriter)
  109. old := writer.Swap(w)
  110. defer writer.Store(old)
  111. otp := otel.GetTracerProvider()
  112. tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
  113. otel.SetTracerProvider(tp)
  114. defer otel.SetTracerProvider(otp)
  115. ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
  116. defer span.End()
  117. l := WithContext(ctx)
  118. SetLevel(InfoLevel)
  119. l.WithDuration(time.Second).Slow(testlog)
  120. assert.True(t, strings.Contains(w.String(), traceKey))
  121. assert.True(t, strings.Contains(w.String(), spanKey))
  122. w.Reset()
  123. l.WithDuration(time.Second).Slowf(testlog)
  124. fmt.Println("buf:", w.String())
  125. validate(t, w.String(), true, true)
  126. w.Reset()
  127. l.WithDuration(time.Second).Slowv(testlog)
  128. validate(t, w.String(), true, true)
  129. w.Reset()
  130. l.WithDuration(time.Second).Sloww(testlog, Field("foo", "bar"))
  131. validate(t, w.String(), true, true)
  132. assert.True(t, strings.Contains(w.String(), "foo"), w.String())
  133. assert.True(t, strings.Contains(w.String(), "bar"), w.String())
  134. }
  135. func TestTraceWithoutContext(t *testing.T) {
  136. w := new(mockWriter)
  137. old := writer.Swap(w)
  138. defer writer.Store(old)
  139. l := WithContext(context.Background())
  140. SetLevel(InfoLevel)
  141. l.WithDuration(time.Second).Info(testlog)
  142. validate(t, w.String(), false, false)
  143. w.Reset()
  144. l.WithDuration(time.Second).Infof(testlog)
  145. validate(t, w.String(), false, false)
  146. }
  147. func validate(t *testing.T, body string, expectedTrace, expectedSpan bool) {
  148. var val mockValue
  149. assert.Nil(t, json.Unmarshal([]byte(body), &val), body)
  150. assert.Equal(t, expectedTrace, len(val.Trace) > 0, body)
  151. assert.Equal(t, expectedSpan, len(val.Span) > 0, body)
  152. }
  153. type mockValue struct {
  154. Trace string `json:"trace"`
  155. Span string `json:"span"`
  156. }