-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Pass meta data into native methods
- Loading branch information
Jan Krems
committed
Apr 21, 2016
1 parent
75d21e9
commit 109f6d1
Showing
7 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
var assign = require('lodash/assign'); | ||
|
||
var original = typeof window !== 'undefined' ? window.fetch : null; | ||
|
||
function instrument() { | ||
window.fetch = function fetch(url, options) { | ||
// This is terrible instrumentation because it doesn't handle | ||
// all possible arguments. E.g. `url` could also be an instance | ||
// of Request. | ||
assign(instrument, options || {}); | ||
return original.apply(this, arguments); | ||
}; | ||
} | ||
module.exports = instrument; | ||
|
||
instrument.reset = function reset() { | ||
window.fetch = original; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
var http = require('http'); | ||
|
||
var assign = require('lodash/assign'); | ||
|
||
var original = http.request; | ||
|
||
function instrument() { | ||
http.request = function request(options) { | ||
// This is terrible instrumentation because it doesn't handle | ||
// all possible arguments. `options` isn't the only possible | ||
// call signature of `http.request`. | ||
assign(instrument, options); | ||
return original.apply(this, arguments); | ||
}; | ||
} | ||
module.exports = instrument; | ||
|
||
instrument.reset = function reset() { | ||
http.request = original; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use strict'; | ||
var assert = require('assertive'); | ||
var assign = require('lodash/assign'); | ||
|
||
var Gofer = require('../'); | ||
|
||
var instrument = require('./instrument'); | ||
|
||
var options = require('./mock-service'); | ||
|
||
function ensureEmpty() { | ||
assign(instrument, { | ||
serviceName: null, | ||
endpointName: null, | ||
methodName: null, | ||
pathParams: null, | ||
}); | ||
assert.equal(null, instrument.serviceName); | ||
assert.equal(null, instrument.endpointName); | ||
assert.equal(null, instrument.methodName); | ||
assert.equal(null, instrument.pathParams); | ||
} | ||
|
||
describe('Verify instrumentation support', function () { | ||
var client = new Gofer({ instrumented: options }, 'instrumented'); | ||
|
||
client.registerEndpoints({ | ||
echo: function (fetch) { | ||
return function () { | ||
return fetch('/{x}', { method: 'PUT', pathParams: { x: 'echo' } }); | ||
}; | ||
}, | ||
named: function (fetch) { | ||
return function () { | ||
return fetch('/echo', { method: 'PUT', methodName: 'echo' }); | ||
}; | ||
}, | ||
}); | ||
|
||
before('add instrumentation', instrument); | ||
after('remove instrumentation', instrument.reset); | ||
|
||
describe('direct request', function () { | ||
before(ensureEmpty); | ||
|
||
before('make a request', function () { | ||
return client.fetch('/echo'); | ||
}); | ||
|
||
it('sets the meta data', function () { | ||
assert.equal('instrumented', instrument.serviceName); | ||
assert.equal(undefined, instrument.endpointName); | ||
assert.equal('get', instrument.methodName); | ||
assert.equal(undefined, instrument.pathParams); | ||
}); | ||
}); | ||
|
||
describe('call endpoint', function () { | ||
before(ensureEmpty); | ||
|
||
before('make a request', function () { | ||
return client.echo(); | ||
}); | ||
|
||
it('sets the meta data', function () { | ||
assert.equal('instrumented', instrument.serviceName); | ||
assert.equal('echo', instrument.endpointName); | ||
assert.equal('put', instrument.methodName); | ||
assert.deepEqual({ x: 'echo' }, instrument.pathParams); | ||
}); | ||
}); | ||
|
||
describe('with explicit methodName', function () { | ||
before(ensureEmpty); | ||
|
||
before('make a request', function () { | ||
return client.named(); | ||
}); | ||
|
||
it('sets the meta data', function () { | ||
assert.equal('instrumented', instrument.serviceName); | ||
assert.equal('named', instrument.endpointName); | ||
assert.equal('echo', instrument.methodName); | ||
assert.equal(undefined, instrument.pathParams); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters