This repository was archived by the owner on Feb 7, 2025. It is now read-only.
File tree 3 files changed +34
-7
lines changed
3 files changed +34
-7
lines changed Original file line number Diff line number Diff line change @@ -7,8 +7,19 @@ Datadog middleware for Connect JS / Express
7
7
8
8
Add middleware immediately before your router.
9
9
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
+ ```
12
23
13
24
## Options
14
25
@@ -27,4 +38,3 @@ All options are optional.
27
38
## License
28
39
29
40
View the [ LICENSE] ( https://github.com/AppPress/node-connect-datadog/blob/master/LICENSE ) file.
30
-
Original file line number Diff line number Diff line change @@ -41,10 +41,12 @@ module.exports = function (options) {
41
41
let end = res . end ;
42
42
res . end = function ( chunk , encoding ) {
43
43
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 ] ;
45
49
46
- let statTags = [ ...tags ] ;
47
-
48
50
const route = getRoute ( req , base_url ) ;
49
51
if ( route . length > 0 ) {
50
52
statTags . push ( `route:${ route } ` ) ;
Original file line number Diff line number Diff line change @@ -38,9 +38,10 @@ describe('connectDatadog', () => {
38
38
mockStatsDImplementation . increment . mockReset ( )
39
39
} )
40
40
41
- const asyncConnectDatadogAndCallMiddleware = ( options , timeoutInterval = 0 ) =>
41
+ const asyncConnectDatadogAndCallMiddleware = ( options , timeoutInterval = 0 , routeTags = undefined ) =>
42
42
new Promise ( resolve => {
43
43
const middleware = connectDatadog ( options )
44
+ req . ddTags = routeTags
44
45
middleware ( req , res , next )
45
46
46
47
timeoutInterval > 0
@@ -167,6 +168,20 @@ describe('connectDatadog', () => {
167
168
expectConnectedToDatadog ( stat , statTags )
168
169
} )
169
170
} )
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
+ } )
170
185
} )
171
186
172
187
describe ( 'path' , ( ) => {
You can’t perform that action at this time.
0 commit comments