Skip to content

Commit 0fad0ad

Browse files
committed
feat(ignore): requests can be ignored based on status codes
1 parent 14b686b commit 0fad0ad

File tree

6 files changed

+100
-9
lines changed

6 files changed

+100
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ module.exports = {
5656
},
5757
ignorePaths: [
5858
'/healthcheck'
59+
],
60+
ignoreStatusCodes: [
61+
401,
62+
404
5963
]
6064
}
6165
```

example/config/trace.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@
1010
},
1111
ignorePaths: [
1212
'/healtcheck'
13+
],
14+
ignoreStatusCodes: [
15+
401,
16+
403
1317
]
1418
}

lib/agent/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ Agent.prototype.serverSend = function (data) {
137137
this.reservoirSampler.addReturnsSuccess(span)
138138
}
139139

140-
this.rpmMetrics.addResponseTime(data.responseTime)
141-
this.rpmMetrics.addStatusCode(data.statusCode)
142140
delete this.partials[data.id]
143141
}
144142

@@ -362,6 +360,10 @@ Agent.prototype.getSpanId = function () {
362360
return this.cls.get(SPAN_ID)
363361
}
364362

363+
Agent.prototype.clearTransaction = function (requestId) {
364+
delete this.partials[requestId]
365+
}
366+
365367
Agent.prototype.bind = function (fn) {
366368
return this.cls.bind(fn)
367369
}

lib/agent/index.spec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ describe('The Trace agent', function () {
147147
mustCollect: '1'
148148
})
149149

150-
expect(rpmMetrics.addResponseTime).to.have.been.calledWith(responseTime)
151-
expect(rpmMetrics.addStatusCode).to.have.been.calledWith(statusCode)
152150
expect(agent.totalRequestCount).to.eql(1)
153151
expect(agent.partials).to.eql({})
154152
expect(agent.reservoirSampler.getItems()).to.eql([{
@@ -310,6 +308,15 @@ describe('The Trace agent', function () {
310308
})
311309
})
312310

311+
it('clears a transaction', function () {
312+
agent.partials[transactionId] = {
313+
data: 'something'
314+
}
315+
316+
agent.clearTransaction(transactionId)
317+
expect(agent.partials).to.eql({})
318+
})
319+
313320
it('passes sample and span data to the API client', function () {
314321
agent.reservoirSampler.addReturnsSuccess(1)
315322
agent.totalRequestCount = 6

lib/instrumentations/core/http/server.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,25 @@ function isPathIgnored (ignorePaths, currentPath) {
1919
return false
2020
}
2121

22+
function isStatusCodeIgnored (ignoreStatusCodes, statusCode) {
23+
if (!ignoreStatusCodes || !ignoreStatusCodes.length) {
24+
return false
25+
}
26+
27+
for (var i = 0; i < ignoreStatusCodes.length; i++) {
28+
if (statusCode === ignoreStatusCodes[i]) {
29+
return true
30+
}
31+
}
32+
33+
return false
34+
}
35+
2236
function wrapListener (listener, agent, mustCollectStore) {
2337
var config = agent.getConfig()
2438
var ignoreHeaders = config.ignoreHeaders
2539
var ignorePaths = config.ignorePaths
40+
var ignoreStatusCodes = config.ignoreStatusCodes
2641

2742
return function (request, response) {
2843
var requestUrl = request.url.split('?')[0]
@@ -60,7 +75,7 @@ function wrapListener (listener, agent, mustCollectStore) {
6075
var method = request.method
6176
serverReceiveTime = microtime.now()
6277

63-
var collectorDataBag = {
78+
var serverReceiveData = {
6479
id: requestId,
6580
spanId: spanId,
6681
host: headers.host,
@@ -73,7 +88,7 @@ function wrapListener (listener, agent, mustCollectStore) {
7388
}
7489

7590
// Collect request start
76-
agent.serverReceive(collectorDataBag)
91+
agent.serverReceive(serverReceiveData)
7792

7893
var serverSendTime
7994
/*
@@ -82,7 +97,15 @@ function wrapListener (listener, agent, mustCollectStore) {
8297
function instrumentedFinish () {
8398
var responseTime = serverSendTime - serverReceiveTime
8499

85-
var collectorDataBag = {
100+
agent.rpmMetrics.addResponseTime(responseTime)
101+
agent.rpmMetrics.addStatusCode(response.statusCode)
102+
103+
if (isStatusCodeIgnored(ignoreStatusCodes, response.statusCode)) {
104+
agent.clearTransaction(requestId)
105+
return debug('statusCode %s is ignored', response.statusCode)
106+
}
107+
108+
var serverSendData = {
86109
mustCollect: mustCollectStore[requestId],
87110
id: requestId,
88111
spanId: headers['x-span-id'],
@@ -96,7 +119,7 @@ function wrapListener (listener, agent, mustCollectStore) {
96119

97120
// Collect request ended
98121
debug('trace event (ss); request: %s, request finished', requestId)
99-
agent.serverSend(collectorDataBag)
122+
agent.serverSend(serverSendData)
100123
delete mustCollectStore[requestId]
101124
}
102125

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ describe('The http.Server.prototype wrapper module', function () {
5757
serverReceive: this.sandbox.spy(),
5858
bind: function () {
5959
return this.sandbox.spy()
60-
}.bind(this)
60+
}.bind(this),
61+
clearTransaction: this.sandbox.spy(),
62+
rpmMetrics: {
63+
addResponseTime: this.sandbox.spy(),
64+
addStatusCode: this.sandbox.spy()
65+
}
6166
}
6267
})
6368

@@ -97,6 +102,48 @@ describe('The http.Server.prototype wrapper module', function () {
97102
})
98103
})
99104

105+
it('skips the request if the statusCode is a match', function () {
106+
config.ignoreStatusCodes = [
107+
401
108+
]
109+
var s = server(original, agent, mustCollectStore)
110+
var writeHeadSpy = this.sandbox.spy()
111+
var setHeaderSpy = this.sandbox.spy()
112+
var cb
113+
114+
var request = {
115+
headers: {
116+
host: 'localhost'
117+
},
118+
pathname: '/',
119+
url: '/healthcheck',
120+
method: 'POST'
121+
}
122+
var response = {
123+
writeHead: writeHeadSpy,
124+
// it is ugly, let's make it better later
125+
once: function (name, _cb) {
126+
if (name === 'finish') {
127+
cb = _cb
128+
}
129+
},
130+
setHeader: setHeaderSpy,
131+
statusCode: 401
132+
}
133+
134+
this.sandbox.stub(microtime, 'now').returns(12345678)
135+
136+
s(request, response)
137+
138+
response.writeHead()
139+
cb()
140+
141+
expect(agent.clearTransaction).to.be.calledWith(transactionId)
142+
expect(agent.rpmMetrics.addResponseTime).to.be.calledWith(0)
143+
expect(agent.rpmMetrics.addStatusCode).to.be.calledWith(401)
144+
expect(agent.serverSend).not.to.have.been.called
145+
})
146+
100147
it('skips requests if there is an *', function () {
101148
config.ignoreHeaders = {
102149
'user-agent': '*'
@@ -177,6 +224,8 @@ describe('The http.Server.prototype wrapper module', function () {
177224

178225
expect(setHeaderSpy).to.be.calledWith('x-server-send', 12345678)
179226
expect(setHeaderSpy).to.be.calledWith('x-parent', 0)
227+
expect(agent.rpmMetrics.addResponseTime).to.be.calledWith(0)
228+
expect(agent.rpmMetrics.addStatusCode).to.be.calledWith(statusCode)
180229
expect(writeHeadSpy).to.be.calledOnce
181230
})
182231

@@ -244,6 +293,8 @@ describe('The http.Server.prototype wrapper module', function () {
244293
expect(setHeaderSpy).to.be.calledWith('x-parent', 0)
245294
expect(setHeaderSpy).to.be.calledWith('x-span-id', spanId)
246295
expect(setHeaderSpy).to.be.calledWith('x-must-collect', '1')
296+
expect(agent.rpmMetrics.addResponseTime).to.be.calledWith(0)
297+
expect(agent.rpmMetrics.addStatusCode).to.be.calledWith(statusCode)
247298
expect(writeHeadSpy).to.be.calledOnce
248299
})
249300
})

0 commit comments

Comments
 (0)