Эх сурвалжийг харах

chore: refactor (#2545)

* chore: refactor

* chore: refactor
Kevin Wan 2 жил өмнө
parent
commit
9cadab2684

+ 1 - 1
core/stores/redis/hook.go

@@ -149,7 +149,7 @@ func logDuration(ctx context.Context, cmds []red.Cmder, duration time.Duration)
 			}
 			}
 			build.WriteString(mapping.Repr(arg))
 			build.WriteString(mapping.Repr(arg))
 		}
 		}
-		buf.WriteString(mapping.Repr(build.String()))
+		buf.WriteString(build.String())
 	}
 	}
 	logx.WithContext(ctx).WithDuration(duration).Slowf("[REDIS] slowcall on executing: %s", buf.String())
 	logx.WithContext(ctx).WithDuration(duration).Slowf("[REDIS] slowcall on executing: %s", buf.String())
 }
 }

+ 19 - 12
core/stores/redis/hook_test.go

@@ -91,10 +91,10 @@ func TestHookProcessPipelineCase1(t *testing.T) {
 	log.SetOutput(&buf)
 	log.SetOutput(&buf)
 	defer log.SetOutput(writer)
 	defer log.SetOutput(writer)
 
 
-	ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{red.NewCmd(context.Background())})
-	if err != nil {
-		t.Fatal(err)
-	}
+	ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{
+		red.NewCmd(context.Background()),
+	})
+	assert.NoError(t, err)
 	assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
 	assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
 
 
 	assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
 	assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
@@ -115,10 +115,10 @@ func TestHookProcessPipelineCase2(t *testing.T) {
 	w, restore := injectLog()
 	w, restore := injectLog()
 	defer restore()
 	defer restore()
 
 
-	ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{red.NewCmd(context.Background())})
-	if err != nil {
-		t.Fatal(err)
-	}
+	ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{
+		red.NewCmd(context.Background()),
+	})
+	assert.NoError(t, err)
 	assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
 	assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
 
 
 	time.Sleep(slowThreshold.Load() + time.Millisecond)
 	time.Sleep(slowThreshold.Load() + time.Millisecond)
@@ -159,7 +159,9 @@ func TestHookProcessPipelineCase5(t *testing.T) {
 	defer log.SetOutput(writer)
 	defer log.SetOutput(writer)
 
 
 	ctx := context.WithValue(context.Background(), startTimeKey, "foo")
 	ctx := context.WithValue(context.Background(), startTimeKey, "foo")
-	assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{red.NewCmd(context.Background())}))
+	assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
+		red.NewCmd(context.Background()),
+	}))
 	assert.True(t, buf.Len() == 0)
 	assert.True(t, buf.Len() == 0)
 }
 }
 
 
@@ -167,11 +169,16 @@ func TestLogDuration(t *testing.T) {
 	w, restore := injectLog()
 	w, restore := injectLog()
 	defer restore()
 	defer restore()
 
 
-	logDuration(context.Background(), []red.Cmder{red.NewCmd(context.Background(), "get", "foo")}, 1*time.Second)
+	logDuration(context.Background(), []red.Cmder{
+		red.NewCmd(context.Background(), "get", "foo"),
+	}, 1*time.Second)
 	assert.True(t, strings.Contains(w.String(), "get foo"))
 	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)
-	assert.True(t, strings.Contains(w.String(), "get foo\\nset bar 0"))
+	logDuration(context.Background(), []red.Cmder{
+		red.NewCmd(context.Background(), "get", "foo"),
+		red.NewCmd(context.Background(), "set", "bar", 0),
+	}, 1*time.Second)
+	assert.True(t, strings.Contains(w.String(), `get foo\nset bar 0`))
 }
 }
 
 
 func injectLog() (r *strings.Builder, restore func()) {
 func injectLog() (r *strings.Builder, restore func()) {

+ 5 - 6
rest/handler/tracinghandler.go

@@ -12,11 +12,11 @@ import (
 	oteltrace "go.opentelemetry.io/otel/trace"
 	oteltrace "go.opentelemetry.io/otel/trace"
 )
 )
 
 
-var dontTracingSpanNames sync.Map
+var notTracingSpans sync.Map
 
 
-// DontTracingSpanName disable tracing for the specified spanName.
-func DontTracingSpanName(spanName string) {
-	dontTracingSpanNames.Store(spanName, lang.Placeholder)
+// DontTraceSpan disable tracing for the specified span name.
+func DontTraceSpan(spanName string) {
+	notTracingSpans.Store(spanName, lang.Placeholder)
 }
 }
 
 
 // TracingHandler return a middleware that process the opentelemetry.
 // TracingHandler return a middleware that process the opentelemetry.
@@ -36,8 +36,7 @@ func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
 				spanName = r.URL.Path
 				spanName = r.URL.Path
 			}
 			}
 
 
-			_, ok := dontTracingSpanNames.Load(spanName)
-			if ok {
+			if _, ok := notTracingSpans.Load(spanName); ok {
 				return
 				return
 			}
 			}
 
 

+ 1 - 1
rest/handler/tracinghandler_test.go

@@ -60,7 +60,7 @@ func TestDontTracingSpanName(t *testing.T) {
 		Sampler:  1.0,
 		Sampler:  1.0,
 	})
 	})
 
 
-	DontTracingSpanName("bar")
+	DontTraceSpan("bar")
 
 
 	for _, test := range []string{"", "bar", "foo"} {
 	for _, test := range []string{"", "bar", "foo"} {
 		t.Run(test, func(t *testing.T) {
 		t.Run(test, func(t *testing.T) {