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

Add Stripe client telemetry to request headers #661

Merged
merged 10 commits into from
Feb 6, 2019

Conversation

jameshageman-stripe
Copy link
Contributor

@jameshageman-stripe jameshageman-stripe commented Jan 31, 2019

Adds opt-in client telemetry similar to stripe/stripe-node#557, stripe/stripe-go#766, stripe/stripe-ruby#696, etc.

Request metrics are recorded in LiveStripeResponseGetter and stored on a thread safe buffer using ConcurrentLinkedQueue. If the buffer exceeds 100 elements, metrics are dropped. When a request is sent, if the buffer is not empty, one of the metrics is popped off and sent as JSON in the X-Stripe-Client-Telemetry header.

Clients can enable the telemetry using:

Stripe.enableTelemetry = true;

r? @brandur-stripe
cc @dcarney-stripe @akropp-stripe

Copy link

@devshorts devshorts left a comment

Choose a reason for hiding this comment

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

Lookin good! made some comments

@jameshageman-stripe
Copy link
Contributor Author

Updated!
ptal @akropp-stripe @brandur-stripe

Copy link
Contributor

@brandur-stripe brandur-stripe left a comment

Choose a reason for hiding this comment

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

Looks largely good!

My one major objection is that I'd prefer not to pull in a new dependency and establish a new testing convention for this. All the tests around threading are nice, but I'm personally pretty comfortable assuming that ConcurrentLinkedQueue's implementation is correct and isn't going to cause any problems around thread safety.

I'd prefer to just augment verifyRequest to be able to deal with headers, and then just check that we got the telemetry one. If we were going to move everything over to the new testing system, then that would be a totally different story.

Do you have an opinion on this @ob-stripe?

src/main/java/com/stripe/Stripe.java Show resolved Hide resolved
src/main/java/com/stripe/net/LiveStripeResponseGetter.java Outdated Show resolved Hide resolved
@jameshageman-stripe
Copy link
Contributor Author

@brandur-stripe

All the tests around threading are nice, but I'm personally pretty comfortable assuming that ConcurrentLinkedQueue's implementation is correct and isn't going to cause any problems around thread safety.

I'd assume the same thing, but I think the purpose of the test is to make sure our logic is thread safe, not ConcurrentLinkedQueue's.

I'd prefer to just augment verifyRequest to be able to deal with headers, and then just check that we got the telemetry one. If we were going to move everything over to the new testing system, then that would be a totally different story.

Makes sense, let me see what I can do!

@brandur-stripe
Copy link
Contributor

I'd assume the same thing, but I think the purpose of the test is to make sure our logic is thread safe, not ConcurrentLinkedQueue's.

Ah, right. I just meant that our logic is a pretty thin shim around the queue — ConcurrentLinkedQueue is doing all the heavy lifting when it comes to guaranteeing thread safety.

@jameshageman-stripe
Copy link
Contributor Author

@brandur-stripe re:

I'd prefer to just augment verifyRequest to be able to deal with headers, and then just check that we got the telemetry one. If we were going to move everything over to the new testing system, then that would be a totally different story.

I poked around to try to see how I could write the tests using our existing tools, and I ran into the following problem:

verifyRequest verifies calls to the public StripeResponseGetter interface, but the headers are added behind the scenes by the static method LiveStripeResponseGetter::getHeaders. Mockito does not support mocking static code, so trying to spy on the headers will require a non-trivial refactor.

Maybe it's a result of how I modified the LiveStripeResponseGetter, but I think it will be more difficult than it seems to rewrite the new tests using mocks and spies. Do you think we could reconsider adding MockWebServer? We have used the same end-to-end style strategy to test the implementation of client telemetry in stripe-go, stripe-python, and stripe-node.

@ob-stripe
Copy link
Contributor

Mockito does not support mocking static code, so trying to spy on the headers will require a non-trivial refactor.

Yeah, that's the same conclusion I reached back when I wrote verifyRequest. It was good enough for most of our tests, where we mostly want to assert that each resource method sends the correct request (i.e. HTTP verb + path) to the API, but it doesn't let us inspect headers.

I think MockWebServer might be the path of least resistance here, and it's just a dev dependency so it's not too bad.

@brandur-stripe
Copy link
Contributor

Okay, thanks for looking into it at least!

I don't feel too strongly about the new dependency, but if we decide to include it, it'd be nice to have some sort of idea of what our end game is so that we don't end up with half the tests running on one system and half running on the other a year for now. Stated differently: is MockWebServer the direction we want to start moving overall?

But anyway, if OB's happy with it, then LGTM too.

@jameshageman-stripe
Copy link
Contributor Author

Thanks @brandur-stripe. I'll leave it to you and @ob-stripe to merge this when you're ready.

@brandur-stripe
Copy link
Contributor

Ack. @ob-stripe Mind taking a quick look before we merge?

Copy link
Contributor

@ob-stripe ob-stripe left a comment

Choose a reason for hiding this comment

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

LGTM!

@ob-stripe ob-stripe merged commit 6a759ef into master Feb 6, 2019
@ob-stripe ob-stripe deleted the jameshageman/add-client-telemetry branch February 6, 2019 09:26
@ob-stripe
Copy link
Contributor

Released as 7.19.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants