From 5c5e54e4859b72eb627b506bcd1ed10d2038622f Mon Sep 17 00:00:00 2001 From: Serphentas Date: Fri, 5 Jan 2018 23:52:54 +0100 Subject: [PATCH 1/3] fix #6 --- lib/GraphiteClient.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/GraphiteClient.js b/lib/GraphiteClient.js index 8ec201c..39c4ad8 100644 --- a/lib/GraphiteClient.js +++ b/lib/GraphiteClient.js @@ -30,12 +30,13 @@ GraphiteClient.flatten = function(obj, flat, prefix) { GraphiteClient.prototype.write = function(metrics, timestamp, cb) { if (typeof timestamp === 'function') { - cb = timestamp; - timestamp = null; + cb = timestamp; + timestamp = undefined; } - timestamp = timestamp || Date.now(); - timestamp = Math.floor(timestamp / 1000); + if (timestamp === undefined) { + timestamp = Math.floor(Date.now() / 1000); + } this._carbon.write(GraphiteClient.flatten(metrics), timestamp, cb); }; From e8a4c2a1d6cc28e59f02a8923d500b012ed178a2 Mon Sep 17 00:00:00 2001 From: Serphentas Date: Sun, 14 Jan 2018 12:23:05 +0100 Subject: [PATCH 2/3] changed GraphiteClient write to handle epoch as milliseconds and add doc accordingly --- README.md | 16 +++++++++++++++- lib/GraphiteClient.js | 17 ++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0a3a4a6..be9ee66 100644 --- a/README.md +++ b/README.md @@ -12,18 +12,32 @@ npm install graphite ## Usage -Sending data: +You first have to define the Graphite client: ```js var graphite = require('graphite'); var client = graphite.createClient('plaintext://graphite.example.org:2003/'); +``` + +You can send metrics without a timestamp. The current Unix epoch will then be used in place: +```js var metrics = {foo: 23}; client.write(metrics, function(err) { // if err is null, your data was sent to graphite! }); ``` +If you wish to set your own timestamp, you must use `Date.now()` (millisecond precision) as parameter and not `Math.floor(Date.now() / 1000)` (second precision), otherwise your metrics will probably get ignored by Graphite: + +```js +var metrics = {foo: 23}; +var timestamp = Date.now(); +client.write(metrics, timestamp, function(err) { + // if err is null, your data was sent to graphite! +}); +``` + ## Todo * More docs diff --git a/lib/GraphiteClient.js b/lib/GraphiteClient.js index 39c4ad8..0cdd749 100644 --- a/lib/GraphiteClient.js +++ b/lib/GraphiteClient.js @@ -28,15 +28,26 @@ GraphiteClient.flatten = function(obj, flat, prefix) { return flat; }; +/** + * Writes the given metrics to the underlying plaintext socket to Graphite + * + * If no timestamp is given, the current Unix epoch is used (second precision). + * + * If a timestamp is provided, it must have a millisecond precision, otherwise + * Graphite will probably reject the data. + * + * @param {object} metrics + * @param {object} timestamp + * @param {function} cb + */ GraphiteClient.prototype.write = function(metrics, timestamp, cb) { if (typeof timestamp === 'function') { cb = timestamp; timestamp = undefined; } - if (timestamp === undefined) { - timestamp = Math.floor(Date.now() / 1000); - } + // cutting timestamp for precision up to the second + timestamp = Math.floor(Date.now() / 1000); this._carbon.write(GraphiteClient.flatten(metrics), timestamp, cb); }; From 0736a8b61f878998e8ca86dc956c7fdcc5742aa9 Mon Sep 17 00:00:00 2001 From: Serphentas Date: Sun, 14 Jan 2018 12:29:09 +0100 Subject: [PATCH 3/3] fix timestamp handling when undefined --- lib/GraphiteClient.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/GraphiteClient.js b/lib/GraphiteClient.js index 0cdd749..30bf610 100644 --- a/lib/GraphiteClient.js +++ b/lib/GraphiteClient.js @@ -43,11 +43,11 @@ GraphiteClient.flatten = function(obj, flat, prefix) { GraphiteClient.prototype.write = function(metrics, timestamp, cb) { if (typeof timestamp === 'function') { cb = timestamp; - timestamp = undefined; + timestamp = Date.now(); } // cutting timestamp for precision up to the second - timestamp = Math.floor(Date.now() / 1000); + timestamp = Math.floor(timestamp / 1000); this._carbon.write(GraphiteClient.flatten(metrics), timestamp, cb); };