Skip to content

Commit

Permalink
feat(stream): bind stream
Browse files Browse the repository at this point in the history
Bind stream module as event emitter
Remove third-party wrapping
Remove separate request/response binding
Cover w/ test
Improve naming in tests
  • Loading branch information
Gábor Döbrei committed Jul 29, 2015
1 parent d11581f commit 9d18174
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 56 deletions.
39 changes: 39 additions & 0 deletions lib/e2e/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var stream = require('stream');

var cls = require('continuation-local-storage');
var expect = require('chai').expect;
var sinon = require('sinon');

var collectorConfig = require('../config');
var wraps = require('../wraps');

describe('Trace', function () {

before(function () {
sinon.sandbox.stub(collectorConfig, 'appName', 'test');
sinon.sandbox.stub(collectorConfig, 'reporterType', 'logstash');
sinon.sandbox.stub(collectorConfig, 'reporterConfig',
'{"type":"tcp","host":"localhost","port":12201}');
});

after(function () {
wraps.uninstrument();
});

it('binds the stream core module', function () {
var originalCreateNamespace = cls.createNamespace;
var bindEmitterSpy;

var sandbox = this.sandbox;

sinon.sandbox.stub(cls, 'createNamespace', function (str) {
var ns = originalCreateNamespace(str);
bindEmitterSpy = sandbox.spy(ns, 'bindEmitter');
return ns;
});

require('../');

expect(bindEmitterSpy).to.have.been.calledWith(stream.prototype);
});
});
2 changes: 1 addition & 1 deletion lib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('The Trace module', function () {
});

after(function () {
wraps.uninstrumentNatives();
wraps.uninstrument();
});

it('exposes methods', function () {
Expand Down
5 changes: 1 addition & 4 deletions lib/wraps/http.Server.prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@ function wrapListener(listener, collector) {
originalWriteHead.apply(response, arguments);
};

function addSession () {
session.bindEmitter(response);
session.bindEmitter(request);
function addSession() {
session.set('request-id', requestId);

return listener.apply(this, arguments);
}

Expand Down
1 change: 0 additions & 1 deletion lib/wraps/http.request.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ function wrapRequest (original, collector) {
*/
var returned;
returned = original.apply(this, arguments);
session.bindEmitter(returned);

returned.on('error', function (err) {
var collectorDataBag = {
Expand Down
2 changes: 1 addition & 1 deletion lib/wraps/http.request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var dummyCollector = {
}
};

describe('The wrapper module', function () {
describe('The http.request wrapper module', function () {
before(function () {
require('continuation-local-storage').createNamespace('trace');

Expand Down
13 changes: 7 additions & 6 deletions lib/wraps/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var http = require('http');
var https = require('https');
var stream = require('stream');

var getNamespace = require('continuation-local-storage').getNamespace;
var Shimmer = require('./shimmer');
var thirdParty = require('./third-party');

function instrument (collector) {
Shimmer.wrap(http.Server.prototype, 'http.Server.prototype', ['on', 'addListener'],
Expand Down Expand Up @@ -41,15 +42,15 @@ function instrument (collector) {
return require('./process._fatalException')(original, collector);
});

thirdParty.instrument();
getNamespace('trace').bindEmitter(stream.prototype);
}

function uninstrumentNatives () {
function uninstrument () {
Shimmer.unwrap(https.Server.prototype, 'https.Server.prototype', 'on');
Shimmer.unwrap(https.Server.prototype, 'https.Server.prototype', 'addListener');

Shimmer.unwrap(http.Server.prototype, 'https.Server.prototype', 'addListener');
Shimmer.unwrap(http.Server.prototype, 'https.Server.prototype', 'addListener');
Shimmer.unwrap(http.Server.prototype, 'http.Server.prototype', 'addListener');
Shimmer.unwrap(http.Server.prototype, 'http.Server.prototype', 'addListener');

Shimmer.unwrap(http, 'http', 'request');

Expand All @@ -59,4 +60,4 @@ function uninstrumentNatives () {
}

module.exports.instrument = instrument;
module.exports.uninstrumentNatives = uninstrumentNatives;
module.exports.uninstrument = uninstrument;
2 changes: 1 addition & 1 deletion lib/wraps/process._fatalException.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function startFailingServer () {
return child;
}

describe('The stacktrace wrapper module', function () {
describe('The process._fatalException wrapper module', function () {

var spawned;

Expand Down
42 changes: 0 additions & 42 deletions lib/wraps/third-party/index.js

This file was deleted.

0 comments on commit 9d18174

Please sign in to comment.