Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit 26e75d1

Browse files
lewisvail3rochdev
authored andcommitted
Enabling metrics for requests that don't have a defined route. (#8)
* Enabling metrics for requests that don't have a defined route. * Incrementing version. * Exclude the `route` metric if the derived metric value is an empty string. * Only sending metrics if there are tags associated with the request. * Updating tests. * Updating jest version to support the latest version of Node. * Revert "Only sending metrics if there are tags associated with the request." This reverts commit 3fed74f. * Removing test case that is no longer valid. * Refactoring the code that builds the request route into a separate function.
1 parent 2a32a62 commit 26e75d1

File tree

4 files changed

+3057
-2511
lines changed

4 files changed

+3057
-2511
lines changed

lib/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ module.exports = function (options) {
2727
return str && str.replace(REGEX_PIPE, DELIM);
2828
}
2929

30+
function getRoute(req, base_url) {
31+
const routePath = req.route && req.route.path ? req.route.path : '';
32+
const baseUrl = (base_url !== false) ? req.baseUrl : '';
33+
return baseUrl + replacePipeChar(routePath);
34+
}
35+
3036
return function (req, res, next) {
3137
if (!req._startTime) {
3238
req._startTime = new Date();
@@ -37,13 +43,13 @@ module.exports = function (options) {
3743
res.end = end;
3844
res.end(chunk, encoding);
3945

40-
if (!req.route || !req.route.path) {
41-
return;
46+
let statTags = [...tags];
47+
48+
const route = getRoute(req, base_url);
49+
if (route.length > 0) {
50+
statTags.push(`route:${route}`);
4251
}
4352

44-
const baseUrl = (base_url !== false) ? req.baseUrl : '';
45-
let statTags = [`route:${baseUrl + replacePipeChar(req.route.path)}`].concat(tags);
46-
4753
if (method !== false) {
4854
statTags.push(`method:${req.method.toLowerCase()}`);
4955
}

lib/index.test.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,33 @@ describe('connectDatadog', () => {
205205
})
206206

207207
describe('when the path is not set in the request', () => {
208-
it('should NOT send a response', async () => {
209-
req = {}
208+
describe('when base_url is truthly', () => {
209+
it('uses the base URL as the route', async() => {
210+
delete req.route
211+
const stat = 'node.express.router'
212+
const statTags = [
213+
`route:${req.baseUrl}`,
214+
`response_code:${res.statusCode}`,
215+
]
216+
217+
await asyncConnectDatadogAndCallMiddleware({ base_url: true, response_code: true })
218+
expectConnectedToDatadog(stat, statTags, false)
219+
expect(next).toHaveBeenCalled()
220+
})
221+
})
210222

211-
await asyncConnectDatadogAndCallMiddleware({})
212-
expect(mockStatsDImplementation.increment).not.toHaveBeenCalled()
213-
expect(mockStatsDImplementation.histogram).not.toHaveBeenCalled()
214-
expect(next).toHaveBeenCalled()
223+
describe('when base_url is falsely', () => {
224+
it('route tag is not specified', async() => {
225+
delete req.route
226+
const stat = 'node.express.router'
227+
const statTags = [
228+
`response_code:${res.statusCode}`,
229+
]
230+
231+
await asyncConnectDatadogAndCallMiddleware({ response_code: true })
232+
expectConnectedToDatadog(stat, statTags, false)
233+
expect(next).toHaveBeenCalled()
234+
})
215235
})
216236
})
217237
})

0 commit comments

Comments
 (0)