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

✨ add ability to set route specific tags #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ Datadog middleware for Connect JS / Express

Add middleware immediately before your router.

app.use(require("connect-datadog")({}));
app.use(app.router);
```javascript
app.use(require("connect-datadog")({}));
app.use(app.router);
```

You can add specific tags in other middlewares:

```javascript
const someMiddleware = (req, res, next) => {
req.ddTags = ['foo:bar']
next()
}
```

## Options

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

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

8 changes: 5 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ module.exports = function (options) {
let end = res.end;
res.end = function (chunk, encoding) {
res.end = end;
res.end(chunk, encoding);
res.end(chunk, encoding);

const routeTags = req.ddTags || [];

let statTags = [...tags, ...routeTags];

let statTags = [...tags];

const route = getRoute(req, base_url);
if (route.length > 0) {
statTags.push(`route:${route}`);
Expand Down
17 changes: 16 additions & 1 deletion lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ describe('connectDatadog', () => {
mockStatsDImplementation.increment.mockReset()
})

const asyncConnectDatadogAndCallMiddleware = (options, timeoutInterval = 0) =>
const asyncConnectDatadogAndCallMiddleware = (options, timeoutInterval = 0, routeTags = undefined) =>
new Promise(resolve => {
const middleware = connectDatadog(options)
req.ddTags = routeTags
middleware(req, res, next)

timeoutInterval > 0
Expand Down Expand Up @@ -167,6 +168,20 @@ describe('connectDatadog', () => {
expectConnectedToDatadog(stat, statTags)
})
})

describe('when a later middleware add a tag', () => {
it('appends the list of tags to the metric tag', async () => {
const stat = 'node.express.router'
const routeTags = ['foo:bar']
const statTags = [
`route:${req.route.path}`,
`response_code:${res.statusCode}`,
]

await asyncConnectDatadogAndCallMiddleware({ response_code: true }, 0, routeTags)
expectConnectedToDatadog(stat, [...routeTags, ...statTags])
})
})
})

describe('path', () => {
Expand Down