Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redis's pipeline logs are not printed completely #2538

Merged
merged 2 commits into from Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions core/stores/redis/hook.go
Expand Up @@ -56,7 +56,7 @@ func (h hook) AfterProcess(ctx context.Context, cmd red.Cmder) error {

duration := timex.Since(start)
if duration > slowThreshold.Load() {
logDuration(ctx, cmd, duration)
logDuration(ctx, []red.Cmder{cmd}, duration)
}

metricReqDur.Observe(int64(duration/time.Millisecond), cmd.Name())
Expand Down Expand Up @@ -103,7 +103,7 @@ func (h hook) AfterProcessPipeline(ctx context.Context, cmds []red.Cmder) error

duration := timex.Since(start)
if duration > slowThreshold.Load()*time.Duration(len(cmds)) {
logDuration(ctx, cmds[0], duration)
logDuration(ctx, cmds, duration)
}

metricReqDur.Observe(int64(duration/time.Millisecond), "Pipeline")
Expand Down Expand Up @@ -136,13 +136,20 @@ func formatError(err error) string {
}
}

func logDuration(ctx context.Context, cmd red.Cmder, duration time.Duration) {
func logDuration(ctx context.Context, cmds []red.Cmder, duration time.Duration) {
var buf strings.Builder
for i, arg := range cmd.Args() {
if i > 0 {
buf.WriteByte(' ')
for k, cmd := range cmds {
if k > 0 {
buf.WriteByte('\n')
}
buf.WriteString(mapping.Repr(arg))
var build strings.Builder
for i, arg := range cmd.Args() {
if i > 0 {
build.WriteByte(' ')
}
build.WriteString(mapping.Repr(arg))
}
buf.WriteString(mapping.Repr(build.String()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already string, no need to use mapping.Repr?

}
logx.WithContext(ctx).WithDuration(duration).Slowf("[REDIS] slowcall on executing: %s", buf.String())
}
Expand Down
11 changes: 11 additions & 0 deletions core/stores/redis/hook_test.go
Expand Up @@ -163,6 +163,17 @@ func TestHookProcessPipelineCase5(t *testing.T) {
assert.True(t, buf.Len() == 0)
}

func TestLogDuration(t *testing.T) {
w, restore := injectLog()
defer restore()

logDuration(context.Background(), []red.Cmder{red.NewCmd(context.Background(), "get", "foo")}, 1*time.Second)
assert.True(t, strings.Contains(w.String(), "get foo"))

logDuration(context.Background(), []red.Cmder{red.NewCmd(context.Background(), "get", "foo"), red.NewCmd(context.Background(), "set", "bar", 0)}, 1*time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line is too long.

assert.True(t, strings.Contains(w.String(), "get foo\\nset bar 0"))
}

func injectLog() (r *strings.Builder, restore func()) {
var buf strings.Builder
w := logx.NewWriter(&buf)
Expand Down