Skip to content

Commit

Permalink
fix review 2
Browse files Browse the repository at this point in the history
* add interface validation at compile time
* remove useless helper func

Signed-off-by: sacha-froment-ext <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Sep 29, 2020
1 parent beb0ad0 commit 4ba99db
Showing 1 changed file with 4 additions and 22 deletions.
26 changes: 4 additions & 22 deletions contrib/go-redis/redis.v8/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type datadogHook struct {
*params
}

var _ redis.Hook = (*datadogHook)(nil)

// params holds the tracer and a set of parameters which are recorded with every trace.
type params struct {
host string
Expand Down Expand Up @@ -60,7 +62,7 @@ func NewClient(opt *redis.Options, opts ...ClientOption) redis.UniversalClient {
}

func (ddh *datadogHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
raw := cmderToString(cmd)
raw := cmd.String()
parts := strings.Split(raw, " ")
length := len(parts) - 1
p := ddh.params
Expand Down Expand Up @@ -135,28 +137,8 @@ func (ddh *datadogHook) AfterProcessPipeline(ctx context.Context, cmds []redis.C
func commandsToString(cmds []redis.Cmder) string {
var b bytes.Buffer
for _, cmd := range cmds {
b.WriteString(cmderToString(cmd))
b.WriteString(cmd.String())
b.WriteString("\n")
}
return b.String()
}

func cmderToString(cmd redis.Cmder) string {
// We want to support multiple versions of the go-redis library. In
// older versions Cmder implements the Stringer interface, while in
// newer versions that was removed, and this String method which
// sometimes returns an error is used instead. By doing a type assertion
// we can support both versions.
if stringer, ok := cmd.(fmt.Stringer); ok {
return stringer.String()
}

args := cmd.Args()
if len(args) == 0 {
return ""
}
if str, ok := args[0].(string); ok {
return str
}
return ""
}

0 comments on commit 4ba99db

Please sign in to comment.