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): add opentelemetry instrumentation #131

Closed
wants to merge 2 commits into from
Closed
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: 1 addition & 1 deletion redis/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ require (
github.com/go-redis/redis v6.15.9+incompatible
github.com/philippgille/gokv/encoding v0.6.0
github.com/philippgille/gokv/test v0.6.0
github.com/philippgille/gokv/util v0.6.0
)

require (
github.com/go-test/deep v1.0.4 // indirect
github.com/onsi/ginkgo v1.10.2 // indirect
github.com/onsi/gomega v1.7.0 // indirect
github.com/philippgille/gokv v0.6.0 // indirect
golang.org/x/sys v0.8.0 // indirect
)
5 changes: 2 additions & 3 deletions redis/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ github.com/philippgille/gokv/encoding v0.6.0 h1:P1TN+Aulpd6Qd7qcLqgPwoxzOQ42UHBX
github.com/philippgille/gokv/encoding v0.6.0/go.mod h1:/yKvq2BKJlKJsH7KMDrhDlEw2Pt3V1nKyFhs4iOqz5U=
github.com/philippgille/gokv/test v0.6.0 h1:pe4HsmywhKSNFlrtkFMnSP4K4wHlVQPUhYSvEzde7ag=
github.com/philippgille/gokv/test v0.6.0/go.mod h1:yawxKr4W1Qk0RqvZwiZSs6QYrXTV3iJHt43IpH8k7AI=
github.com/philippgille/gokv/util v0.6.0 h1:GrTxVENzKBxs8lB3tnaA88mKOuVPT7atZPplxX+PNmo=
github.com/philippgille/gokv/util v0.6.0/go.mod h1:ovoDHZ2Svr7YX972SPPJQRXbhHEy3Gb20HRH/Tr9BiQ=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
43 changes: 38 additions & 5 deletions redis/redis.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package redis

import (
"github.com/go-redis/redis"
"context"
"time"

"github.com/redis/go-redis/extra/redisotel/v9"
"github.com/redis/go-redis/v9"
Comment on lines +7 to +8
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With these import lines the go.mod is out of sync and when you run go get ./... or go mod tidy this will add the v9 to the go.mod file and effectively include the dependency update in this PR.

Your plan was probably to have the other separate update PR merged first and then this one, but this PR's second commit contains a removal of the dependency lines from the go.mod.

I would suggest to treat this PR as a "stacked PR" building entirely on top of the other one, without removing anything from the other one. For any new commit you might add onto the other, you can rebase this branch on the other to include all the same commits. Then when the other PR is merged, this PR's diff should be automatically changed to be only the relevant ones (either out of the box, or after squashing the commits that are originally from the other branch).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense, thanks


"github.com/philippgille/gokv/encoding"
"github.com/philippgille/gokv/util"
Expand Down Expand Up @@ -30,7 +34,10 @@ func (c Client) Set(k string, v interface{}) error {
return err
}

err = c.c.Set(k, string(data), 0).Err()
tctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

err = c.c.Set(tctx, k, string(data), 0).Err()
if err != nil {
return err
}
Expand All @@ -48,7 +55,10 @@ func (c Client) Get(k string, v interface{}) (found bool, err error) {
return false, err
}

dataString, err := c.c.Get(k).Result()
tctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

dataString, err := c.c.Get(tctx, k).Result()
if err != nil {
if err == redis.Nil {
return false, nil
Expand All @@ -67,7 +77,10 @@ func (c Client) Delete(k string) error {
return err
}

_, err := c.c.Del(k).Result()
tctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

_, err := c.c.Del(tctx, k).Result()
return err
}

Expand All @@ -91,6 +104,12 @@ type Options struct {
// Encoding format.
// Optional (encoding.JSON by default).
Codec encoding.Codec
// Redis opentelemtry Tracing
// Optional (false by default).
EnableTracing bool
// Redis opentelemtry Metrics
// Optional (false by default).
EnableMetrics bool
}

// DefaultOptions is an Options object with default values.
Expand Down Expand Up @@ -121,7 +140,21 @@ func NewClient(options Options) (Client, error) {
DB: options.DB,
})

err := client.Ping().Err()
if options.EnableTracing {
if err := redisotel.InstrumentTracing(client); err != nil {
return result, err
}
}
if options.EnableMetrics {
if err := redisotel.InstrumentMetrics(client); err != nil {
return result, err
}
}

tctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

err := client.Ping(tctx).Err()
if err != nil {
return result, err
}
Expand Down