Skip to content

Commit 76aaef6

Browse files
committed
feat(http): add monitorIncomingHTTPRequests config
1 parent 55e45e1 commit 76aaef6

File tree

7 files changed

+34
-32
lines changed

7 files changed

+34
-32
lines changed

lib/config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ var DEFAULTS = {
6969
logLevel: 'info',
7070
metricsInterval: '30s',
7171
metricsLimit: 1000,
72+
monitorIncomingHTTPRequests: true,
7273
centralConfig: true,
7374
serverTimeout: '30s',
7475
sourceLinesErrorAppFrames: 5,
@@ -113,6 +114,7 @@ var ENV_TABLE = {
113114
logLevel: 'ELASTIC_APM_LOG_LEVEL',
114115
metricsInterval: 'ELASTIC_APM_METRICS_INTERVAL',
115116
metricsLimit: 'ELASTIC_APM_METRICS_LIMIT',
117+
monitorIncomingHTTPRequests: 'ELASTIC_APM_MONITOR_INCOMING_HTTP_REQUESTS',
116118
payloadLogFile: 'ELASTIC_APM_PAYLOAD_LOG_FILE',
117119
centralConfig: 'ELASTIC_APM_CENTRAL_CONFIG',
118120
secretToken: 'ELASTIC_APM_SECRET_TOKEN',
@@ -146,10 +148,11 @@ var BOOL_OPTS = [
146148
'captureExceptions',
147149
'captureHeaders',
148150
'captureSpanStackTraces',
151+
'centralConfig',
149152
'errorOnAbortedRequests',
150153
'filterHttpHeaders',
151154
'instrument',
152-
'centralConfig',
155+
'monitorIncomingHTTPRequests',
153156
'usePathAsTransactionName',
154157
'verifyServerCert'
155158
]

lib/instrumentation/modules/http.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ var httpShared = require('../http-shared')
66
var shimmer = require('../shimmer')
77

88
module.exports = function (http, agent, { enabled }) {
9+
if (agent._conf.monitorIncomingHTTPRequests) {
10+
agent.logger.debug('shimming http.Server.prototype.emit function')
11+
shimmer.wrap(http && http.Server && http.Server.prototype, 'emit', httpShared.instrumentRequest(agent, 'http'))
12+
13+
agent.logger.debug('shimming http.ServerResponse.prototype.writeHead function')
14+
shimmer.wrap(http && http.ServerResponse && http.ServerResponse.prototype, 'writeHead', wrapWriteHead)
15+
}
16+
917
if (!enabled) return http
10-
agent.logger.debug('shimming http.Server.prototype.emit function')
11-
shimmer.wrap(http && http.Server && http.Server.prototype, 'emit', httpShared.instrumentRequest(agent, 'http'))
1218

1319
agent.logger.debug('shimming http.request function')
1420
shimmer.wrap(http, 'request', httpShared.traceOutgoingRequest(agent, 'http', 'request'))
@@ -18,9 +24,6 @@ module.exports = function (http, agent, { enabled }) {
1824
shimmer.wrap(http, 'get', httpShared.traceOutgoingRequest(agent, 'http', 'get'))
1925
}
2026

21-
agent.logger.debug('shimming http.ServerResponse.prototype.writeHead function')
22-
shimmer.wrap(http && http.ServerResponse && http.ServerResponse.prototype, 'writeHead', wrapWriteHead)
23-
2427
return http
2528

2629
function wrapWriteHead (original) {

lib/instrumentation/modules/http2.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ var shimmer = require('../shimmer')
88
var symbols = require('../../symbols')
99

1010
module.exports = function (http2, agent, { enabled }) {
11+
if (agent._conf.monitorIncomingHTTPRequests) {
12+
agent.logger.debug('shimming http2.createServer function')
13+
shimmer.wrap(http2, 'createServer', wrapCreateServer)
14+
shimmer.wrap(http2, 'createSecureServer', wrapCreateServer)
15+
}
16+
1117
if (!enabled) return http2
1218
var ins = agent._instrumentation
13-
agent.logger.debug('shimming http2.createServer function')
14-
shimmer.wrap(http2, 'createServer', wrapCreateServer)
15-
shimmer.wrap(http2, 'createSecureServer', wrapCreateServer)
19+
agent.logger.debug('shimming http2.connect function')
1620
shimmer.wrap(http2, 'connect', wrapConnect)
1721

1822
return http2

lib/instrumentation/modules/https.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ var httpShared = require('../http-shared')
66
var shimmer = require('../shimmer')
77

88
module.exports = function (https, agent, { version, enabled }) {
9-
if (!enabled) return https
10-
agent.logger.debug('shimming https.Server.prototype.emit function')
11-
shimmer.wrap(https && https.Server && https.Server.prototype, 'emit', httpShared.instrumentRequest(agent, 'https'))
9+
if (agent._conf.monitorIncomingHTTPRequests) {
10+
agent.logger.debug('shimming https.Server.prototype.emit function')
11+
shimmer.wrap(https && https.Server && https.Server.prototype, 'emit', httpShared.instrumentRequest(agent, 'https'))
12+
}
1213

14+
if (!enabled) return https
1315
// From Node.js v9.0.0 and onwards, https requests no longer just call the
1416
// http.request function. So to correctly instrument outgoing HTTPS requests
1517
// in all supported Node.js versions, we'll only only instrument the

test/instrumentation/_agent.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
var config = require('../../lib/config')
34
var Metrics = require('../../lib/metrics')
45
var Instrumentation = require('../../lib/instrumentation')
56
var mockClient = require('../_mock_http_client')
@@ -12,27 +13,14 @@ var sharedInstrumentation
1213

1314
module.exports = function mockAgent (expected, cb) {
1415
var agent = {
15-
_conf: {
16-
serviceName: 'service-name',
17-
active: true,
18-
instrument: true,
19-
captureSpanStackTraces: true,
20-
errorOnAbortedRequests: false,
16+
_conf: config({
2117
abortedErrorThreshold: 0.25,
22-
sourceLinesErrorAppFrames: 5,
23-
sourceLinesErrorLibraryFrames: 5,
24-
sourceLinesSpanAppFrames: 5,
25-
sourceLinesSpanLibraryFrames: 0,
26-
ignoreUrlStr: [],
27-
ignoreUrlRegExp: [],
28-
ignoreUserAgentStr: [],
29-
ignoreUserAgentRegExp: [],
30-
transactionSampleRate: 1.0,
31-
disableInstrumentations: [],
32-
captureHeaders: true,
33-
metricsInterval: 0,
34-
centralConfig: false
35-
},
18+
asyncHooks: false,
19+
breakdownMetrics: false,
20+
centralConfig: false,
21+
errorOnAbortedRequests: false,
22+
metricsInterval: 0
23+
}),
3624
_errorFilters: new Filters(),
3725
_transactionFilters: new Filters(),
3826
_spanFilters: new Filters(),

test/instrumentation/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var agent = require('../..').start({
44
serviceName: 'test',
5+
breakdownMetrics: false,
56
captureExceptions: false,
67
metricsInterval: 0
78
})

test/instrumentation/modules/http/aborted-requests-enabled.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ test('client-side abort below error threshold - call end', function (t) {
2424
t.equal(agent._transport._writes.length, 0, 'should not have any samples to begin with')
2525

2626
agent.captureError = function (err, opts) { // eslint-disable-line handle-callback-err
27+
console.log(err)
2728
t.fail('should not register the closed socket as an error')
2829
}
2930
agent._instrumentation.addEndedTransaction = function () {

0 commit comments

Comments
 (0)