axonhub / internal /tracing /log_test.go
llzai's picture
Upload 1793 files
9853396 verified
package tracing_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/looplj/axonhub/internal/log"
"github.com/looplj/axonhub/internal/tracing"
)
func TestTraceHook(t *testing.T) {
hook := log.HookFunc(tracing.TraceFieldsHooks)
t.Run("with trace ID", func(t *testing.T) {
ctx := tracing.WithTraceID(context.Background(), "at-test-trace-id")
fields := hook.Apply(ctx, "test message")
assert.Len(t, fields, 1)
assert.Equal(t, "trace_id", fields[0].Key)
assert.Equal(t, "at-test-trace-id", fields[0].String)
})
t.Run("with operation name", func(t *testing.T) {
ctx := tracing.WithOperationName(context.Background(), "test-operation-name")
fields := hook.Apply(ctx, "test message")
assert.Len(t, fields, 1)
assert.Equal(t, "operation_name", fields[0].Key)
assert.Equal(t, "test-operation-name", fields[0].String)
})
t.Run("with context that has trace ID", func(t *testing.T) {
ctx := tracing.WithTraceID(context.Background(), "at-test-trace-id")
fields := hook.Apply(ctx, "test message")
assert.Len(t, fields, 1)
assert.Equal(t, "trace_id", fields[0].Key)
assert.Equal(t, "at-test-trace-id", fields[0].String)
})
t.Run("with context that doesn't have trace ID", func(t *testing.T) {
ctx := context.Background()
fields := hook.Apply(ctx, "test message")
assert.Len(t, fields, 0)
})
t.Run("with nil context", func(t *testing.T) {
fields := hook.Apply(nil, "test message")
assert.Len(t, fields, 0)
})
}