Skip to content

Commit

Permalink
Merge pull request #201 from dvd-z/fix-format-date-number
Browse files Browse the repository at this point in the history
Fix formatDate ignoring number primitives
  • Loading branch information
bdeitte authored Apr 4, 2021
2 parents 4fbf70f + 405b6c4 commit fe8555a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function formatDate(date) {
if (date instanceof Date) {
// Datadog expects seconds.
timestamp = Math.round(date.getTime() / 1000);
} else if (date instanceof Number) {
} else if (date instanceof Number || typeof date === 'number') {
// Make sure it is an integer, not a float.
timestamp = Math.round(date);
}
Expand Down
22 changes: 22 additions & 0 deletions test/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ describe('#event', () => {
});
});

it('should send proper event format for title, text, and options for primitive dates', done => {
const date = Math.round(Date.now() / 1000);
const dateAsDate = new Date(date);
server = createServer(serverType, opts => {
statsd = createHotShotsClient(opts, clientType);
const options = {
date_happened: date,
hostname: 'host',
aggregation_key: 'ag_key',
priority: 'low',
source_type_name: 'source_type',
alert_type: 'warning'
};
statsd.event('test title', 'another\nmultiline\ndescription', options);
});
server.on('metrics', event => {
assert.strictEqual(event, `_e{10,31}:test title|another\\nmultiline\\ndescription|d:${dateAsDate.getTime()}|h:host|k:ag_key|p:low|s:source_type|t:warning${metricEnd}`
);
done();
});
});

it('should send proper event format for title, text, some options, and tags', done => {
server = createServer(serverType, opts => {
statsd = createHotShotsClient(opts, clientType);
Expand Down

0 comments on commit fe8555a

Please sign in to comment.