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

feat: redis record traceid without open trace #535

Merged
merged 3 commits into from
Nov 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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