Skip to content

Commit

Permalink
feat(http): add monitorIncomingHTTPRequests config
Browse files Browse the repository at this point in the history
  • Loading branch information
Qard committed Aug 27, 2019
1 parent d866894 commit aa46b28
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 32 deletions.
5 changes: 4 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var DEFAULTS = {
logLevel: 'info',
metricsInterval: '30s',
metricsLimit: 1000,
monitorIncomingHTTPRequests: true,
centralConfig: true,
serverTimeout: '30s',
sourceLinesErrorAppFrames: 5,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -138,10 +140,11 @@ var BOOL_OPTS = [
'captureExceptions',
'captureHeaders',
'captureSpanStackTraces',
'centralConfig',
'errorOnAbortedRequests',
'filterHttpHeaders',
'instrument',
'centralConfig',
'monitorIncomingHTTPRequests',
'usePathAsTransactionName',
'verifyServerCert'
]
Expand Down
13 changes: 8 additions & 5 deletions lib/instrumentation/modules/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions lib/instrumentation/modules/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions lib/instrumentation/modules/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 8 additions & 20 deletions test/instrumentation/_agent.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions test/instrumentation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var agent = require('../..').start({
serviceName: 'test',
breakdownMetrics: false,
captureExceptions: false,
metricsInterval: 0
})
Expand Down

0 comments on commit aa46b28

Please sign in to comment.