Skip to content

redis dialer and hook #2172

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
82 changes: 81 additions & 1 deletion net/redisclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package net
import (
"context"
"fmt"
"log"
"net"
"sync"
"time"

"github.com/cenkalti/backoff"
"github.com/go-redis/redis/v9"
"github.com/opentracing/opentracing-go"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/logging"
"github.com/zalando/skipper/metrics"

Expand Down Expand Up @@ -197,6 +198,85 @@ func NewRendezvousVnodes(shards []string) redis.ConsistentHash {
return rendezvousVnodes{rendezvous.New(vshards, xxhash.Sum64String), table}
}

type RedisDialer struct {
d *net.Dialer
}

func NewRedisDialer(d *net.Dialer) *RedisDialer {
return &RedisDialer{
d: d,
}
}

func foo() {
NewRedisDialer(&net.Dialer{
Timeout: 250 * time.Millisecond,
KeepAlive: 30 * time.Second,
DualStack: false,
})
}

func (rd *RedisDialer) Dialer(ctx context.Context, network, addr string) (net.Conn, error) {
return rd.DialContext(ctx, network, addr)
}

func (rd *RedisDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
span := opentracing.SpanFromContext(ctx)
if span != nil {
span.LogKV("dial_context", "start")
}

conn, err := rd.d.DialContext(ctx, network, addr)

if span != nil {
span.LogKV("dial_context", "done")
if err != nil {
span.SetTag("error", true)
span.LogKV("event", "error", "message", err.Error())
}

}
log.Errorf("Failed to Dial: %v", err)

return conn, err
}

func (rd *RedisDialer) OnConnect(ctx context.Context, cn *redis.Conn) error {
return nil
}

type RedisHook struct {
tracer *opentracing.Tracer
}

func (rh *RedisHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
now := time.Now()
c := context.WithValue(ctx, "start", now)
return c, nil
}

func (rh *RedisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
var ts time.Time
var ok bool

ts, ok = ctx.Value("start").(time.Time)
if !ok {
return nil
}
d := time.Now().Sub(ts)
span := opentracing.SpanFromContext(ctx)
span.LogKV("command processing took", d.String())
return nil
}

func (rh *RedisHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
return ctx, nil
}

func (rh *RedisHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error {
return nil
}

func NewRedisRingClient(ro *RedisOptions) *RedisRingClient {
r := &RedisRingClient{
once: sync.Once{},
Expand Down
3 changes: 2 additions & 1 deletion ratelimit/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ func (c *clusterLimitRedis) AllowContext(ctx context.Context, clearText string)
defer span.Finish()
}
c.setCommonTags(span)
spanCtx := context.WithValue(ctx, "span", span)

allow, err := c.allow(ctx, clearText)
allow, err := c.allow(spanCtx, clearText)
failed := err != nil
if failed {
allow = !c.failClosed
Expand Down