From 2fba04a355eea874b63aa319123cb0f4319d5f37 Mon Sep 17 00:00:00 2001 From: David Zhou Date: Thu, 1 Apr 2021 15:28:55 -0400 Subject: [PATCH 1/2] Fix formatDate not parsing number primitives --- lib/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/helpers.js b/lib/helpers.js index 4901e35..d14f168 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -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); } From 405b6c4e9fae8fb0d14133dafe454ea5d92cd42a Mon Sep 17 00:00:00 2001 From: David Zhou Date: Sun, 4 Apr 2021 07:45:23 -0400 Subject: [PATCH 2/2] Add test for primitive dates --- test/event.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/event.js b/test/event.js index 94a9253..afa49d0 100644 --- a/test/event.js +++ b/test/event.js @@ -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);