diff --git a/lib/config.js b/lib/config.js index 90472f27641..18ef922e149 100644 --- a/lib/config.js +++ b/lib/config.js @@ -61,6 +61,7 @@ var DEFAULTS = { logLevel: 'info', metricsInterval: '30s', metricsLimit: 1000, + monitorIncomingHTTPRequests: true, centralConfig: true, serverTimeout: '30s', sourceLinesErrorAppFrames: 5, @@ -105,6 +106,7 @@ var ENV_TABLE = { logLevel: 'ELASTIC_APM_LOG_LEVEL', metricsInterval: 'ELASTIC_APM_METRICS_INTERVAL', metricsLimit: 'ELASTIC_APM_METRICS_LIMIT', + monitorIncomingHTTPRequests: 'ELASTIC_APM_MONITOR_INCOMING_HTTP_REQUESTS', payloadLogFile: 'ELASTIC_APM_PAYLOAD_LOG_FILE', centralConfig: 'ELASTIC_APM_CENTRAL_CONFIG', secretToken: 'ELASTIC_APM_SECRET_TOKEN', @@ -138,10 +140,11 @@ var BOOL_OPTS = [ 'captureExceptions', 'captureHeaders', 'captureSpanStackTraces', + 'centralConfig', 'errorOnAbortedRequests', 'filterHttpHeaders', 'instrument', - 'centralConfig', + 'monitorIncomingHTTPRequests', 'usePathAsTransactionName', 'verifyServerCert' ] diff --git a/lib/instrumentation/modules/http.js b/lib/instrumentation/modules/http.js index c3a5e63d4b0..f5f9877d871 100644 --- a/lib/instrumentation/modules/http.js +++ b/lib/instrumentation/modules/http.js @@ -6,9 +6,15 @@ var httpShared = require('../http-shared') var shimmer = require('../shimmer') module.exports = function (http, agent, { enabled }) { + if (agent._conf.monitorIncomingHTTPRequests) { + agent.logger.debug('shimming http.Server.prototype.emit function') + shimmer.wrap(http && http.Server && http.Server.prototype, 'emit', httpShared.instrumentRequest(agent, 'http')) + + agent.logger.debug('shimming http.ServerResponse.prototype.writeHead function') + shimmer.wrap(http && http.ServerResponse && http.ServerResponse.prototype, 'writeHead', wrapWriteHead) + } + if (!enabled) return http - agent.logger.debug('shimming http.Server.prototype.emit function') - shimmer.wrap(http && http.Server && http.Server.prototype, 'emit', httpShared.instrumentRequest(agent, 'http')) agent.logger.debug('shimming http.request function') shimmer.wrap(http, 'request', httpShared.traceOutgoingRequest(agent, 'http', 'request')) @@ -18,9 +24,6 @@ module.exports = function (http, agent, { enabled }) { shimmer.wrap(http, 'get', httpShared.traceOutgoingRequest(agent, 'http', 'get')) } - agent.logger.debug('shimming http.ServerResponse.prototype.writeHead function') - shimmer.wrap(http && http.ServerResponse && http.ServerResponse.prototype, 'writeHead', wrapWriteHead) - return http function wrapWriteHead (original) { diff --git a/lib/instrumentation/modules/http2.js b/lib/instrumentation/modules/http2.js index fd5bf4067e0..db2ab458ce7 100644 --- a/lib/instrumentation/modules/http2.js +++ b/lib/instrumentation/modules/http2.js @@ -8,11 +8,15 @@ var shimmer = require('../shimmer') var symbols = require('../../symbols') module.exports = function (http2, agent, { enabled }) { + if (agent._conf.monitorIncomingHTTPRequests) { + agent.logger.debug('shimming http2.createServer function') + shimmer.wrap(http2, 'createServer', wrapCreateServer) + shimmer.wrap(http2, 'createSecureServer', wrapCreateServer) + } + if (!enabled) return http2 var ins = agent._instrumentation - agent.logger.debug('shimming http2.createServer function') - shimmer.wrap(http2, 'createServer', wrapCreateServer) - shimmer.wrap(http2, 'createSecureServer', wrapCreateServer) + agent.logger.debug('shimming http2.connect function') shimmer.wrap(http2, 'connect', wrapConnect) return http2 diff --git a/lib/instrumentation/modules/https.js b/lib/instrumentation/modules/https.js index 9f64d82d40c..4850455edcd 100644 --- a/lib/instrumentation/modules/https.js +++ b/lib/instrumentation/modules/https.js @@ -6,10 +6,12 @@ var httpShared = require('../http-shared') var shimmer = require('../shimmer') module.exports = function (https, agent, { version, enabled }) { - if (!enabled) return https - agent.logger.debug('shimming https.Server.prototype.emit function') - shimmer.wrap(https && https.Server && https.Server.prototype, 'emit', httpShared.instrumentRequest(agent, 'https')) + if (agent._conf.monitorIncomingHTTPRequests) { + agent.logger.debug('shimming https.Server.prototype.emit function') + shimmer.wrap(https && https.Server && https.Server.prototype, 'emit', httpShared.instrumentRequest(agent, 'https')) + } + if (!enabled) return https // From Node.js v9.0.0 and onwards, https requests no longer just call the // http.request function. So to correctly instrument outgoing HTTPS requests // in all supported Node.js versions, we'll only only instrument the diff --git a/test/instrumentation/_agent.js b/test/instrumentation/_agent.js index 810a6191483..0ae2ad68018 100644 --- a/test/instrumentation/_agent.js +++ b/test/instrumentation/_agent.js @@ -1,5 +1,6 @@ 'use strict' +var config = require('../../lib/config') var Metrics = require('../../lib/metrics') var Instrumentation = require('../../lib/instrumentation') var mockClient = require('../_mock_http_client') @@ -12,27 +13,14 @@ var sharedInstrumentation module.exports = function mockAgent (expected, cb) { var agent = { - _conf: { - serviceName: 'service-name', - active: true, - instrument: true, - captureSpanStackTraces: true, + _conf: config({ + abortedErrorThreshold: '250ms', + asyncHooks: false, + breakdownMetrics: false, + centralConfig: false, errorOnAbortedRequests: false, - abortedErrorThreshold: 0.25, - sourceLinesErrorAppFrames: 5, - sourceLinesErrorLibraryFrames: 5, - sourceLinesSpanAppFrames: 5, - sourceLinesSpanLibraryFrames: 0, - ignoreUrlStr: [], - ignoreUrlRegExp: [], - ignoreUserAgentStr: [], - ignoreUserAgentRegExp: [], - transactionSampleRate: 1.0, - disableInstrumentations: [], - captureHeaders: true, - metricsInterval: 0, - centralConfig: false - }, + metricsInterval: 0 + }), _errorFilters: new Filters(), _transactionFilters: new Filters(), _spanFilters: new Filters(), diff --git a/test/instrumentation/index.js b/test/instrumentation/index.js index 4c4074c75f1..cdd9f598b5f 100644 --- a/test/instrumentation/index.js +++ b/test/instrumentation/index.js @@ -2,6 +2,7 @@ var agent = require('../..').start({ serviceName: 'test', + breakdownMetrics: false, captureExceptions: false, metricsInterval: 0 })