Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Commit 78bf7ee

Browse files
committed
✨ add ability to set route specific tags
1 parent e6c82cc commit 78bf7ee

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

README.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@ Datadog middleware for Connect JS / Express
77

88
Add middleware immediately before your router.
99

10-
app.use(require("connect-datadog")({}));
11-
app.use(app.router);
10+
```javascript
11+
app.use(require("connect-datadog")({}));
12+
app.use(app.router);
13+
```
14+
15+
You can add specific tags in other middlewares:
16+
17+
```javascript
18+
const someMiddleware = (req, res, next) => {
19+
req.ddTags = ['foo:bar']
20+
next()
21+
}
22+
```
1223

1324
## Options
1425

@@ -27,4 +38,3 @@ All options are optional.
2738
## License
2839

2940
View the [LICENSE](https://github.com/AppPress/node-connect-datadog/blob/master/LICENSE) file.
30-

lib/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ module.exports = function (options) {
4141
let end = res.end;
4242
res.end = function (chunk, encoding) {
4343
res.end = end;
44-
res.end(chunk, encoding);
44+
res.end(chunk, encoding);
45+
46+
const routeTags = req.ddTags || [];
47+
48+
let statTags = [...tags, ...routeTags];
4549

46-
let statTags = [...tags];
47-
4850
const route = getRoute(req, base_url);
4951
if (route.length > 0) {
5052
statTags.push(`route:${route}`);

lib/index.test.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ describe('connectDatadog', () => {
3838
mockStatsDImplementation.increment.mockReset()
3939
})
4040

41-
const asyncConnectDatadogAndCallMiddleware = (options, timeoutInterval = 0) =>
41+
const asyncConnectDatadogAndCallMiddleware = (options, timeoutInterval = 0, routeTags = undefined) =>
4242
new Promise(resolve => {
4343
const middleware = connectDatadog(options)
44+
req.ddTags = routeTags
4445
middleware(req, res, next)
4546

4647
timeoutInterval > 0
@@ -167,6 +168,20 @@ describe('connectDatadog', () => {
167168
expectConnectedToDatadog(stat, statTags)
168169
})
169170
})
171+
172+
describe('when a later middleware add a tag', () => {
173+
it('appends the list of tags to the metric tag', async () => {
174+
const stat = 'node.express.router'
175+
const routeTags = ['foo:bar']
176+
const statTags = [
177+
`route:${req.route.path}`,
178+
`response_code:${res.statusCode}`,
179+
]
180+
181+
await asyncConnectDatadogAndCallMiddleware({ response_code: true }, 0, routeTags)
182+
expectConnectedToDatadog(stat, [...routeTags, ...statTags])
183+
})
184+
})
170185
})
171186

172187
describe('path', () => {

0 commit comments

Comments
 (0)