Skip to content

Commit

Permalink
Add request start and end time to request and response events
Browse files Browse the repository at this point in the history
  • Loading branch information
Just-Sieb committed Aug 7, 2019
1 parent 68da1c7 commit 00e4757
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,11 @@ stripe.off('request', onRequest);
```js
{
api_version: 'latest',
account: 'acct_TEST', // Only present if provided
idempotency_key: 'abc123', // Only present if provided
account: 'acct_TEST', // Only present if provided
idempotency_key: 'abc123', // Only present if provided
method: 'POST',
path: '/v1/charges'
path: '/v1/charges',
request_start_time: 1565125303932 // Unix timestamp in milliseconds
}
```

Expand All @@ -224,13 +225,14 @@ stripe.off('request', onRequest);
```js
{
api_version: 'latest',
account: 'acct_TEST', // Only present if provided
idempotency_key: 'abc123', // Only present if provided
account: 'acct_TEST', // Only present if provided
idempotency_key: 'abc123', // Only present if provided
method: 'POST',
path: '/v1/charges',
status: 402,
request_id: 'req_Ghc9r26ts73DRf',
elapsed: 445 // Elapsed time in milliseconds
elapsed: 445 // Elapsed time in milliseconds
request_end_time: 1565125304377 // Unix timestamp in milliseconds
}
```

Expand Down
9 changes: 7 additions & 2 deletions lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ StripeResource.prototype = {
// lastResponse.
res.requestId = headers['request-id'];

const requestDurationMs = Date.now() - req._requestStart;
const requestEndTime = Date.now();
const requestDurationMs = requestEndTime - req._requestStart;

const responseEvent = utils.removeNullish({
api_version: headers['stripe-version'],
Expand All @@ -131,6 +132,7 @@ StripeResource.prototype = {
status: res.statusCode,
request_id: res.requestId,
elapsed: requestDurationMs,
request_end_time: requestEndTime,
});

this._stripe._emitter.emit('response', responseEvent);
Expand Down Expand Up @@ -368,19 +370,22 @@ StripeResource.prototype = {
ciphers: 'DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:!MD5',
});

const requestStartTime = Date.now();

const requestEvent = utils.removeNullish({
api_version: apiVersion,
account: headers['Stripe-Account'],
idempotency_key: headers['Idempotency-Key'],
method,
path,
request_start_time: requestStartTime,
});

const requestRetries = numRetries || 0;

req._requestEvent = requestEvent;

req._requestStart = Date.now();
req._requestStart = requestStartTime;

this._stripe._emitter.emit('request', requestEvent);

Expand Down
20 changes: 13 additions & 7 deletions test/flows.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,15 @@ describe('Flows', function() {
.slice(2);

function onRequest(request) {
expect(request).to.eql({
api_version: 'latest',
idempotency_key: idempotencyKey,
method: 'POST',
path: '/v1/charges',
account: connectedAccountId,
});
expect(request.api_version).to.equal('latest');
expect(request.idempotency_key).to.equal(idempotencyKey);
expect(request.account).to.equal(connectedAccountId);
expect(request.method).to.equal('POST');
expect(request.path).to.equal('/v1/charges');
expect(request.request_start_time).to.be.within(
Date.now() - 30000,
Date.now()
);

done();
}
Expand Down Expand Up @@ -380,6 +382,10 @@ describe('Flows', function() {
expect(response.request_id).to.match(/req_[\w\d]/);
expect(response.status).to.equal(402);
expect(response.elapsed).to.be.within(50, 30000);
expect(response.request_end_time).to.be.within(
Date.now() - 30000,
Date.now()
);

done();
}
Expand Down

0 comments on commit 00e4757

Please sign in to comment.