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

Support emitting distribution metrics alongside or instead of histograms #20

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ All options are optional.
* `protocol` *boolean* include protocol tag. `default = false`
* `response_code` *boolean* include http response codes. `default = false`
* `delim` *string* char to replace pipe char with in the route `default = '-'`
* `timing_type` *string* the type of metric to emit `histogram`, `distribution`, or `both`. `default = 'histogram'`

## License

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

11 changes: 9 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = function (options) {
let protocol = options.protocol || false;
let response_code = options.response_code || false;
let DELIM = options.delim || '-';
let timing_type = options.timing_type || 'histogram';
let REGEX_PIPE = /\|/g;

/**
Expand Down Expand Up @@ -44,7 +45,7 @@ module.exports = function (options) {
res.end(chunk, encoding);

let statTags = [...tags];

const route = getRoute(req, base_url);
if (route.length > 0) {
statTags.push(`route:${route}`);
Expand All @@ -68,7 +69,13 @@ module.exports = function (options) {
datadog.increment(`${stat}.response_code.all`, 1, statTags);
}

datadog.histogram(`${stat}.response_time`, new Date() - req._startTime, 1, statTags);
if (['histogram', 'both'].includes(timing_type)) {
datadog.histogram(`${stat}.response_time`, new Date() - req._startTime, 1, statTags);
}

if (['distribution', 'both'].includes(timing_type)) {
datadog.distribution(`${stat}.dist.response_time`, new Date() - req._startTime, 1, statTags);
}
};

next();
Expand Down
23 changes: 20 additions & 3 deletions lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const connectDatadog = require("./index");

const mockStatsDImplementation = {
histogram: jest.fn(),
distribution: jest.fn(),
increment: jest.fn(),
}

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

afterEach(() => {
mockStatsDImplementation.histogram.mockReset()
mockStatsDImplementation.distribution.mockReset()
mockStatsDImplementation.increment.mockReset()
})

Expand Down Expand Up @@ -101,14 +103,29 @@ describe('connectDatadog', () => {
let dogstatsd

beforeEach(() => {
dogstatsd = { histogram: jest.fn() }
dogstatsd = {
histogram: jest.fn(),
distribution: jest.fn(),
}
})

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

it('uses distribution when specified', async () => {
await asyncConnectDatadogAndCallMiddleware({ dogstatsd, timing_type: 'distribution' });
expect(dogstatsd.histogram).not.toHaveBeenCalled();
expect(dogstatsd.distribution).toHaveBeenCalled();
})

it('uses both timing types when specified', async () => {
await asyncConnectDatadogAndCallMiddleware({ dogstatsd, timing_type: 'both' });
expect(dogstatsd.histogram).toHaveBeenCalled();
expect(dogstatsd.distribution).toHaveBeenCalled();
})

it('uses the default value for the dogstatsd option if not passed', async () => {
await asyncConnectDatadogAndCallMiddleware({})
expect(mockStatsDImplementation.histogram).toHaveBeenCalled()
Expand Down Expand Up @@ -213,7 +230,7 @@ describe('connectDatadog', () => {
`route:${req.baseUrl}`,
`response_code:${res.statusCode}`,
]

await asyncConnectDatadogAndCallMiddleware({ base_url: true, response_code: true })
expectConnectedToDatadog(stat, statTags, false)
expect(next).toHaveBeenCalled()
Expand All @@ -227,7 +244,7 @@ describe('connectDatadog', () => {
const statTags = [
`response_code:${res.statusCode}`,
]

await asyncConnectDatadogAndCallMiddleware({ response_code: true })
expectConnectedToDatadog(stat, statTags, false)
expect(next).toHaveBeenCalled()
Expand Down