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

contrib/bradfitz/gomemcache: trace item info for memcached operations #642

Merged
merged 4 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 37 additions & 0 deletions contrib/bradfitz/gomemcache/memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func (c *Client) startSpan(resourceName string) ddtrace.Span {
func (c *Client) Add(item *memcache.Item) error {
span := c.startSpan("Add")
err := c.Client.Add(item)
span.SetTag("item.key", item.Key)
if c.cfg.withValueTags {
span.SetTag("item.value", item.Value)
span.SetTag("item.expiration", item.Expiration)
gbbr marked this conversation as resolved.
Show resolved Hide resolved
}
span.Finish(tracer.WithError(err))
return err
}
Expand All @@ -88,6 +93,11 @@ func (c *Client) Add(item *memcache.Item) error {
func (c *Client) CompareAndSwap(item *memcache.Item) error {
span := c.startSpan("CompareAndSwap")
err := c.Client.CompareAndSwap(item)
span.SetTag("item.key", item.Key)
if c.cfg.withValueTags {
span.SetTag("item.value", item.Value)
span.SetTag("item.expiration", item.Expiration)
}
span.Finish(tracer.WithError(err))
return err
}
Expand All @@ -96,6 +106,11 @@ func (c *Client) CompareAndSwap(item *memcache.Item) error {
func (c *Client) Decrement(key string, delta uint64) (newValue uint64, err error) {
span := c.startSpan("Decrement")
newValue, err = c.Client.Decrement(key, delta)
span.SetTag("item.key", key)
if c.cfg.withValueTags {
span.SetTag("item.value.before", newValue-delta)
span.SetTag("item.value.after", newValue)
}
span.Finish(tracer.WithError(err))
return newValue, err
}
Expand All @@ -104,6 +119,7 @@ func (c *Client) Decrement(key string, delta uint64) (newValue uint64, err error
func (c *Client) Delete(key string) error {
span := c.startSpan("Delete")
err := c.Client.Delete(key)
span.SetTag("item.key", key)
span.Finish(tracer.WithError(err))
return err
}
Expand All @@ -128,6 +144,7 @@ func (c *Client) FlushAll() error {
func (c *Client) Get(key string) (item *memcache.Item, err error) {
span := c.startSpan("Get")
item, err = c.Client.Get(key)
span.SetTag("item.key", key)
span.Finish(tracer.WithError(err))
return item, err
}
Expand All @@ -136,6 +153,7 @@ func (c *Client) Get(key string) (item *memcache.Item, err error) {
func (c *Client) GetMulti(keys []string) (map[string]*memcache.Item, error) {
span := c.startSpan("GetMulti")
items, err := c.Client.GetMulti(keys)
span.SetTag("item.keys", keys)
span.Finish(tracer.WithError(err))
return items, err
}
Expand All @@ -144,6 +162,11 @@ func (c *Client) GetMulti(keys []string) (map[string]*memcache.Item, error) {
func (c *Client) Increment(key string, delta uint64) (newValue uint64, err error) {
span := c.startSpan("Increment")
newValue, err = c.Client.Increment(key, delta)
span.SetTag("item.key", key)
if c.cfg.withValueTags {
span.SetTag("item.value.before", newValue-delta)
span.SetTag("item.value.after", newValue)
}
span.Finish(tracer.WithError(err))
return newValue, err
}
Expand All @@ -152,6 +175,11 @@ func (c *Client) Increment(key string, delta uint64) (newValue uint64, err error
func (c *Client) Replace(item *memcache.Item) error {
span := c.startSpan("Replace")
err := c.Client.Replace(item)
span.SetTag("item.key", item.Key)
if c.cfg.withValueTags {
span.SetTag("item.value", item.Value)
span.SetTag("item.expiration", item.Expiration)
}
span.Finish(tracer.WithError(err))
return err
}
Expand All @@ -160,6 +188,11 @@ func (c *Client) Replace(item *memcache.Item) error {
func (c *Client) Set(item *memcache.Item) error {
span := c.startSpan("Set")
err := c.Client.Set(item)
span.SetTag("item.key", item.Key)
if c.cfg.withValueTags {
span.SetTag("item.value", item.Value)
span.SetTag("item.expiration", item.Expiration)
}
span.Finish(tracer.WithError(err))
return err
}
Expand All @@ -168,6 +201,10 @@ func (c *Client) Set(item *memcache.Item) error {
func (c *Client) Touch(key string, seconds int32) error {
span := c.startSpan("Touch")
err := c.Client.Touch(key, seconds)
span.SetTag("item.key", key)
if c.cfg.withValueTags {
span.SetTag("item.expiration", seconds)
}
span.Finish(tracer.WithError(err))
return err
}
10 changes: 7 additions & 3 deletions contrib/bradfitz/gomemcache/memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestMemcacheIntegration(t *testing.T) {
}

func testMemcache(t *testing.T, addr string) {
client := WrapClient(memcache.New(addr), WithServiceName("test-memcache"))
client := WrapClient(memcache.New(addr), WithServiceName("test-memcache"), WithValueTags(true))
defer client.DeleteAll()

validateMemcacheSpan := func(t *testing.T, span mocktracer.Span, resourceName string) {
Expand Down Expand Up @@ -76,8 +76,9 @@ func testMemcache(t *testing.T, addr string) {
err := client.
WithContext(ctx).
Add(&memcache.Item{
Key: "key2",
Value: []byte("value2"),
Key: "key2",
Value: []byte("value2"),
Expiration: 10,
})
assert.Nil(t, err)

Expand All @@ -89,6 +90,9 @@ func testMemcache(t *testing.T, addr string) {
assert.Equal(t, span, spans[1])
assert.Equal(t, spans[1].TraceID(), spans[0].TraceID(),
"memcache span should be part of the parent trace")
assert.Equal(t, "key2", spans[0].Tag("item.key"))
assert.Equal(t, []byte("value2"), spans[0].Tag("item.value"))
assert.Equal(t, int32(10), spans[0].Tag("item.expiration"))
})
}

Expand Down
8 changes: 8 additions & 0 deletions contrib/bradfitz/gomemcache/memcache/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
type clientConfig struct {
serviceName string
analyticsRate float64
withValueTags bool
}

// ClientOption represents an option that can be passed to Dial.
Expand Down Expand Up @@ -57,3 +58,10 @@ func WithAnalyticsRate(rate float64) ClientOption {
}
}
}

// WithValueTags enables tracing of values used in operations
func WithValueTags(on bool) ClientOption {
gbbr marked this conversation as resolved.
Show resolved Hide resolved
return func(cfg *clientConfig) {
cfg.withValueTags = on
}
}