Skip to content

Commit

Permalink
contrib/bradfitz/gomemcache: trace item info for memcached operations (
Browse files Browse the repository at this point in the history
…DataDog#642)

This commit adds more tags to each memcached operation, along with an option to also add assignment values.

Fixes DataDog#640
  • Loading branch information
mingrammer authored Apr 22, 2020
1 parent 6639723 commit 3f45f6d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
35 changes: 35 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)
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,8 @@ 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)
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())
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
9 changes: 9 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,11 @@ func WithAnalyticsRate(rate float64) ClientOption {
}
}
}

// WithValueTags specifies whether values assigned to keys in memcache operations
// should be added to spans as tags.
func WithValueTags() ClientOption {
return func(cfg *clientConfig) {
cfg.withValueTags = true
}
}

0 comments on commit 3f45f6d

Please sign in to comment.