From 88a74e17725e54a370dd6626fb4fedd518ffece4 Mon Sep 17 00:00:00 2001 From: Gergely Nemeth Date: Thu, 6 Aug 2015 14:12:54 +0200 Subject: [PATCH] feat(ignoreHeaders): add ignoreHeaders to the config --- README.md | 4 +++ lib/index.js | 2 +- lib/wraps/http.Server.prototype.js | 16 +++++++++- lib/wraps/http.Server.prototype.spec.js | 40 +++++++++++++++++++++++++ lib/wraps/index.js | 12 ++++---- 5 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 lib/wraps/http.Server.prototype.spec.js diff --git a/README.md b/README.md index 2d9ac47..94e1f45 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,10 @@ var config = {}; config.appName = 'Users'; +config.ignoreHeaders = { + 'user-agent': ['007'] +}; + config.reporter = require('@risingstack/trace/lib/reporters').trace.create({ apiKey: '1234', appName: config.appName diff --git a/lib/index.js b/lib/index.js index 1ef4d7b..cba6e86 100644 --- a/lib/index.js +++ b/lib/index.js @@ -29,7 +29,7 @@ var config = getConfig(); var collector = new Collector(config); -wraps.instrument(collector); +wraps.instrument(collector, config); function setService(config, callback) { var reporter = config.reporter; diff --git a/lib/wraps/http.Server.prototype.js b/lib/wraps/http.Server.prototype.js index 017d188..ac48041 100644 --- a/lib/wraps/http.Server.prototype.js +++ b/lib/wraps/http.Server.prototype.js @@ -3,16 +3,30 @@ var url = require('url'); var qs = require('qs'); var uuid = require('node-uuid'); var microtime = require('microtime'); +var reduce = require('lodash/collection/reduce'); var getNamespace = require('continuation-local-storage').getNamespace; var Collector = require('../collector'); -function wrapListener(listener, collector) { +function wrapListener(listener, collector, config) { + + var ignoreHeaders = config.ignoreHeaders; return function (request, response) { var headers = request.headers; + var skipped = reduce(ignoreHeaders, function (found, value, key) { + if (headers[key] && value.indexOf(headers[key]) > -1) { + found = true; + } + return found; + }, false); + + if (skipped) { + return listener.apply(this, arguments); + } + var requestUrl = url.parse(request.url); var requestQuery = qs.parse(requestUrl.query).requestId; var originalWriteHead = response.writeHead; diff --git a/lib/wraps/http.Server.prototype.spec.js b/lib/wraps/http.Server.prototype.spec.js new file mode 100644 index 0000000..c697bc1 --- /dev/null +++ b/lib/wraps/http.Server.prototype.spec.js @@ -0,0 +1,40 @@ +var expect = require('chai').expect; + +var wrapper = require('./http.Server.prototype'); + +var dummyCollector = { + emit: function () { + }, + getService: function () { + return 1; + } +}; + +describe('The http.Server.prototype wrapper module', function () { + + describe('ingoreHeaders option', function () { + + it('skips requests if there is a match', function () { + + var request = { + headers: { + 'user-agent': '007' + } + }; + + var listener = this.sandbox.spy(); + + var wrappedListener = wrapper(listener, dummyCollector, { + ignoreHeaders: { + 'user-agent': ['006', '007'] + } + }); + + wrappedListener(request); + + expect(listener).to.be.calledWith(request); + }); + + }); + +}); diff --git a/lib/wraps/index.js b/lib/wraps/index.js index b408220..369cae3 100644 --- a/lib/wraps/index.js +++ b/lib/wraps/index.js @@ -5,13 +5,13 @@ var stream = require('stream'); var getNamespace = require('continuation-local-storage').getNamespace; var Shimmer = require('./shimmer'); -function instrument (collector) { +function instrument (collector, config) { Shimmer.wrap(http.Server.prototype, 'http.Server.prototype', ['on', 'addListener'], function (addListener) { return function (type, listener) { if (type === 'request' && typeof listener === 'function') { return addListener.call(this, type, - require('./http.Server.prototype.js')(listener, collector)); + require('./http.Server.prototype.js')(listener, collector, config)); } else { return addListener.apply(this, arguments); } @@ -23,7 +23,7 @@ function instrument (collector) { return function (type, listener) { if (type === 'request' && typeof listener === 'function') { return addListener.call(this, type, - require('./http.Server.prototype.js')(listener, collector)); + require('./http.Server.prototype.js')(listener, collector, config)); } else { return addListener.apply(this, arguments); } @@ -31,15 +31,15 @@ function instrument (collector) { }); Shimmer.wrap(http, 'http', 'request', function (original) { - return require('./http.request')(original, collector); + return require('./http.request')(original, collector, config); }); Shimmer.wrap(https, 'https', 'request', function (original) { - return require('./http.request')(original, collector); + return require('./http.request')(original, collector, config); }); Shimmer.wrap(process, 'process', '_fatalException', function (original) { - return require('./process._fatalException')(original, collector); + return require('./process._fatalException')(original, collector, config); }); getNamespace('trace').bindEmitter(stream.prototype);