diff --git a/lib/adapters/default.js b/lib/adapters/default.js index c23ca00..360041e 100644 --- a/lib/adapters/default.js +++ b/lib/adapters/default.js @@ -29,7 +29,10 @@ function requestProxy(options = {}) { reqId = requestOptions.headers[traceHeaderName]; reqObj = mapOutReq(requestOptions, reqId, opts); - const blacklistedPathRegex = opts.blacklistedPathRegex; + const blacklistedPathRegex = + typeof opts.blacklistedPathRegex === 'string' + ? new RegExp(opts.blacklistedPathRegex, 'gi') + : opts.blacklistedPathRegex; if (!reqObj.protocol || (blacklistedPathRegex && blacklistedPathRegex.test(reqObj.path))) { // do not log custom requests (for example requests started by the newrelic agent) return target.apply(thisArg, argumentsList); diff --git a/lib/transformers/transformers.js b/lib/transformers/transformers.js index d1a6a8f..5890142 100644 --- a/lib/transformers/transformers.js +++ b/lib/transformers/transformers.js @@ -179,7 +179,7 @@ const mapOutReq = (requestOptions, reqId, opts = {}) => { if (opts.bodyKeysCallback && typeof opts.bodyKeysCallback === 'function') { picked = opts.bodyKeysCallback(picked); } else if (opts.bodyKeysRegex) { - const REGEX = opts.bodyKeysRegex; + const REGEX = typeof opts.bodyKeysRegex === 'string' ? new RegExp(opts.bodyKeysRegex, 'i') : opts.bodyKeysRegex; picked = pickBy(picked, (_, key) => REGEX.test(key)); } else { picked = pick(picked, opts.bodyKeys); diff --git a/test/lib/adapters/default/requestProxyTest.js b/test/lib/adapters/default/requestProxyTest.js index c7925b2..d5abab6 100644 --- a/test/lib/adapters/default/requestProxyTest.js +++ b/test/lib/adapters/default/requestProxyTest.js @@ -164,50 +164,100 @@ describe('#defaultAdapter', () => { logger.info.callCount.should.eql(0); }); - it('should pass but not log when path is in blacklisted paths', () => { - const logger = { - info: sandbox.spy() - }; - const serialize = a => a; - const traceHeaderName = 'test'; - const requestProxy = defaultAdapter.requestProxy({ - logger, - serialize, - traceHeaderName, - level: 'info', - opts: { - blacklistedPathRegex: new RegExp('^/v4.0/traces$'), - request: { - enabled: true + context('when path is in blacklisted paths', () => { + it('should pass but not log when path is in blacklisted paths', () => { + const logger = { + info: sandbox.spy() + }; + const serialize = a => a; + const traceHeaderName = 'test'; + const requestProxy = defaultAdapter.requestProxy({ + logger, + serialize, + traceHeaderName, + level: 'info', + opts: { + blacklistedPathRegex: new RegExp('^/v4.0/traces$'), + request: { + enabled: true + }, + hostFieldName: 'myHost' + } + }); + const options = { + method: 'PUT', + port: '8080', + headers: { + test: 'ok' }, - hostFieldName: 'myHost' - } + protocol: 'https:', + path: '/v4.0/traces', + pathname: '/v4.0/traces', + hostname: 'test-host' + }; + const http = { + request: () => { + return { + on: (event, fn) => { + fn({ + statusCode: 200 + }); + } + }; + } + }; + http.request = new Proxy(http.request, requestProxy); + http.request(options); + logger.info.callCount.should.eql(0); + }); + + it('should convert a string blacklistedPathRegex to RegExp', () => { + const logger = { + info: sandbox.spy(), + error: sandbox.spy() + }; + const serialize = a => a; + const traceHeaderName = 'test'; + const requestProxy = defaultAdapter.requestProxy({ + logger, + serialize, + traceHeaderName, + level: 'info', + opts: { + blacklistedPathRegex: '^/v4.0/trac.*', + request: { + enabled: true + }, + hostFieldName: 'myHost' + } + }); + const options = { + method: 'PUT', + port: '8080', + headers: { + test: 'ok' + }, + protocol: 'https:', + path: '/v4.0/traces', + pathname: '/v4.0/traces', + hostname: 'test-host' + }; + const http = { + request: () => { + return { + on: (event, fn) => { + fn({ + statusCode: 200 + }); + } + }; + } + }; + http.request = new Proxy(http.request, requestProxy); + http.request(options); + logger.info.callCount.should.eql(0); + logger.error.callCount.should.eql(0); }); - const options = { - method: 'PUT', - port: '8080', - headers: { - test: 'ok' - }, - protocol: 'https:', - path: '/v4.0/traces', - pathname: '/v4.0/traces', - hostname: 'test-host' - }; - const http = { - request: () => { - return { - on: (event, fn) => { - fn({ - statusCode: 200 - }); - } - }; - } - }; - http.request = new Proxy(http.request, requestProxy); - http.request(options); - logger.info.callCount.should.eql(0); }); it('should pass when incomingMessage.url does not exist', () => { diff --git a/test/lib/transformers/transformersTest.js b/test/lib/transformers/transformersTest.js index 2fe52df..d167ebf 100644 --- a/test/lib/transformers/transformersTest.js +++ b/test/lib/transformers/transformersTest.js @@ -274,4 +274,45 @@ describe('mapOutReq', () => { log_tag: 'outbound_request' }); }); + + it('should convert a string bodyKeysRegex to RegExp', () => { + const inMsg = { + method: 'POST', + body: '{ "bananas": "bar", "banana1": "apple1", "banana2": "apple2" }', + headers: { + 'Content-type': 'application/json; charset=UTF-8' + }, + uri: { + protocol: 'http:', + hostname: 'hostname', + pathname: 'path', + query: 'query' + } + }; + + const options = { + bodyKeysRegex: '^banana\\d+' + }; + + const result = mapOutReq(inMsg, undefined, options); + result.should.eql({ + method: 'POST', + protocol: 'http', + host: 'hostname', + port: undefined, + path: 'path', + query: 'query', + href: 'http://hostname/path', + requestId: undefined, + contentLength: 0, + log_tag: 'outbound_request', + metaHeaders: {}, + metaBody: { + body: { + banana1: 'apple1', + banana2: 'apple2' + } + } + }); + }); });