tracelogger_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package logx
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tal-tech/go-zero/core/trace/tracespec"
  8. )
  9. const (
  10. mockTraceId = "mock-trace-id"
  11. mockSpanId = "mock-span-id"
  12. )
  13. var mock tracespec.Trace = new(mockTrace)
  14. func TestTraceLog(t *testing.T) {
  15. var buf strings.Builder
  16. ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
  17. WithContext(ctx).(*traceLogger).write(&buf, levelInfo, testlog)
  18. assert.True(t, strings.Contains(buf.String(), mockTraceId))
  19. assert.True(t, strings.Contains(buf.String(), mockSpanId))
  20. }
  21. type mockTrace struct{}
  22. func (t mockTrace) TraceId() string {
  23. return mockTraceId
  24. }
  25. func (t mockTrace) SpanId() string {
  26. return mockSpanId
  27. }
  28. func (t mockTrace) Finish() {
  29. }
  30. func (t mockTrace) Fork(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
  31. return nil, nil
  32. }
  33. func (t mockTrace) Follow(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
  34. return nil, nil
  35. }
  36. func (t mockTrace) Visit(fn func(key string, val string) bool) {
  37. }