Skip to content

Commit

Permalink
feat: redis record traceid without open trace (#535)
Browse files Browse the repository at this point in the history
Co-authored-by: papa-hexuan <hexuan@douyu.tv>
  • Loading branch information
papa-hexuan and papa-hexuan authored Nov 1, 2022
1 parent dc0f10b commit 1944d57
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 26 deletions.
2 changes: 0 additions & 2 deletions pkg/client/redis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ type Config struct {
// Should be less than server's timeout.
// Default is 5 minutes. -1 disables idle timeout check.
IdleTimeout time.Duration `json:"idleTimeout" toml:"idleTimeout"`
// Enables read-only commands on slave nodes.
ReadOnly bool

/****** for jupiter ******/
ReadOnMaster bool `json:"readOnMaster" toml:"readOnMaster"`
Expand Down
18 changes: 7 additions & 11 deletions pkg/client/redis/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/fatih/color"
"github.com/go-redis/redis/v8"
"github.com/spf13/cast"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
Expand Down Expand Up @@ -194,11 +193,9 @@ func accessInterceptor(compName string, addr string, config *Config, logger *xlo
xlog.Any("req", cmd.Args()),
xlog.FieldCost(cost))

// 开启了链路,那么就记录链路id
if config.EnableTraceInterceptor && otel.GetTracerProvider() != nil {
if traceId := xlog.GetTraceID(ctx); len(traceId) > 0 {
fields = append(fields, xlog.String("trace_id", traceId))
}
// 记录链路id
if traceId := xlog.GetTraceID(ctx); len(traceId) > 0 {
fields = append(fields, xlog.String("trace_id", traceId))
}
// error
if err != nil {
Expand Down Expand Up @@ -227,12 +224,11 @@ func accessInterceptor(compName string, addr string, config *Config, logger *xlo
xlog.Any("req", cmd.Args()),
xlog.FieldCost(cost))

// 开启了链路,那么就记录链路id
if config.EnableTraceInterceptor && otel.GetTracerProvider() != nil {
if traceId := xlog.GetTraceID(ctx); len(traceId) > 0 {
fields = append(fields, xlog.String("trace_id", traceId))
}
// 记录链路id
if traceId := xlog.GetTraceID(ctx); len(traceId) > 0 {
fields = append(fields, xlog.String("trace_id", traceId))
}

// error
if err != nil {
fields = append(fields, xlog.FieldErr(err))
Expand Down
15 changes: 3 additions & 12 deletions pkg/client/redis/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package redis

import (
"context"
"fmt"
"testing"
"time"

"github.com/go-redis/redis/v8"
"go.opentelemetry.io/otel/trace"
"github.com/douyu/jupiter/pkg/xlog"

"github.com/douyu/jupiter/pkg/core/xtrace"
"github.com/douyu/jupiter/pkg/core/xtrace/jaeger"
"github.com/go-redis/redis/v8"
)

func Test_Interceptor(t *testing.T) {
Expand Down Expand Up @@ -55,15 +54,7 @@ func Test_Interceptor(t *testing.T) {
})

t.Run("access", func(t *testing.T) {
xtrace.SetGlobalTracer((&jaeger.Config{
Name: "trace",
Endpoint: "localhost:6831",
Sampler: 1,
}).Build())

ctx, span := xtrace.NewTracer(trace.SpanKindServer).Start(context.Background(), "test", nil)
fmt.Println(span.SpanContext().TraceID())

ctx := xlog.SetTraceID(context.Background(), "123456")
config.EnableAccessLogInterceptor = true
client, _ := config.Build()

Expand Down
2 changes: 1 addition & 1 deletion website/docs/jupiter/6.8clientredis.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ addr是dns连接包含用户名密码
`redis://127.0.0.2:6379`
- 无用户名有密码写法
`redis://:password@127.0.0.2:6379` password前的`:`不可缺省
- 有用户名有密码写法
- 有用户名有密码写法
`redis://username:password@127.0.0.2:6379`

### RedisClusterConfig
Expand Down

0 comments on commit 1944d57

Please sign in to comment.