Skip to content

Commit 24e2114

Browse files
committed
feat(ignorePath): possibility to filter out urls based on path
1 parent aac5eba commit 24e2114

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ module.exports = {
5353
apiKey: 'KEEP_ME_SECRET',
5454
ignoreHeaders: {
5555
'user-agent': ['007']
56-
}
56+
},
57+
ignorePaths: [
58+
'/healthcheck'
59+
]
5760
}
5861
```
5962

example/config/trace.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
apiKey: 'KEEP_ME_SECRET',
88
ignoreHeaders: {
99
'user-agent': ['007']
10-
}
10+
},
11+
ignorePaths: [
12+
'/healtcheck'
13+
]
1114
}

lib/instrumentations/core/http/server.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,48 @@
1-
var url = require('url')
2-
3-
var qs = require('qs')
41
var microtime = require('../../../optionalDependencies/microtime')
52
var isNumber = require('lodash.isnumber')
63
var debug = require('debug')('risingstack/trace')
74

85
var util = require('./util')
96
var consts = require('../../../consts')
107

8+
function isPathIgnored (ignorePaths, currentPath) {
9+
if (!ignorePaths || !ignorePaths.length) {
10+
return false
11+
}
12+
13+
for (var i = 0; i < ignorePaths.length; i++) {
14+
if (currentPath.match(ignorePaths[i])) {
15+
return true
16+
}
17+
}
18+
19+
return false
20+
}
21+
1122
function wrapListener (listener, agent, mustCollectStore) {
12-
var ignoreHeaders = agent.getConfig().ignoreHeaders
23+
var config = agent.getConfig()
24+
var ignoreHeaders = config.ignoreHeaders
25+
var ignorePaths = config.ignorePaths
1326

1427
return function (request, response) {
28+
var requestUrl = request.url.split('?')[0]
1529
var serverReceiveTime
1630

1731
var headers = request.headers
1832
var spanId = headers['x-span-id']
1933

20-
var skipped = ignoreHeaders && Object.keys(ignoreHeaders).some(function (key) {
34+
var isSkippedHeader = ignoreHeaders && Object.keys(ignoreHeaders).some(function (key) {
2135
return headers[key] && (ignoreHeaders[key].indexOf('*') > -1 || ignoreHeaders[key].indexOf(headers[key]) > -1)
2236
})
2337

24-
if (skipped) {
25-
debug('trace event (sr); request skipped because of ignoreHeaders', headers)
38+
if (isSkippedHeader || isPathIgnored(ignorePaths, requestUrl)) {
39+
debug('trace event (sr); request skipped because of ignore options', headers)
2640
return listener.apply(this, arguments)
2741
}
2842

29-
var requestUrl = url.parse(request.url)
30-
var requestQuery = qs.parse(requestUrl.query).requestId
31-
3243
var originalWriteHead = response.writeHead
3344

34-
var requestId = headers['request-id'] || headers['x-request-id'] || requestQuery || agent.generateId()
45+
var requestId = headers['request-id'] || headers['x-request-id'] || agent.generateId()
3546

3647
debug('trace event (sr); request: %s', requestId, headers)
3748

@@ -53,7 +64,7 @@ function wrapListener (listener, agent, mustCollectStore) {
5364
id: requestId,
5465
spanId: spanId,
5566
host: headers.host,
56-
url: util.formatDataUrl(requestUrl.pathname),
67+
url: util.formatDataUrl(requestUrl),
5768
time: serverReceiveTime,
5869
method: method,
5970
protocol: consts.PROTOCOLS.HTTP,
@@ -76,7 +87,7 @@ function wrapListener (listener, agent, mustCollectStore) {
7687
id: requestId,
7788
spanId: headers['x-span-id'],
7889
host: headers.host,
79-
url: util.formatDataUrl(requestUrl.pathname),
90+
url: util.formatDataUrl(requestUrl),
8091
time: serverSendTime,
8192
protocol: consts.PROTOCOLS.HTTP,
8293
statusCode: response.statusCode,

lib/instrumentations/core/http/server.spec.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,32 @@ describe('The http.Server.prototype wrapper module', function () {
6868
s({
6969
headers: {
7070
'user-agent': '007'
71-
}
71+
},
72+
url: '/'
7273
})
7374

7475
expect(original).to.be.calledWith({
7576
headers: {
7677
'user-agent': '007'
77-
}
78+
},
79+
url: '/'
80+
})
81+
})
82+
83+
it('skips the request if the path is a match', function () {
84+
config.ignorePaths = [
85+
'/healthcheck'
86+
]
87+
var s = server(original, agent, mustCollectStore)
88+
89+
s({
90+
url: '/healthcheck',
91+
headers: {}
92+
})
93+
94+
expect(original).to.be.calledWith({
95+
url: '/healthcheck',
96+
headers: {}
7897
})
7998
})
8099

@@ -87,13 +106,15 @@ describe('The http.Server.prototype wrapper module', function () {
87106
s({
88107
headers: {
89108
'user-agent': '007'
90-
}
109+
},
110+
url: '/'
91111
})
92112

93113
expect(original).to.be.calledWith({
94114
headers: {
95115
'user-agent': '007'
96-
}
116+
},
117+
url: '/'
97118
})
98119
})
99120

@@ -108,7 +129,7 @@ describe('The http.Server.prototype wrapper module', function () {
108129
host: 'localhost'
109130
},
110131
pathname: '/',
111-
url: '/?id=2',
132+
url: '/?id=1',
112133
method: 'POST'
113134
}
114135
var response = {
@@ -172,7 +193,7 @@ describe('The http.Server.prototype wrapper module', function () {
172193
'x-must-collect': '1'
173194
},
174195
pathname: '/',
175-
url: '/?id=2',
196+
url: '/',
176197
method: 'POST'
177198
}
178199
var response = {

0 commit comments

Comments
 (0)