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

Commit 54d5ff4

Browse files
committed
Support emitting distribution metrics alongside or instead of histograms
1 parent e6c82cc commit 54d5ff4

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ All options are optional.
2323
* `protocol` *boolean* include protocol tag. `default = false`
2424
* `response_code` *boolean* include http response codes. `default = false`
2525
* `delim` *string* char to replace pipe char with in the route `default = '-'`
26+
* `timing_type` *string* the type of metric to emit `histogram`, `distribution`, or `both`. `default = 'histogram'`
2627

2728
## License
2829

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

lib/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = function (options) {
1010
let protocol = options.protocol || false;
1111
let response_code = options.response_code || false;
1212
let DELIM = options.delim || '-';
13+
let timing_type = options.timing_type || 'histogram';
1314
let REGEX_PIPE = /\|/g;
1415

1516
/**
@@ -44,7 +45,7 @@ module.exports = function (options) {
4445
res.end(chunk, encoding);
4546

4647
let statTags = [...tags];
47-
48+
4849
const route = getRoute(req, base_url);
4950
if (route.length > 0) {
5051
statTags.push(`route:${route}`);
@@ -68,7 +69,13 @@ module.exports = function (options) {
6869
datadog.increment(`${stat}.response_code.all`, 1, statTags);
6970
}
7071

71-
datadog.histogram(`${stat}.response_time`, new Date() - req._startTime, 1, statTags);
72+
if (['histogram', 'both'].includes(timing_type)) {
73+
datadog.histogram(`${stat}.response_time`, new Date() - req._startTime, 1, statTags);
74+
}
75+
76+
if (['distribution', 'both'].includes(timing_type)) {
77+
datadog.distribution(`${stat}.dist.response_time`, new Date() - req._startTime, 1, statTags);
78+
}
7279
};
7380

7481
next();

lib/index.test.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const connectDatadog = require("./index");
22

33
const mockStatsDImplementation = {
44
histogram: jest.fn(),
5+
distribution: jest.fn(),
56
increment: jest.fn(),
67
}
78

@@ -35,6 +36,7 @@ describe('connectDatadog', () => {
3536

3637
afterEach(() => {
3738
mockStatsDImplementation.histogram.mockReset()
39+
mockStatsDImplementation.distribution.mockReset()
3840
mockStatsDImplementation.increment.mockReset()
3941
})
4042

@@ -101,14 +103,29 @@ describe('connectDatadog', () => {
101103
let dogstatsd
102104

103105
beforeEach(() => {
104-
dogstatsd = { histogram: jest.fn() }
106+
dogstatsd = {
107+
histogram: jest.fn(),
108+
distribution: jest.fn(),
109+
}
105110
})
106111

107112
it('gets a value for the dogstatsd option', async () => {
108113
await asyncConnectDatadogAndCallMiddleware({ dogstatsd })
109114
expect(dogstatsd.histogram).toHaveBeenCalled()
110115
})
111116

117+
it('uses distribution when specified', async () => {
118+
await asyncConnectDatadogAndCallMiddleware({ dogstatsd, timing_type: 'distribution' });
119+
expect(dogstatsd.histogram).not.toHaveBeenCalled();
120+
expect(dogstatsd.distribution).toHaveBeenCalled();
121+
})
122+
123+
it('uses both timing types when specified', async () => {
124+
await asyncConnectDatadogAndCallMiddleware({ dogstatsd, timing_type: 'both' });
125+
expect(dogstatsd.histogram).toHaveBeenCalled();
126+
expect(dogstatsd.distribution).toHaveBeenCalled();
127+
})
128+
112129
it('uses the default value for the dogstatsd option if not passed', async () => {
113130
await asyncConnectDatadogAndCallMiddleware({})
114131
expect(mockStatsDImplementation.histogram).toHaveBeenCalled()
@@ -213,7 +230,7 @@ describe('connectDatadog', () => {
213230
`route:${req.baseUrl}`,
214231
`response_code:${res.statusCode}`,
215232
]
216-
233+
217234
await asyncConnectDatadogAndCallMiddleware({ base_url: true, response_code: true })
218235
expectConnectedToDatadog(stat, statTags, false)
219236
expect(next).toHaveBeenCalled()
@@ -227,7 +244,7 @@ describe('connectDatadog', () => {
227244
const statTags = [
228245
`response_code:${res.statusCode}`,
229246
]
230-
247+
231248
await asyncConnectDatadogAndCallMiddleware({ response_code: true })
232249
expectConnectedToDatadog(stat, statTags, false)
233250
expect(next).toHaveBeenCalled()

0 commit comments

Comments
 (0)