package main
import (
"context"
"fmt"
"github.com/go-redis/redis"
apmgoredis "github.com/opentracing-contrib/goredis"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegerConfig "github.com/uber/jaeger-client-go/config"
"log"
"time"
)
const (
redisAddress = "127.0.0.1:6379"
redisPassword = ""
clientServerName = "redis-client-demo"
testKey = "redis-demo-key"
endPoint = "xxxxx:6831"
token = "abc"
)
func main() {
cfg := &jaegerConfig.Configuration{
ServiceName: clientServerName,
Sampler: &jaegerConfig.SamplerConfig{
Type: "const",
Param: 1,
},
Reporter: &jaegerConfig.ReporterConfig{
LogSpans: true,
LocalAgentHostPort: endPoint,
},
Tags: []opentracing.Tag{
opentracing.Tag{Key: "token", Value: token},
},
}
tracer, closer, err := cfg.NewTracer(jaegerConfig.Logger(jaeger.StdLogger))
opentracing.SetGlobalTracer(tracer)
defer closer.Close()
if err != nil {
panic(fmt.Sprintf("ERROR: fail init Jaeger: %v\\n", err))
}
InitRedisConnector()
redisClient := GetRedisDBConnector(context.Background())
redisClient.Set(testKey, "redis-client-demo", time.Duration(1000)*time.Second)
redisClient.Get(testKey)
}
var (
redisClient redis.UniversalClient
)
func GetRedisDBConnector(ctx context.Context) redis.UniversalClient {
client := apmgoredis.Wrap(redisClient).WithContext(ctx)
return client
}
func InitRedisConnector() error {
redisClient = redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{redisAddress},
Password: redisPassword,
DB: 0,
})
if err := redisClient.Ping().Err(); err != nil {
log.Println("redisClient.Ping() error:", err.Error())
return err
}
return nil
}
本页内容是否解决了您的问题?