Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
c59902201d | |||
0981f89f60 | |||
332fe5f4d4 | |||
757fe0245b | |||
27eccc1ed2 | |||
7c641fa8ac | |||
24c9f20196 | |||
953b5b0021 | |||
87e2e2b947 | |||
256e61a437 | |||
f9cdd41c94 | |||
ecad15fe17 | |||
fa3d18b353 | |||
2f3951773f | |||
b263e14032 | |||
518cc1db73 | |||
4484cd34ec | |||
7bceeee6bf | |||
aed9512b93 | |||
de72a10973 | |||
62c2de51d4 | |||
741b2310ec | |||
1e8a44b088 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.idea
|
15
go.mod
15
go.mod
@ -1,14 +1,19 @@
|
|||||||
module go.unistack.org/micro-store-redis/v3
|
module go.unistack.org/micro-store-redis/v3
|
||||||
|
|
||||||
go 1.20
|
go 1.22
|
||||||
|
|
||||||
|
toolchain go1.22.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/redis/go-redis/v9 v9.2.1
|
github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2
|
||||||
go.unistack.org/micro/v3 v3.10.28
|
github.com/redis/go-redis/v9 v9.6.2
|
||||||
|
go.unistack.org/micro/v3 v3.10.97
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
|
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
|
||||||
|
google.golang.org/protobuf v1.35.1 // indirect
|
||||||
)
|
)
|
||||||
|
24
go.sum
24
go.sum
@ -1,12 +1,20 @@
|
|||||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
|
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||||
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||||
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
|
github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2 h1:oBlErygFka9FAVfowzW7sRUfB7n31YX0aFzfaOflM3w=
|
||||||
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2/go.mod h1:jYLUE5tC8UsFnpSclhEjZlFLMMtHH4jTfFMnshNWkoo=
|
||||||
go.unistack.org/micro/v3 v3.10.28 h1:/87lGekrmi0/66pioy+Nh8lVUBBYnVqKoHiNYX5OmMI=
|
github.com/redis/go-redis/v9 v9.6.2 h1:w0uvkRbc9KpgD98zcvo5IrVUsn0lXpRMuhNgiHDJzdk=
|
||||||
go.unistack.org/micro/v3 v3.10.28/go.mod h1:eUgtvbtiiz6te93m0ZdmoecbitWwjdBmmr84srmEIKA=
|
github.com/redis/go-redis/v9 v9.6.2/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
|
||||||
|
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
||||||
|
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
||||||
|
go.unistack.org/micro/v3 v3.10.97 h1:8l7fv+i06/PjPrBBhRC/ZQkWGIOuHPg3jJN0vktYE78=
|
||||||
|
go.unistack.org/micro/v3 v3.10.97/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
||||||
|
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
||||||
|
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
|
55
options.go
55
options.go
@ -1,18 +1,67 @@
|
|||||||
package redis
|
package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/redis/go-redis/v9"
|
goredis "github.com/redis/go-redis/v9"
|
||||||
|
"go.unistack.org/micro/v3/logger"
|
||||||
|
"go.unistack.org/micro/v3/meter"
|
||||||
"go.unistack.org/micro/v3/store"
|
"go.unistack.org/micro/v3/store"
|
||||||
|
"go.unistack.org/micro/v3/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type configKey struct{}
|
type configKey struct{}
|
||||||
|
|
||||||
func Config(c *redis.Options) store.Option {
|
func Config(c *goredis.Options) store.Option {
|
||||||
return store.SetOption(configKey{}, c)
|
return store.SetOption(configKey{}, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
type clusterConfigKey struct{}
|
type clusterConfigKey struct{}
|
||||||
|
|
||||||
func ClusterConfig(c *redis.ClusterOptions) store.Option {
|
func ClusterConfig(c *goredis.ClusterOptions) store.Option {
|
||||||
return store.SetOption(clusterConfigKey{}, c)
|
return store.SetOption(clusterConfigKey{}, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type universalConfigKey struct{}
|
||||||
|
|
||||||
|
func UniversalConfig(c *goredis.UniversalOptions) store.Option {
|
||||||
|
return store.SetOption(universalConfigKey{}, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
labelHost = "redis_host"
|
||||||
|
labelName = "redis_name"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Options struct holds wrapper options
|
||||||
|
type Options struct {
|
||||||
|
Logger logger.Logger
|
||||||
|
Meter meter.Meter
|
||||||
|
Tracer tracer.Tracer
|
||||||
|
RedisHost string
|
||||||
|
RedisName string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Option func signature
|
||||||
|
type Option func(*Options)
|
||||||
|
|
||||||
|
// NewOptions create new Options struct from provided option slice
|
||||||
|
func NewOptions(opts ...Option) Options {
|
||||||
|
options := Options{
|
||||||
|
Logger: logger.DefaultLogger,
|
||||||
|
Meter: meter.DefaultMeter,
|
||||||
|
Tracer: tracer.DefaultTracer,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
options.Meter = options.Meter.Clone(
|
||||||
|
meter.Labels(
|
||||||
|
labelHost, options.RedisHost,
|
||||||
|
labelName, options.RedisName),
|
||||||
|
)
|
||||||
|
|
||||||
|
options.Logger = options.Logger.Clone(logger.WithAddCallerSkipCount(1))
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
734
redis.go
734
redis.go
@ -2,36 +2,72 @@ package redis
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
goredis "github.com/redis/go-redis/v9"
|
||||||
|
"go.unistack.org/micro/v3/semconv"
|
||||||
"go.unistack.org/micro/v3/store"
|
"go.unistack.org/micro/v3/store"
|
||||||
|
pool "go.unistack.org/micro/v3/util/xpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultPathSeparator = "/"
|
||||||
|
|
||||||
|
DefaultUniversalOptions = &goredis.UniversalOptions{
|
||||||
|
Username: "",
|
||||||
|
Password: "", // no password set
|
||||||
|
MaxRetries: 2,
|
||||||
|
MaxRetryBackoff: 256 * time.Millisecond,
|
||||||
|
DialTimeout: 1 * time.Second,
|
||||||
|
ReadTimeout: 1 * time.Second,
|
||||||
|
WriteTimeout: 1 * time.Second,
|
||||||
|
PoolTimeout: 1 * time.Second,
|
||||||
|
MinIdleConns: 10,
|
||||||
|
}
|
||||||
|
|
||||||
|
DefaultClusterOptions = &goredis.ClusterOptions{
|
||||||
|
Username: "",
|
||||||
|
Password: "", // no password set
|
||||||
|
MaxRetries: 2,
|
||||||
|
MaxRetryBackoff: 256 * time.Millisecond,
|
||||||
|
DialTimeout: 1 * time.Second,
|
||||||
|
ReadTimeout: 1 * time.Second,
|
||||||
|
WriteTimeout: 1 * time.Second,
|
||||||
|
PoolTimeout: 1 * time.Second,
|
||||||
|
MinIdleConns: 10,
|
||||||
|
}
|
||||||
|
|
||||||
|
DefaultOptions = &goredis.Options{
|
||||||
|
Username: "",
|
||||||
|
Password: "", // no password set
|
||||||
|
DB: 0, // use default DB
|
||||||
|
MaxRetries: 2,
|
||||||
|
MaxRetryBackoff: 256 * time.Millisecond,
|
||||||
|
DialTimeout: 1 * time.Second,
|
||||||
|
ReadTimeout: 1 * time.Second,
|
||||||
|
WriteTimeout: 1 * time.Second,
|
||||||
|
PoolTimeout: 1 * time.Second,
|
||||||
|
MinIdleConns: 10,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type Store struct {
|
type Store struct {
|
||||||
opts store.Options
|
opts store.Options
|
||||||
cli redisClient
|
cli goredis.UniversalClient
|
||||||
}
|
done chan struct{}
|
||||||
|
pool *pool.StringsPool
|
||||||
type redisClient interface {
|
|
||||||
Get(ctx context.Context, key string) *redis.StringCmd
|
|
||||||
Del(ctx context.Context, keys ...string) *redis.IntCmd
|
|
||||||
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
|
|
||||||
Keys(ctx context.Context, pattern string) *redis.StringSliceCmd
|
|
||||||
MGet(ctx context.Context, keys ...string) *redis.SliceCmd
|
|
||||||
MSet(ctx context.Context, kv ...interface{}) *redis.StatusCmd
|
|
||||||
Exists(ctx context.Context, keys ...string) *redis.IntCmd
|
|
||||||
Ping(ctx context.Context) *redis.StatusCmd
|
|
||||||
Pipeline() redis.Pipeliner
|
|
||||||
Pipelined(ctx context.Context, fn func(redis.Pipeliner) error) ([]redis.Cmder, error)
|
|
||||||
Close() error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Connect(ctx context.Context) error {
|
func (r *Store) Connect(ctx context.Context) error {
|
||||||
return r.cli.Ping(ctx).Err()
|
if r.cli == nil {
|
||||||
|
return store.ErrNotConnected
|
||||||
|
}
|
||||||
|
err := r.cli.Ping(ctx).Err()
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Init(opts ...store.Option) error {
|
func (r *Store) Init(opts ...store.Option) error {
|
||||||
@ -39,100 +75,192 @@ func (r *Store) Init(opts ...store.Option) error {
|
|||||||
o(&r.opts)
|
o(&r.opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.configure()
|
err := r.configure()
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Store) Redis() *redis.Client {
|
|
||||||
return r.cli.(*redis.Client)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Store) Disconnect(ctx context.Context) error {
|
|
||||||
return r.cli.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
|
||||||
if r.opts.Timeout > 0 {
|
|
||||||
var cancel context.CancelFunc
|
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
||||||
defer cancel()
|
|
||||||
}
|
|
||||||
options := store.NewExistsOptions(opts...)
|
|
||||||
if len(options.Namespace) == 0 {
|
|
||||||
options.Namespace = r.opts.Namespace
|
|
||||||
}
|
|
||||||
if options.Namespace != "" {
|
|
||||||
key = fmt.Sprintf("%s%s", r.opts.Namespace, key)
|
|
||||||
}
|
|
||||||
val, err := r.cli.Exists(ctx, key).Result()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if val == 0 {
|
|
||||||
return store.ErrNotFound
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Store) Client() *goredis.Client {
|
||||||
|
if c, ok := r.cli.(*goredis.Client); ok {
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
func (r *Store) UniversalClient() goredis.UniversalClient {
|
||||||
if r.opts.Timeout > 0 {
|
return r.cli
|
||||||
var cancel context.CancelFunc
|
}
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
||||||
defer cancel()
|
|
||||||
}
|
|
||||||
options := store.NewReadOptions(opts...)
|
|
||||||
if len(options.Namespace) == 0 {
|
|
||||||
options.Namespace = r.opts.Namespace
|
|
||||||
}
|
|
||||||
if options.Namespace != "" {
|
|
||||||
key = fmt.Sprintf("%s%s", options.Namespace, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
buf, err := r.cli.Get(ctx, key).Bytes()
|
func (r *Store) ClusterClient() *goredis.ClusterClient {
|
||||||
if err != nil && err == redis.Nil {
|
if c, ok := r.cli.(*goredis.ClusterClient); ok {
|
||||||
return store.ErrNotFound
|
return c
|
||||||
} else if err != nil {
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Store) Disconnect(ctx context.Context) error {
|
||||||
|
var err error
|
||||||
|
select {
|
||||||
|
case <-r.done:
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
if r.cli != nil {
|
||||||
|
err = r.cli.Close()
|
||||||
|
}
|
||||||
|
close(r.done)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if buf == nil {
|
}
|
||||||
return store.ErrNotFound
|
|
||||||
|
func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
||||||
|
b := r.pool.Get()
|
||||||
|
defer r.pool.Put(b)
|
||||||
|
options := store.NewExistsOptions(opts...)
|
||||||
|
labels := make([]string, 0, 6)
|
||||||
|
labels = append(labels, "name", options.Name, "statement", "exists")
|
||||||
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
}
|
}
|
||||||
return r.opts.Codec.Unmarshal(buf, val)
|
|
||||||
|
if timeout > 0 {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
||||||
|
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
||||||
|
ts := time.Now()
|
||||||
|
val, err := r.cli.Exists(ctx, rkey).Result()
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
te := time.Since(ts)
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
||||||
|
|
||||||
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||||
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||||
|
if errors.Is(err, goredis.Nil) || (err == nil && val == 0) {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
|
return store.ErrNotFound
|
||||||
|
} else if err == nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
||||||
|
} else if err != nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
||||||
|
b := r.pool.Get()
|
||||||
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
|
options := store.NewReadOptions(opts...)
|
||||||
|
labels := make([]string, 0, 6)
|
||||||
|
labels = append(labels, "name", options.Name, "statement", "read")
|
||||||
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeout > 0 {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
||||||
|
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
||||||
|
ts := time.Now()
|
||||||
|
buf, err := r.cli.Get(ctx, rkey).Bytes()
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
te := time.Since(ts)
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
||||||
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||||
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||||
|
if errors.Is(err, goredis.Nil) || (err == nil && buf == nil) {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
|
return store.ErrNotFound
|
||||||
|
} else if err == nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
||||||
|
} else if err != nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch b := val.(type) {
|
||||||
|
case *[]byte:
|
||||||
|
*b = buf
|
||||||
|
case *string:
|
||||||
|
*b = string(buf)
|
||||||
|
default:
|
||||||
|
if err = r.opts.Codec.Unmarshal(buf, val); err != nil {
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...store.ReadOption) error {
|
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...store.ReadOption) error {
|
||||||
if len(keys) == 1 {
|
options := store.NewReadOptions(opts...)
|
||||||
vt := reflect.ValueOf(vals)
|
|
||||||
if vt.Kind() == reflect.Ptr {
|
timeout := r.opts.Timeout
|
||||||
vt = reflect.Indirect(vt)
|
if options.Timeout > 0 {
|
||||||
return r.Read(ctx, keys[0], vt.Index(0).Interface(), opts...)
|
timeout = options.Timeout
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if r.opts.Timeout > 0 {
|
|
||||||
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
options := store.NewReadOptions(opts...)
|
|
||||||
rkeys := make([]string, 0, len(keys))
|
var rkeys []string
|
||||||
if len(options.Namespace) == 0 {
|
var pools []*strings.Builder
|
||||||
options.Namespace = r.opts.Namespace
|
if r.opts.Namespace != "" || options.Namespace != "" {
|
||||||
}
|
rkeys = make([]string, len(keys))
|
||||||
for _, key := range keys {
|
pools = make([]*strings.Builder, len(keys))
|
||||||
if options.Namespace != "" {
|
for idx, key := range keys {
|
||||||
rkeys = append(rkeys, fmt.Sprintf("%s%s", options.Namespace, key))
|
b := r.pool.Get()
|
||||||
} else {
|
pools[idx] = b
|
||||||
rkeys = append(rkeys, key)
|
rkeys[idx] = r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rvals, err := r.cli.MGet(ctx, rkeys...).Result()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
||||||
if err != nil && err == redis.Nil {
|
ts := time.Now()
|
||||||
return store.ErrNotFound
|
var rvals []interface{}
|
||||||
} else if err != nil {
|
var err error
|
||||||
return err
|
if r.opts.Namespace != "" || options.Namespace != "" {
|
||||||
|
rvals, err = r.cli.MGet(ctx, rkeys...).Result()
|
||||||
|
for idx := range pools {
|
||||||
|
r.pool.Put(pools[idx])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
rvals, err = r.cli.MGet(ctx, keys...).Result()
|
||||||
}
|
}
|
||||||
if len(rvals) == 0 {
|
setSpanError(ctx, err)
|
||||||
|
te := time.Since(ts)
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
||||||
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
||||||
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
||||||
|
if err == goredis.Nil || (len(rvals) == 0) {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
||||||
return store.ErrNotFound
|
return store.ErrNotFound
|
||||||
|
} else if err == nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
||||||
|
} else if err != nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
vv := reflect.ValueOf(vals)
|
vv := reflect.ValueOf(vals)
|
||||||
@ -163,77 +291,140 @@ func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts
|
|||||||
// special case for raw data
|
// special case for raw data
|
||||||
if vt.Kind() == reflect.Slice && vt.Elem().Kind() == reflect.Uint8 {
|
if vt.Kind() == reflect.Slice && vt.Elem().Kind() == reflect.Uint8 {
|
||||||
itm.Set(reflect.MakeSlice(itm.Type(), len(buf), len(buf)))
|
itm.Set(reflect.MakeSlice(itm.Type(), len(buf), len(buf)))
|
||||||
} else {
|
itm.SetBytes(buf)
|
||||||
itm.Set(reflect.New(vt.Elem()))
|
continue
|
||||||
|
} else if vt.Kind() == reflect.String {
|
||||||
|
itm.SetString(string(buf))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itm.Set(reflect.New(vt.Elem()))
|
||||||
if err = r.opts.Codec.Unmarshal(buf, itm.Interface()); err != nil {
|
if err = r.opts.Codec.Unmarshal(buf, itm.Interface()); err != nil {
|
||||||
|
setSpanError(ctx, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vv.Set(nvv)
|
vv.Set(nvv)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error {
|
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error {
|
||||||
if len(keys) == 1 {
|
options := store.NewDeleteOptions(opts...)
|
||||||
return r.Delete(ctx, keys[0], opts...)
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
}
|
}
|
||||||
if r.opts.Timeout > 0 {
|
|
||||||
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
options := store.NewDeleteOptions(opts...)
|
|
||||||
if len(options.Namespace) == 0 {
|
var rkeys []string
|
||||||
options.Namespace = r.opts.Namespace
|
var pools []*strings.Builder
|
||||||
|
if r.opts.Namespace != "" || options.Namespace != "" {
|
||||||
|
rkeys = make([]string, len(keys))
|
||||||
|
pools = make([]*strings.Builder, len(keys))
|
||||||
|
for idx, key := range keys {
|
||||||
|
b := r.pool.Get()
|
||||||
|
pools[idx] = b
|
||||||
|
rkeys[idx] = r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if options.Namespace == "" {
|
|
||||||
return r.cli.Del(ctx, keys...).Err()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
||||||
|
ts := time.Now()
|
||||||
|
var err error
|
||||||
|
if r.opts.Namespace != "" || options.Namespace != "" {
|
||||||
|
err = r.cli.Del(ctx, rkeys...).Err()
|
||||||
|
for idx := range pools {
|
||||||
|
r.pool.Put(pools[idx])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = r.cli.Del(ctx, keys...).Err()
|
||||||
}
|
}
|
||||||
for idx := range keys {
|
setSpanError(ctx, err)
|
||||||
keys[idx] = options.Namespace + keys[idx]
|
te := time.Since(ts)
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
||||||
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
||||||
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
||||||
|
if err == goredis.Nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
||||||
|
return store.ErrNotFound
|
||||||
|
} else if err == nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
||||||
|
} else if err != nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return r.cli.Del(ctx, keys...).Err()
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
||||||
if r.opts.Timeout > 0 {
|
b := r.pool.Get()
|
||||||
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
|
options := store.NewDeleteOptions(opts...)
|
||||||
|
labels := make([]string, 0, 6)
|
||||||
|
labels = append(labels, "name", options.Name, "statement", "delete")
|
||||||
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
options := store.NewDeleteOptions(opts...)
|
|
||||||
if len(options.Namespace) == 0 {
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
||||||
options.Namespace = r.opts.Namespace
|
ts := time.Now()
|
||||||
|
err := r.cli.Del(ctx, r.getKey(b, r.opts.Namespace, options.Namespace, key)).Err()
|
||||||
|
te := time.Since(ts)
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
||||||
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||||
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||||
|
if errors.Is(err, goredis.Nil) {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
|
return store.ErrNotFound
|
||||||
|
} else if err == nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
||||||
|
} else if err != nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
if options.Namespace == "" {
|
|
||||||
return r.cli.Del(ctx, key).Err()
|
return nil
|
||||||
}
|
|
||||||
return r.cli.Del(ctx, fmt.Sprintf("%s%s", options.Namespace, key)).Err()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...store.WriteOption) error {
|
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...store.WriteOption) error {
|
||||||
if len(keys) == 1 {
|
options := store.NewWriteOptions(opts...)
|
||||||
return r.Write(ctx, keys[0], vals[0], opts...)
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
}
|
}
|
||||||
if r.opts.Timeout > 0 {
|
|
||||||
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
options := store.NewWriteOptions(opts...)
|
|
||||||
kvs := make([]string, 0, len(keys)*2)
|
kvs := make([]string, 0, len(keys)*2)
|
||||||
|
pools := make([]*strings.Builder, len(keys))
|
||||||
if len(options.Namespace) == 0 {
|
|
||||||
options.Namespace = r.opts.Namespace
|
|
||||||
}
|
|
||||||
|
|
||||||
for idx, key := range keys {
|
for idx, key := range keys {
|
||||||
if options.Namespace != "" {
|
b := r.pool.Get()
|
||||||
kvs = append(kvs, fmt.Sprintf("%s%s", options.Namespace, key))
|
pools[idx] = b
|
||||||
} else {
|
kvs = append(kvs, r.getKey(b, r.opts.Namespace, options.Namespace, key))
|
||||||
kvs = append(kvs, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch vt := vals[idx].(type) {
|
switch vt := vals[idx].(type) {
|
||||||
case string:
|
case string:
|
||||||
@ -249,18 +440,47 @@ func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, o
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cmds, err := r.cli.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
||||||
|
|
||||||
|
pipeliner := func(pipe goredis.Pipeliner) error {
|
||||||
for idx := 0; idx < len(kvs); idx += 2 {
|
for idx := 0; idx < len(kvs); idx += 2 {
|
||||||
pipe.Set(ctx, kvs[idx], kvs[idx+1], options.TTL).Result()
|
if _, err := pipe.Set(ctx, kvs[idx], kvs[idx+1], options.TTL).Result(); err != nil {
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
}
|
||||||
if err != nil {
|
|
||||||
|
ts := time.Now()
|
||||||
|
cmds, err := r.cli.Pipelined(ctx, pipeliner)
|
||||||
|
for idx := range pools {
|
||||||
|
r.pool.Put(pools[idx])
|
||||||
|
}
|
||||||
|
te := time.Since(ts)
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
||||||
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
||||||
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
||||||
|
if err == goredis.Nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
||||||
|
return store.ErrNotFound
|
||||||
|
} else if err == nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
||||||
|
} else if err != nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range cmds {
|
||||||
if err = cmd.Err(); err != nil {
|
if err = cmd.Err(); err != nil {
|
||||||
|
if err == goredis.Nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
||||||
|
return store.ErrNotFound
|
||||||
|
}
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -269,16 +489,25 @@ func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, o
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
||||||
if r.opts.Timeout > 0 {
|
b := r.pool.Get()
|
||||||
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
|
options := store.NewWriteOptions(opts...)
|
||||||
|
labels := make([]string, 0, 6)
|
||||||
|
labels = append(labels, "name", options.Name, "statement", "write")
|
||||||
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
options := store.NewWriteOptions(opts...)
|
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
||||||
if len(options.Namespace) == 0 {
|
|
||||||
options.Namespace = r.opts.Namespace
|
|
||||||
}
|
|
||||||
|
|
||||||
var buf []byte
|
var buf []byte
|
||||||
switch vt := val.(type) {
|
switch vt := val.(type) {
|
||||||
@ -294,42 +523,100 @@ func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.Namespace != "" {
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
||||||
key = fmt.Sprintf("%s%s", options.Namespace, key)
|
ts := time.Now()
|
||||||
|
err := r.cli.Set(ctx, rkey, buf, options.TTL).Err()
|
||||||
|
te := time.Since(ts)
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
||||||
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||||
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||||
|
if errors.Is(err, goredis.Nil) {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
||||||
|
return store.ErrNotFound
|
||||||
|
} else if err == nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
||||||
|
} else if err != nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.cli.Set(ctx, key, buf, options.TTL).Err()
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
||||||
|
b := r.pool.Get()
|
||||||
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
options := store.NewListOptions(opts...)
|
options := store.NewListOptions(opts...)
|
||||||
if len(options.Namespace) == 0 {
|
if len(options.Namespace) == 0 {
|
||||||
options.Namespace = r.opts.Namespace
|
options.Namespace = r.opts.Namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
rkey := fmt.Sprintf("%s%s*", options.Namespace, options.Prefix)
|
rkey := r.getKey(b, options.Namespace, "", options.Prefix+"*")
|
||||||
if options.Suffix != "" {
|
if options.Suffix != "" {
|
||||||
rkey += options.Suffix
|
rkey += options.Suffix
|
||||||
}
|
}
|
||||||
if r.opts.Timeout > 0 {
|
|
||||||
|
timeout := r.opts.Timeout
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = options.Timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add support for prefix/suffix/limit
|
// TODO: add support for prefix/suffix/limit
|
||||||
keys, err := r.cli.Keys(ctx, rkey).Result()
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
||||||
if err != nil {
|
ts := time.Now()
|
||||||
|
var keys []string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if c, ok := r.cli.(*goredis.ClusterClient); ok {
|
||||||
|
err = c.ForEachMaster(ctx, func(nctx context.Context, cli *goredis.Client) error {
|
||||||
|
nkeys, nerr := cli.Keys(nctx, rkey).Result()
|
||||||
|
if nerr != nil {
|
||||||
|
return nerr
|
||||||
|
}
|
||||||
|
keys = append(keys, nkeys...)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
keys, err = r.cli.Keys(ctx, rkey).Result()
|
||||||
|
}
|
||||||
|
te := time.Since(ts)
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
||||||
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
||||||
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
||||||
|
if err == goredis.Nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
||||||
|
return nil, store.ErrNotFound
|
||||||
|
} else if err == nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
||||||
|
} else if err != nil {
|
||||||
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if options.Namespace == "" {
|
|
||||||
|
prefix := r.opts.Namespace
|
||||||
|
if options.Namespace != "" {
|
||||||
|
prefix = options.Namespace
|
||||||
|
}
|
||||||
|
if prefix == "" {
|
||||||
return keys, nil
|
return keys, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
nkeys := make([]string, 0, len(keys))
|
for idx, key := range keys {
|
||||||
for _, key := range keys {
|
keys[idx] = strings.TrimPrefix(key, prefix)
|
||||||
nkeys = append(nkeys, strings.TrimPrefix(key, options.Namespace))
|
|
||||||
}
|
}
|
||||||
return nkeys, nil
|
|
||||||
|
return keys, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Options() store.Options {
|
func (r *Store) Options() store.Options {
|
||||||
@ -345,88 +632,113 @@ func (r *Store) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewStore(opts ...store.Option) *Store {
|
func NewStore(opts ...store.Option) *Store {
|
||||||
return &Store{opts: store.NewOptions(opts...)}
|
return &Store{done: make(chan struct{}), opts: store.NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) configure() error {
|
func (r *Store) configure() error {
|
||||||
var redisOptions *redis.Options
|
|
||||||
var redisClusterOptions *redis.ClusterOptions
|
|
||||||
var err error
|
|
||||||
|
|
||||||
nodes := r.opts.Addrs
|
|
||||||
|
|
||||||
if len(nodes) == 0 {
|
|
||||||
nodes = []string{"redis://127.0.0.1:6379"}
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.cli != nil && r.opts.Context == nil {
|
if r.cli != nil && r.opts.Context == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
universalOptions := DefaultUniversalOptions
|
||||||
|
|
||||||
if r.opts.Context != nil {
|
if r.opts.Context != nil {
|
||||||
if c, ok := r.opts.Context.Value(configKey{}).(*redis.Options); ok {
|
if o, ok := r.opts.Context.Value(configKey{}).(*goredis.Options); ok {
|
||||||
redisOptions = c
|
universalOptions.Addrs = []string{o.Addr}
|
||||||
|
universalOptions.Dialer = o.Dialer
|
||||||
|
universalOptions.OnConnect = o.OnConnect
|
||||||
|
universalOptions.Username = o.Username
|
||||||
|
universalOptions.Password = o.Password
|
||||||
|
|
||||||
|
universalOptions.MaxRetries = o.MaxRetries
|
||||||
|
universalOptions.MinRetryBackoff = o.MinRetryBackoff
|
||||||
|
universalOptions.MaxRetryBackoff = o.MaxRetryBackoff
|
||||||
|
|
||||||
|
universalOptions.DialTimeout = o.DialTimeout
|
||||||
|
universalOptions.ReadTimeout = o.ReadTimeout
|
||||||
|
universalOptions.WriteTimeout = o.WriteTimeout
|
||||||
|
universalOptions.ContextTimeoutEnabled = o.ContextTimeoutEnabled
|
||||||
|
|
||||||
|
universalOptions.PoolFIFO = o.PoolFIFO
|
||||||
|
|
||||||
|
universalOptions.PoolSize = o.PoolSize
|
||||||
|
universalOptions.PoolTimeout = o.PoolTimeout
|
||||||
|
universalOptions.MinIdleConns = o.MinIdleConns
|
||||||
|
universalOptions.MaxIdleConns = o.MaxIdleConns
|
||||||
|
universalOptions.ConnMaxIdleTime = o.ConnMaxIdleTime
|
||||||
|
universalOptions.ConnMaxLifetime = o.ConnMaxLifetime
|
||||||
|
|
||||||
if r.opts.TLSConfig != nil {
|
if r.opts.TLSConfig != nil {
|
||||||
redisOptions.TLSConfig = r.opts.TLSConfig
|
universalOptions.TLSConfig = r.opts.TLSConfig
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c, ok := r.opts.Context.Value(clusterConfigKey{}).(*redis.ClusterOptions); ok {
|
if o, ok := r.opts.Context.Value(clusterConfigKey{}).(*goredis.ClusterOptions); ok {
|
||||||
redisClusterOptions = c
|
universalOptions.Addrs = o.Addrs
|
||||||
|
universalOptions.Dialer = o.Dialer
|
||||||
|
universalOptions.OnConnect = o.OnConnect
|
||||||
|
universalOptions.Username = o.Username
|
||||||
|
universalOptions.Password = o.Password
|
||||||
|
|
||||||
|
universalOptions.MaxRedirects = o.MaxRedirects
|
||||||
|
universalOptions.ReadOnly = o.ReadOnly
|
||||||
|
universalOptions.RouteByLatency = o.RouteByLatency
|
||||||
|
universalOptions.RouteRandomly = o.RouteRandomly
|
||||||
|
|
||||||
|
universalOptions.MaxRetries = o.MaxRetries
|
||||||
|
universalOptions.MinRetryBackoff = o.MinRetryBackoff
|
||||||
|
universalOptions.MaxRetryBackoff = o.MaxRetryBackoff
|
||||||
|
|
||||||
|
universalOptions.DialTimeout = o.DialTimeout
|
||||||
|
universalOptions.ReadTimeout = o.ReadTimeout
|
||||||
|
universalOptions.WriteTimeout = o.WriteTimeout
|
||||||
|
universalOptions.ContextTimeoutEnabled = o.ContextTimeoutEnabled
|
||||||
|
|
||||||
|
universalOptions.PoolFIFO = o.PoolFIFO
|
||||||
|
|
||||||
|
universalOptions.PoolSize = o.PoolSize
|
||||||
|
universalOptions.PoolTimeout = o.PoolTimeout
|
||||||
|
universalOptions.MinIdleConns = o.MinIdleConns
|
||||||
|
universalOptions.MaxIdleConns = o.MaxIdleConns
|
||||||
|
universalOptions.ConnMaxIdleTime = o.ConnMaxIdleTime
|
||||||
|
universalOptions.ConnMaxLifetime = o.ConnMaxLifetime
|
||||||
if r.opts.TLSConfig != nil {
|
if r.opts.TLSConfig != nil {
|
||||||
redisClusterOptions.TLSConfig = r.opts.TLSConfig
|
universalOptions.TLSConfig = r.opts.TLSConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if o, ok := r.opts.Context.Value(universalConfigKey{}).(*goredis.UniversalOptions); ok {
|
||||||
|
universalOptions = o
|
||||||
|
if r.opts.TLSConfig != nil {
|
||||||
|
universalOptions.TLSConfig = r.opts.TLSConfig
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if redisOptions != nil && redisClusterOptions != nil {
|
if len(r.opts.Addrs) > 0 {
|
||||||
return fmt.Errorf("must specify only one option Config or ClusterConfig")
|
universalOptions.Addrs = r.opts.Addrs
|
||||||
|
} else {
|
||||||
|
universalOptions.Addrs = []string{"127.0.0.1:6379"}
|
||||||
}
|
}
|
||||||
|
|
||||||
if redisOptions == nil && redisClusterOptions == nil && r.cli != nil {
|
r.cli = goredis.NewUniversalClient(universalOptions)
|
||||||
return nil
|
setTracing(r.cli, r.opts.Tracer)
|
||||||
}
|
|
||||||
|
|
||||||
if redisOptions == nil && redisClusterOptions == nil && len(nodes) == 1 {
|
r.pool = pool.NewStringsPool(50)
|
||||||
redisOptions, err = redis.ParseURL(nodes[0])
|
|
||||||
if err != nil {
|
|
||||||
// Backwards compatibility
|
|
||||||
redisOptions = &redis.Options{
|
|
||||||
Addr: nodes[0],
|
|
||||||
Username: "",
|
|
||||||
Password: "", // no password set
|
|
||||||
DB: 0, // use default DB
|
|
||||||
MaxRetries: 2,
|
|
||||||
MaxRetryBackoff: 256 * time.Millisecond,
|
|
||||||
DialTimeout: 1 * time.Second,
|
|
||||||
ReadTimeout: 1 * time.Second,
|
|
||||||
WriteTimeout: 1 * time.Second,
|
|
||||||
PoolTimeout: 1 * time.Second,
|
|
||||||
MinIdleConns: 10,
|
|
||||||
TLSConfig: r.opts.TLSConfig,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if redisOptions == nil && redisClusterOptions == nil && len(nodes) > 1 {
|
|
||||||
redisClusterOptions = &redis.ClusterOptions{
|
|
||||||
Addrs: nodes,
|
|
||||||
Username: "",
|
|
||||||
Password: "", // no password set
|
|
||||||
MaxRetries: 2,
|
|
||||||
MaxRetryBackoff: 256 * time.Millisecond,
|
|
||||||
DialTimeout: 1 * time.Second,
|
|
||||||
ReadTimeout: 1 * time.Second,
|
|
||||||
WriteTimeout: 1 * time.Second,
|
|
||||||
PoolTimeout: 1 * time.Second,
|
|
||||||
MinIdleConns: 10,
|
|
||||||
TLSConfig: r.opts.TLSConfig,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if redisOptions != nil {
|
r.statsMeter()
|
||||||
r.cli = redis.NewClient(redisOptions)
|
|
||||||
} else if redisClusterOptions != nil {
|
|
||||||
r.cli = redis.NewClusterClient(redisClusterOptions)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Store) getKey(b *strings.Builder, mainNamespace string, opNamespace string, key string) string {
|
||||||
|
if opNamespace == "" {
|
||||||
|
opNamespace = mainNamespace
|
||||||
|
}
|
||||||
|
if opNamespace != "" {
|
||||||
|
b.WriteString(opNamespace)
|
||||||
|
b.WriteString(DefaultPathSeparator)
|
||||||
|
}
|
||||||
|
b.WriteString(key)
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
@ -7,14 +7,42 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
goredis "github.com/redis/go-redis/v9"
|
||||||
"go.unistack.org/micro/v3/store"
|
"go.unistack.org/micro/v3/store"
|
||||||
|
"go.unistack.org/micro/v3/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestKeepTTL(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
||||||
|
t.Skip()
|
||||||
|
}
|
||||||
|
r := NewStore(store.Addrs(os.Getenv("STORE_NODES")))
|
||||||
|
|
||||||
|
if err := r.Init(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := r.Connect(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
key := "key"
|
||||||
|
err := r.Write(ctx, key, "val1", store.WriteTTL(15*time.Second))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Write error: %v", err)
|
||||||
|
}
|
||||||
|
time.Sleep(3 * time.Second)
|
||||||
|
err = r.Write(ctx, key, "val2", store.WriteTTL(-1))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Write error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Test_rkv_configure(t *testing.T) {
|
func Test_rkv_configure(t *testing.T) {
|
||||||
type fields struct {
|
type fields struct {
|
||||||
options store.Options
|
options store.Options
|
||||||
Client *redis.Client
|
Client goredis.UniversalClient
|
||||||
}
|
}
|
||||||
type wantValues struct {
|
type wantValues struct {
|
||||||
username string
|
username string
|
||||||
@ -37,7 +65,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "legacy Url", fields: fields{options: store.Options{Addrs: []string{"127.0.0.1:6379"}}, Client: nil},
|
name: "legacy Url", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"127.0.0.1:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "",
|
username: "",
|
||||||
password: "",
|
password: "",
|
||||||
@ -45,7 +73,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "New Url", fields: fields{options: store.Options{Addrs: []string{"redis://127.0.0.1:6379"}}, Client: nil},
|
name: "New Url", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"redis://127.0.0.1:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "",
|
username: "",
|
||||||
password: "",
|
password: "",
|
||||||
@ -53,7 +81,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Url with Pwd", fields: fields{options: store.Options{Addrs: []string{"redis://:password@redis:6379"}}, Client: nil},
|
name: "Url with Pwd", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"redis://:password@redis:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "",
|
username: "",
|
||||||
password: "password",
|
password: "password",
|
||||||
@ -61,7 +89,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Url with username and Pwd", fields: fields{options: store.Options{Addrs: []string{"redis://username:password@redis:6379"}}, Client: nil},
|
name: "Url with username and Pwd", fields: fields{options: store.Options{Tracer: tracer.DefaultTracer, Addrs: []string{"redis://username:password@redis:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "username",
|
username: "username",
|
||||||
password: "password",
|
password: "password",
|
||||||
|
49
stats.go
Normal file
49
stats.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
goredis "github.com/redis/go-redis/v9"
|
||||||
|
"go.unistack.org/micro/v3/meter"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
PoolHitsTotal = "pool_hits_total"
|
||||||
|
PoolMissesTotal = "pool_misses_total"
|
||||||
|
PoolTimeoutTotal = "pool_timeout_total"
|
||||||
|
PoolConnTotalCurrent = "pool_conn_total_current"
|
||||||
|
PoolConnIdleCurrent = "pool_conn_idle_current"
|
||||||
|
PoolConnStaleTotal = "pool_conn_stale_total"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Statser interface {
|
||||||
|
PoolStats() *goredis.PoolStats
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Store) statsMeter() {
|
||||||
|
var st Statser
|
||||||
|
|
||||||
|
if r.cli != nil {
|
||||||
|
st = r.cli
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(meter.DefaultMeterStatsInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for _ = range ticker.C {
|
||||||
|
if st == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stats := st.PoolStats()
|
||||||
|
r.opts.Meter.Counter(PoolHitsTotal).Set(uint64(stats.Hits))
|
||||||
|
r.opts.Meter.Counter(PoolMissesTotal).Set(uint64(stats.Misses))
|
||||||
|
r.opts.Meter.Counter(PoolTimeoutTotal).Set(uint64(stats.Timeouts))
|
||||||
|
r.opts.Meter.Counter(PoolConnTotalCurrent).Set(uint64(stats.TotalConns))
|
||||||
|
r.opts.Meter.Counter(PoolConnIdleCurrent).Set(uint64(stats.IdleConns))
|
||||||
|
r.opts.Meter.Counter(PoolConnStaleTotal).Set(uint64(stats.StaleConns))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
128
tracer.go
Normal file
128
tracer.go
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
rediscmd "github.com/redis/go-redis/extra/rediscmd/v9"
|
||||||
|
goredis "github.com/redis/go-redis/v9"
|
||||||
|
"go.unistack.org/micro/v3/tracer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setTracing(rdb goredis.UniversalClient, tr tracer.Tracer, opts ...tracer.SpanOption) {
|
||||||
|
switch rdb := rdb.(type) {
|
||||||
|
case *goredis.Client:
|
||||||
|
opt := rdb.Options()
|
||||||
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
||||||
|
rdb.AddHook(newTracingHook(connString, tr))
|
||||||
|
case *goredis.ClusterClient:
|
||||||
|
rdb.OnNewNode(func(rdb *goredis.Client) {
|
||||||
|
opt := rdb.Options()
|
||||||
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
||||||
|
rdb.AddHook(newTracingHook(connString, tr))
|
||||||
|
})
|
||||||
|
case *goredis.Ring:
|
||||||
|
rdb.OnNewNode(func(rdb *goredis.Client) {
|
||||||
|
opt := rdb.Options()
|
||||||
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
||||||
|
rdb.AddHook(newTracingHook(connString, tr))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type tracingHook struct {
|
||||||
|
tr tracer.Tracer
|
||||||
|
opts []tracer.SpanOption
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ goredis.Hook = (*tracingHook)(nil)
|
||||||
|
|
||||||
|
func newTracingHook(connString string, tr tracer.Tracer, opts ...tracer.SpanOption) *tracingHook {
|
||||||
|
opts = append(opts, tracer.WithSpanKind(tracer.SpanKindClient))
|
||||||
|
if connString != "" {
|
||||||
|
opts = append(opts, tracer.WithSpanLabels("db.connection_string", connString))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &tracingHook{
|
||||||
|
tr: tr,
|
||||||
|
opts: opts,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *tracingHook) DialHook(hook goredis.DialHook) goredis.DialHook {
|
||||||
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
/*
|
||||||
|
_, span := h.tr.Start(ctx, "goredis.dial", h.opts...)
|
||||||
|
defer span.Finish()
|
||||||
|
*/
|
||||||
|
conn, err := hook(ctx, network, addr)
|
||||||
|
// recordError(span, err)
|
||||||
|
|
||||||
|
return conn, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *tracingHook) ProcessHook(hook goredis.ProcessHook) goredis.ProcessHook {
|
||||||
|
return func(ctx context.Context, cmd goredis.Cmder) error {
|
||||||
|
cmdString := rediscmd.CmdString(cmd)
|
||||||
|
var err error
|
||||||
|
|
||||||
|
switch cmdString {
|
||||||
|
case "cluster slots":
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
_, span := h.tr.Start(ctx, "goredis.process", append(h.opts, tracer.WithSpanLabels("db.statement", cmdString))...)
|
||||||
|
defer func() {
|
||||||
|
recordError(span, err)
|
||||||
|
span.Finish()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = hook(ctx, cmd)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *tracingHook) ProcessPipelineHook(hook goredis.ProcessPipelineHook) goredis.ProcessPipelineHook {
|
||||||
|
return func(ctx context.Context, cmds []goredis.Cmder) error {
|
||||||
|
_, cmdsString := rediscmd.CmdsString(cmds)
|
||||||
|
|
||||||
|
opts := append(h.opts, tracer.WithSpanLabels(
|
||||||
|
"db.goredis.num_cmd", strconv.Itoa(len(cmds)),
|
||||||
|
"db.statement", cmdsString,
|
||||||
|
))
|
||||||
|
|
||||||
|
_, span := h.tr.Start(ctx, "goredis.process_pipeline", opts...)
|
||||||
|
defer span.Finish()
|
||||||
|
|
||||||
|
err := hook(ctx, cmds)
|
||||||
|
recordError(span, err)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func setSpanError(ctx context.Context, err error) {
|
||||||
|
if err == nil || err == goredis.Nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if sp, ok := tracer.SpanFromContext(ctx); !ok && sp != nil {
|
||||||
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func recordError(span tracer.Span, err error) {
|
||||||
|
if err != nil && err != goredis.Nil {
|
||||||
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatDBConnString(network, addr string) string {
|
||||||
|
if network == "tcp" {
|
||||||
|
network = "redis"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s://%s", network, addr)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user