go-zero/core/logx/tracelogger_test.go

155 lines
5.2 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package logx
import (
"context"
2020-10-07 22:54:51 +08:00
"log"
2020-07-26 17:09:05 +08:00
"strings"
2020-10-09 19:13:10 +08:00
"sync/atomic"
2020-07-26 17:09:05 +08:00
"testing"
2020-10-07 22:54:51 +08:00
"time"
2020-07-26 17:09:05 +08:00
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
2020-07-26 17:09:05 +08:00
)
const (
traceKey = "trace"
spanKey = "span"
2020-07-26 17:09:05 +08:00
)
func TestTraceLog(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
atomic.StoreUint32(&initialized, 1)
otp := otel.GetTracerProvider()
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
otel.SetTracerProvider(tp)
defer otel.SetTracerProvider(otp)
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
WithContext(ctx).(*traceLogger).write(&buf, levelInfo, testlog)
assert.True(t, strings.Contains(buf.String(), traceKey))
assert.True(t, strings.Contains(buf.String(), spanKey))
2020-07-26 17:09:05 +08:00
}
2020-10-07 22:54:51 +08:00
func TestTraceError(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-10-10 15:36:07 +08:00
atomic.StoreUint32(&initialized, 1)
errorLog = newLogWriter(log.New(&buf, "", flags))
otp := otel.GetTracerProvider()
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
otel.SetTracerProvider(tp)
defer otel.SetTracerProvider(otp)
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
2020-10-07 22:54:51 +08:00
l := WithContext(ctx).(*traceLogger)
SetLevel(InfoLevel)
l.WithDuration(time.Second).Error(testlog)
assert.True(t, strings.Contains(buf.String(), traceKey))
assert.True(t, strings.Contains(buf.String(), spanKey))
2020-10-07 22:54:51 +08:00
buf.Reset()
l.WithDuration(time.Second).Errorf(testlog)
assert.True(t, strings.Contains(buf.String(), traceKey))
assert.True(t, strings.Contains(buf.String(), spanKey))
2021-12-13 11:58:58 +08:00
buf.Reset()
l.WithDuration(time.Second).Errorv(testlog)
assert.True(t, strings.Contains(buf.String(), traceKey))
assert.True(t, strings.Contains(buf.String(), spanKey))
2020-10-07 22:54:51 +08:00
}
func TestTraceInfo(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-10-10 15:36:07 +08:00
atomic.StoreUint32(&initialized, 1)
infoLog = newLogWriter(log.New(&buf, "", flags))
otp := otel.GetTracerProvider()
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
otel.SetTracerProvider(tp)
defer otel.SetTracerProvider(otp)
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
2020-10-07 22:54:51 +08:00
l := WithContext(ctx).(*traceLogger)
SetLevel(InfoLevel)
l.WithDuration(time.Second).Info(testlog)
assert.True(t, strings.Contains(buf.String(), traceKey))
assert.True(t, strings.Contains(buf.String(), spanKey))
2020-10-07 22:54:51 +08:00
buf.Reset()
l.WithDuration(time.Second).Infof(testlog)
assert.True(t, strings.Contains(buf.String(), traceKey))
assert.True(t, strings.Contains(buf.String(), spanKey))
2021-12-13 11:58:58 +08:00
buf.Reset()
l.WithDuration(time.Second).Infov(testlog)
assert.True(t, strings.Contains(buf.String(), traceKey))
assert.True(t, strings.Contains(buf.String(), spanKey))
2020-10-07 22:54:51 +08:00
}
func TestTraceInfoConsole(t *testing.T) {
2022-02-28 23:17:51 +08:00
old := atomic.LoadUint32(&encoding)
atomic.StoreUint32(&encoding, jsonEncodingType)
defer func() {
2022-02-28 23:17:51 +08:00
atomic.StoreUint32(&encoding, old)
}()
var buf mockWriter
atomic.StoreUint32(&initialized, 1)
infoLog = newLogWriter(log.New(&buf, "", flags))
otp := otel.GetTracerProvider()
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
otel.SetTracerProvider(tp)
defer otel.SetTracerProvider(otp)
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
l := WithContext(ctx).(*traceLogger)
SetLevel(InfoLevel)
l.WithDuration(time.Second).Info(testlog)
assert.True(t, strings.Contains(buf.String(), traceIdFromContext(ctx)))
assert.True(t, strings.Contains(buf.String(), spanIdFromContext(ctx)))
buf.Reset()
l.WithDuration(time.Second).Infof(testlog)
assert.True(t, strings.Contains(buf.String(), traceIdFromContext(ctx)))
assert.True(t, strings.Contains(buf.String(), spanIdFromContext(ctx)))
buf.Reset()
l.WithDuration(time.Second).Infov(testlog)
assert.True(t, strings.Contains(buf.String(), traceIdFromContext(ctx)))
assert.True(t, strings.Contains(buf.String(), spanIdFromContext(ctx)))
}
2020-10-07 22:54:51 +08:00
func TestTraceSlow(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-10-10 15:36:07 +08:00
atomic.StoreUint32(&initialized, 1)
slowLog = newLogWriter(log.New(&buf, "", flags))
otp := otel.GetTracerProvider()
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
otel.SetTracerProvider(tp)
defer otel.SetTracerProvider(otp)
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
2020-10-07 22:54:51 +08:00
l := WithContext(ctx).(*traceLogger)
SetLevel(InfoLevel)
l.WithDuration(time.Second).Slow(testlog)
assert.True(t, strings.Contains(buf.String(), traceKey))
assert.True(t, strings.Contains(buf.String(), spanKey))
2020-10-07 22:54:51 +08:00
buf.Reset()
l.WithDuration(time.Second).Slowf(testlog)
assert.True(t, strings.Contains(buf.String(), traceKey))
assert.True(t, strings.Contains(buf.String(), spanKey))
2021-12-13 11:58:58 +08:00
buf.Reset()
l.WithDuration(time.Second).Slowv(testlog)
assert.True(t, strings.Contains(buf.String(), traceKey))
assert.True(t, strings.Contains(buf.String(), spanKey))
2020-10-07 22:54:51 +08:00
}
func TestTraceWithoutContext(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-10-09 19:13:10 +08:00
atomic.StoreUint32(&initialized, 1)
2020-10-07 22:54:51 +08:00
infoLog = newLogWriter(log.New(&buf, "", flags))
2020-10-10 15:36:07 +08:00
l := WithContext(context.Background()).(*traceLogger)
SetLevel(InfoLevel)
2020-10-07 22:54:51 +08:00
l.WithDuration(time.Second).Info(testlog)
assert.False(t, strings.Contains(buf.String(), traceKey))
assert.False(t, strings.Contains(buf.String(), spanKey))
2020-10-07 22:54:51 +08:00
buf.Reset()
l.WithDuration(time.Second).Infof(testlog)
assert.False(t, strings.Contains(buf.String(), traceKey))
assert.False(t, strings.Contains(buf.String(), spanKey))
2020-07-26 17:09:05 +08:00
}