|
|
@ -4,167 +4,103 @@ import (
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"net"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
red "github.com/go-redis/redis/v8"
|
|
|
|
red "github.com/redis/go-redis/v9"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/zeromicro/go-zero/core/breaker"
|
|
|
|
"github.com/zeromicro/go-zero/core/breaker"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx/logtest"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx/logtest"
|
|
|
|
ztrace "github.com/zeromicro/go-zero/core/trace"
|
|
|
|
"github.com/zeromicro/go-zero/core/trace/tracetest"
|
|
|
|
tracesdk "go.opentelemetry.io/otel/trace"
|
|
|
|
tracesdk "go.opentelemetry.io/otel/trace"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func TestHookProcessCase1(t *testing.T) {
|
|
|
|
func TestHookProcessCase1(t *testing.T) {
|
|
|
|
ztrace.StartAgent(ztrace.Config{
|
|
|
|
tracetest.NewInMemoryExporter(t)
|
|
|
|
Name: "go-zero-test",
|
|
|
|
w := logtest.NewCollector(t)
|
|
|
|
Endpoint: "http://localhost:14268/api/traces",
|
|
|
|
|
|
|
|
Batcher: "jaeger",
|
|
|
|
|
|
|
|
Sampler: 1.0,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
defer ztrace.StopAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
writer := log.Writer()
|
|
|
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
|
|
|
log.SetOutput(&buf)
|
|
|
|
|
|
|
|
defer log.SetOutput(writer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx, err := durationHook.BeforeProcess(context.Background(), red.NewCmd(context.Background()))
|
|
|
|
err := durationHook.ProcessHook(func(ctx context.Context, cmd red.Cmder) error {
|
|
|
|
|
|
|
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})(context.Background(), red.NewCmd(context.Background()))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
assert.Nil(t, durationHook.AfterProcess(ctx, red.NewCmd(context.Background())))
|
|
|
|
assert.False(t, strings.Contains(w.String(), "slow"))
|
|
|
|
assert.False(t, strings.Contains(buf.String(), "slow"))
|
|
|
|
|
|
|
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestHookProcessCase2(t *testing.T) {
|
|
|
|
func TestHookProcessCase2(t *testing.T) {
|
|
|
|
ztrace.StartAgent(ztrace.Config{
|
|
|
|
tracetest.NewInMemoryExporter(t)
|
|
|
|
Name: "go-zero-test",
|
|
|
|
|
|
|
|
Endpoint: "http://localhost:14268/api/traces",
|
|
|
|
|
|
|
|
Batcher: "jaeger",
|
|
|
|
|
|
|
|
Sampler: 1.0,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
defer ztrace.StopAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w := logtest.NewCollector(t)
|
|
|
|
w := logtest.NewCollector(t)
|
|
|
|
ctx, err := durationHook.BeforeProcess(context.Background(), red.NewCmd(context.Background()))
|
|
|
|
|
|
|
|
|
|
|
|
err := durationHook.ProcessHook(func(ctx context.Context, cmd red.Cmder) error {
|
|
|
|
|
|
|
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
|
|
|
|
|
|
|
time.Sleep(slowThreshold.Load() + time.Millisecond)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})(context.Background(), red.NewCmd(context.Background()))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
time.Sleep(slowThreshold.Load() + time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert.Nil(t, durationHook.AfterProcess(ctx, red.NewCmd(context.Background(), "foo", "bar")))
|
|
|
|
|
|
|
|
assert.True(t, strings.Contains(w.String(), "slow"))
|
|
|
|
assert.True(t, strings.Contains(w.String(), "slow"))
|
|
|
|
}
|
|
|
|
assert.True(t, strings.Contains(w.String(), "trace"))
|
|
|
|
|
|
|
|
assert.True(t, strings.Contains(w.String(), "span"))
|
|
|
|
func TestHookProcessCase3(t *testing.T) {
|
|
|
|
|
|
|
|
writer := log.Writer()
|
|
|
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
|
|
|
log.SetOutput(&buf)
|
|
|
|
|
|
|
|
defer log.SetOutput(writer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert.Nil(t, durationHook.AfterProcess(context.Background(), red.NewCmd(context.Background())))
|
|
|
|
|
|
|
|
assert.True(t, buf.Len() == 0)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestHookProcessCase4(t *testing.T) {
|
|
|
|
|
|
|
|
writer := log.Writer()
|
|
|
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
|
|
|
log.SetOutput(&buf)
|
|
|
|
|
|
|
|
defer log.SetOutput(writer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), startTimeKey, "foo")
|
|
|
|
|
|
|
|
assert.Nil(t, durationHook.AfterProcess(ctx, red.NewCmd(context.Background())))
|
|
|
|
|
|
|
|
assert.True(t, buf.Len() == 0)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestHookProcessPipelineCase1(t *testing.T) {
|
|
|
|
func TestHookProcessPipelineCase1(t *testing.T) {
|
|
|
|
ztrace.StartAgent(ztrace.Config{
|
|
|
|
tracetest.NewInMemoryExporter(t)
|
|
|
|
Name: "go-zero-test",
|
|
|
|
w := logtest.NewCollector(t)
|
|
|
|
Endpoint: "http://localhost:14268/api/traces",
|
|
|
|
|
|
|
|
Batcher: "jaeger",
|
|
|
|
|
|
|
|
Sampler: 1.0,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
defer ztrace.StopAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
writer := log.Writer()
|
|
|
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
|
|
|
log.SetOutput(&buf)
|
|
|
|
|
|
|
|
defer log.SetOutput(writer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{})
|
|
|
|
err := durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})(context.Background(), nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{
|
|
|
|
|
|
|
|
|
|
|
|
err = durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
|
|
|
|
|
|
|
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})(context.Background(), []red.Cmder{
|
|
|
|
red.NewCmd(context.Background()),
|
|
|
|
red.NewCmd(context.Background()),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
assert.NoError(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{}))
|
|
|
|
assert.False(t, strings.Contains(w.String(), "slow"))
|
|
|
|
assert.NoError(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
|
|
|
|
|
|
|
|
red.NewCmd(context.Background()),
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert.False(t, strings.Contains(buf.String(), "slow"))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestHookProcessPipelineCase2(t *testing.T) {
|
|
|
|
func TestHookProcessPipelineCase2(t *testing.T) {
|
|
|
|
ztrace.StartAgent(ztrace.Config{
|
|
|
|
tracetest.NewInMemoryExporter(t)
|
|
|
|
Name: "go-zero-test",
|
|
|
|
|
|
|
|
Endpoint: "http://localhost:14268/api/traces",
|
|
|
|
|
|
|
|
Batcher: "jaeger",
|
|
|
|
|
|
|
|
Sampler: 1.0,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
defer ztrace.StopAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w := logtest.NewCollector(t)
|
|
|
|
w := logtest.NewCollector(t)
|
|
|
|
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{
|
|
|
|
|
|
|
|
|
|
|
|
err := durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
|
|
|
|
|
|
|
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
|
|
|
|
|
|
|
time.Sleep(slowThreshold.Load() + time.Millisecond)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})(context.Background(), []red.Cmder{
|
|
|
|
red.NewCmd(context.Background()),
|
|
|
|
red.NewCmd(context.Background()),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
time.Sleep(slowThreshold.Load() + time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
|
|
|
|
|
|
|
|
red.NewCmd(context.Background(), "foo", "bar"),
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert.True(t, strings.Contains(w.String(), "slow"))
|
|
|
|
assert.True(t, strings.Contains(w.String(), "slow"))
|
|
|
|
|
|
|
|
assert.True(t, strings.Contains(w.String(), "trace"))
|
|
|
|
|
|
|
|
assert.True(t, strings.Contains(w.String(), "span"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestHookProcessPipelineCase3(t *testing.T) {
|
|
|
|
func TestHookProcessPipelineCase3(t *testing.T) {
|
|
|
|
w := logtest.NewCollector(t)
|
|
|
|
te := tracetest.NewInMemoryExporter(t)
|
|
|
|
|
|
|
|
|
|
|
|
assert.Nil(t, durationHook.AfterProcessPipeline(context.Background(), []red.Cmder{
|
|
|
|
err := durationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
|
|
|
|
red.NewCmd(context.Background()),
|
|
|
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
|
|
|
}))
|
|
|
|
return assert.AnError
|
|
|
|
assert.True(t, len(w.String()) == 0)
|
|
|
|
})(context.Background(), []red.Cmder{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestHookProcessPipelineCase4(t *testing.T) {
|
|
|
|
|
|
|
|
w := logtest.NewCollector(t)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), startTimeKey, "foo")
|
|
|
|
|
|
|
|
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
|
|
|
|
|
|
|
|
red.NewCmd(context.Background()),
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert.True(t, len(w.String()) == 0)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestHookProcessPipelineCase5(t *testing.T) {
|
|
|
|
|
|
|
|
writer := log.Writer()
|
|
|
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
|
|
|
log.SetOutput(&buf)
|
|
|
|
|
|
|
|
defer log.SetOutput(writer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), startTimeKey, "foo")
|
|
|
|
|
|
|
|
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
|
|
|
|
|
|
|
|
red.NewCmd(context.Background()),
|
|
|
|
red.NewCmd(context.Background()),
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
assert.True(t, buf.Len() == 0)
|
|
|
|
assert.ErrorIs(t, err, assert.AnError)
|
|
|
|
|
|
|
|
traceLogs := te.GetSpans().Snapshots()[0]
|
|
|
|
|
|
|
|
assert.Equal(t, "redis", traceLogs.Name())
|
|
|
|
|
|
|
|
assert.Equal(t, assert.AnError.Error(), traceLogs.Events()[0].Attributes[1].Value.AsString(), "trace should record error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestLogDuration(t *testing.T) {
|
|
|
|
func TestLogDuration(t *testing.T) {
|
|
|
|