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: add memcache tracer #291

Merged
merged 12 commits into from
Aug 8, 2018
Merged

Conversation

dd-caleb
Copy link
Contributor

Fixes #290

This adds a memcache trace library. We wrap the memcache.Client and return a new struct with all the same methods:

type Client struct {
	*memcache.Client
	cfg     *clientConfig
	context context.Context
}
func (c *Client) Add(item *memcache.Item) error {
	span := c.startSpan("Add")
	err := c.Client.Add(item)
	span.Finish(tracer.WithError(err))
	return err
}

We also add one additional method, WithContext:

func (c *Client) WithContext(ctx context.Context) *Client {
	// the existing memcache client doesn't support context, but may in the
	// future, so we do a runtime check to detect this
	mc := c.Client
	if hasWithContext, ok := (interface{})(c.Client).(interface {
		WithContext(context.Context) *memcache.Client
	}); ok {
		mc = hasWithContext.WithContext(ctx)
	}
	return &Client{
		Client:  mc,
		cfg:     c.cfg,
		context: ctx,
	}
}

This is similar to how the redis library implements Context.

@dd-caleb dd-caleb added apm:ecosystem contrib/* related feature requests or bugs feature-request labels Aug 3, 2018
Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

An initial round of comments.

// you can use WithContext to set the parent span
mc.WithContext(ctx).Set(&memcache.Item{Key: "my key", Value: []byte("my value")})

span.Finish()
Copy link
Contributor

Choose a reason for hiding this comment

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

We usually defer this in most examples.

)

func Example() {
span, ctx := tracer.StartSpanFromContext(context.Background(), "example",
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make up some better imaginary names than "example" for span and service? Let's think of a basic simplistic use-case or "hello world" example.

// the existing memcache client doesn't support context, but may in the
// future, so we do a runtime check to detect this
mc := c.Client
if hasWithContext, ok := (interface{})(c.Client).(interface {
Copy link
Contributor

Choose a reason for hiding this comment

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

This (interface{}) hack is nasty. But it's ok, it's a good approach.

// the existing memcache client doesn't support context, but may in the
// future, so we do a runtime check to detect this
mc := c.Client
if hasWithContext, ok := (interface{})(c.Client).(interface {
Copy link
Contributor

Choose a reason for hiding this comment

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

hasWithContext sounds like a boolean. Can we use something else (some examples: v, wc)

// startSpan starts a span from the context set with WithContext.
func (c *Client) startSpan(resourceName string) ddtrace.Span {
span, _ := tracer.StartSpanFromContext(c.context, operationName,
tracer.ServiceName(c.cfg.serviceName),
Copy link
Contributor

Choose a reason for hiding this comment

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

tracer.SpanType(ext.SpanTypeMemcached)

}
defer li.Close()

go func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we move this to a separate function and have a unit test for it?

spans := mt.FinishedSpans()
assert.Len(t, spans, 1)
validateMemcacheSpan(t, spans[0], "Add")
})
Copy link
Contributor

Choose a reason for hiding this comment

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

please add an empty line between

})
}

func makeFakeServer(t *testing.T) net.Listener {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should write a test for this helper. Our main tests rely on it.

Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

Thanks!

@@ -30,6 +30,7 @@ jobs:
DD_APM_ENABLED: "true"
DD_BIND_HOST: "0.0.0.0"
DD_API_KEY: invalid_key_but_this_is_fine
- image: memcached:1.5.9
Copy link
Contributor

Choose a reason for hiding this comment

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

You might have already noticed, but in case not, we're using resource_class: xlarge. I'm not sure how long that'll last us if we keep adding new services. It's 8 CPUs and 16GB.

}

func TestMemcacheIntegration(t *testing.T) {
if _, ok := os.LookupEnv("INTEGRATION"); !ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@dd-caleb dd-caleb merged commit e48db92 into v1 Aug 8, 2018
@dd-caleb dd-caleb deleted the caleb/memcache branch August 8, 2018 13:20
@gbbr gbbr added this to the 1.2.0 milestone Aug 22, 2018
mingrammer pushed a commit to mingrammer/dd-trace-go that referenced this pull request Dec 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apm:ecosystem contrib/* related feature requests or bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants