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 #557

Merged
merged 4 commits into from
Jan 31, 2019

Conversation

jameshageman-stripe
Copy link
Contributor

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

r? @brandur-stripe @ob-stripe
cc @stripe/api-libraries @dcarney @akropp-stripe

Replicates stripe/stripe-go#766, adding request duration metrics to subsequent requests in the X-Stripe-Client-Telemetry header.

It can be enabled with:

stripe.setTelemetryEnabled(true);

Since node.js is concurrent but only runs on one thread, I've used a buffering approach similar to the go implementation but without explicit mutual exclusion. Right now the buffer is unbounded, so it's size is proportional to the maximum number of successful, concurrent requests.

Instead of mocking out the server, I spin up a real http server on a random port for each test, and add assertions on the server side. This may be unconventional, but it allows me to be absolutely sure that the metrics are reaching the server. I used a similar approach in the Go implementation.

@@ -125,6 +125,8 @@ StripeResource.prototype = {
// lastResponse.
res.requestId = headers['request-id'];

var requestDuration = Date.now() - req._requestStart;
Copy link

Choose a reason for hiding this comment

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

could you make this variable include the units in the name? (e.g. suffix it with "ms")?

lib/stripe.js Outdated
this._enableTelemetry = enableTelemetry;
},

telemetryEnabled: function() {
Copy link

Choose a reason for hiding this comment

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

It looks like the convention in the rest of this file is to use get/set prefixes on the function names, so I'd change this to getTelemetryEnabled.

@dcarney
Copy link

dcarney commented Jan 28, 2019

Made a couple small comments, but I'll defer to the lib's maintainers for the final approval. 👍

@jameshageman-stripe
Copy link
Contributor Author

jameshageman-stripe commented Jan 30, 2019

@brandur-stripe could you take a look at this one?

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.

Left a couple comments, but largely LGTM! Thanks for getting all of these through, and thanks for the additional review @dcarney!

ptal @jameshageman-stripe

'request_id': res.requestId,
'request_duration_ms': requestDurationMs,
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Like for stripe-python, can we put these into a _addTelemetryHeader and _recordRequestMetrics or such?

Copy link
Contributor

Choose a reason for hiding this comment

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

This unbounded buffer worries me a little bit, do you think that we could have a check against some arbitrarily large size (e.g. > 100) and if we get there, don't push on a new value and then use utils.emitWarning to indicate that we dropped a request result? (So that we can tell it's happening.)

@@ -248,6 +258,13 @@ StripeResource.prototype = {
Object.assign(headers, options.headers);
}

if (self._stripe.getTelemetryEnabled() && self._stripe._prevRequestMetrics.length > 0) {
var metrics = self._stripe._prevRequestMetrics.pop();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we use shift instead of pop so that the buffer becomes FIFO?

@jameshageman-stripe
Copy link
Contributor Author

I've made the buffer both bounded and FIFO
ptal @brandur-stripe

@brandur-stripe
Copy link
Contributor

LGTM! Thanks.

@brandur-stripe brandur-stripe merged commit bb2acf9 into master Jan 31, 2019
@brandur-stripe brandur-stripe deleted the jameshageman/add-client-telemetry branch January 31, 2019 00:57
@brandur-stripe
Copy link
Contributor

Released as 6.23.0.

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

Successfully merging this pull request may close these issues.

4 participants