Comparing version 1.1.0 to 2.0.0
var client = require("../client"); | ||
/*section: metric | ||
@@ -10,4 +9,4 @@ *comment: | | ||
* points: | | ||
* single datapoint or array of [timestamp, datapoint], if a single point | ||
* is given "now" is used as the timestamp | ||
* a single data point (e.g. `50`), an array of data points (e.g. `[50, 100]`) | ||
* or an array of `[timestamp, value]` elements (e.g. `[[now, 50], [now, 100]]`) | ||
* extra: | | ||
@@ -31,4 +30,7 @@ * optional, object which can contain the following keys | ||
* }); | ||
* dogapi.metric.send("my.metric", [500, 1000], function(err, results){ | ||
* console.dir(results); | ||
* }); | ||
* var now = parseInt(new Date().getTime() / 1000); | ||
* dogapi.metric.send("my.metric", [now, 1000], function(err, results){ | ||
* dogapi.metric.send("my.metric", [[now, 1000]], function(err, results){ | ||
* console.dir(results); | ||
@@ -43,19 +45,2 @@ * }); | ||
} | ||
// Try to normalize `points` | ||
// DEV: We need `points` to be an array of arrays regardless of what they give us | ||
// Always wrap points in an array, this way we will get: | ||
// 500 => [500] | ||
// [<timestamp>, 500] => [[<timestamp>, 500]] | ||
points = [points]; | ||
points = points.map(function(point){ | ||
// Make sure each point is an array, if not make array with current timestamp | ||
// 500 => [<timestamp>, 500] | ||
// [<timestamp>, 500] => unchanged | ||
if(!Array.isArray(point)){ | ||
var now = parseInt(new Date().getTime() / 1000); | ||
point = [now, point]; | ||
} | ||
return point; | ||
}); | ||
extra = extra || {}; | ||
@@ -82,3 +67,3 @@ var series = [ | ||
* * metric: the name of the metric | ||
* * points: a single datapoint or an array of [timestamp, datapoint] (same as `dogapi.metric.send`) | ||
* * points: a single data point (e.g. `50`), an array of data points (e.g. `[50, 100]`) or an array of `[timestamp, value]` elements (e.g. `[[now, 50], [now, 100]]`) | ||
* * tags: an array of "tag:value"'s | ||
@@ -101,3 +86,3 @@ * * host: the source hostname to use for the metrics | ||
* metric: "my.metric", | ||
* points: [now, 1000], | ||
* points: [[now, 1000]], | ||
* tags: ["tag:value"] | ||
@@ -107,2 +92,6 @@ * }, | ||
* metric: "another.metric", | ||
* points: [50, 1000] | ||
* }, | ||
* { | ||
* metric: "another.metric", | ||
* points: 1000 | ||
@@ -119,6 +108,26 @@ * } | ||
for(var i = 0; i < metrics.length; ++i){ | ||
// Try to normalize `points` | ||
// DEV: We need `points` to be an array of arrays regardless of what they give us | ||
// Always wrap points in an array, this way we will get: | ||
// 500 => [500] | ||
// [500, 100] => [[<timestamp>, 500], [<timestamp>, 1000]] | ||
// [[<timestamp>, 500]] => [[<timestamp>, 500]] | ||
points = metrics[i].points | ||
if(!Array.isArray(metrics[i].points)){ | ||
metrics[i].points = [now, metrics[i].points]; | ||
points = [points]; | ||
} | ||
points = points.map(function(point){ | ||
// Make sure each point is an array, if not make array with current timestamp | ||
// 500 => [<timestamp>, 500] | ||
// [<timestamp>, 500] => unchanged | ||
if(!Array.isArray(point)){ | ||
var now = parseInt(new Date().getTime() / 1000); | ||
point = [now, point]; | ||
} | ||
return point; | ||
}); | ||
metrics[i].points = points; | ||
} | ||
var params = { | ||
@@ -125,0 +134,0 @@ body: { |
{ | ||
"name": "dogapi", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Datadog API Node.JS Client", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -103,2 +103,62 @@ dogapi | ||
## Major changes from 1.x.x to 2.x.x | ||
We have updated major versions for this library due to a backwards incompatible change to the argument format for `dogapi.metric.send` and `dogapi.metric.send_all`. | ||
### dogapi.metric.send | ||
Previously in `1.x.x`: | ||
```javascript | ||
var now = parseInt(new Date().getTime() / 1000); | ||
dogapi.metric.send("metric.name", 50); | ||
dogapi.metric.send("metric.name", [now, 50]); | ||
``` | ||
Now in `2.x.x`: | ||
```javascript | ||
var now = parseInt(new Date().getTime() / 1000); | ||
dogapi.metric.send("metric.name", 50); | ||
dogapi.metric.send("metric.name", [50, 100]); | ||
dogapi.metric.send("metric.name", [[now, 50]]); | ||
``` | ||
### dogapi.metric.send_all | ||
Previously in `1.x.x`: | ||
```javascript | ||
var now = parseInt(new Date().getTime() / 1000); | ||
var metrics = [ | ||
{ | ||
metric: "metric.name", | ||
points: [now, 50] | ||
}, | ||
{ | ||
metric: "metric.name", | ||
points: 50 | ||
} | ||
]; | ||
dogapi.metric.send_all(metrics); | ||
``` | ||
Now in `2.x.x`: | ||
```javascript | ||
var now = parseInt(new Date().getTime() / 1000); | ||
var metrics = [ | ||
{ | ||
metric: "metric.name", | ||
points: [[now, 50]] | ||
}, | ||
{ | ||
metric: "metric.name", | ||
points: [50, 100] | ||
}, | ||
{ | ||
metric: "metric.name", | ||
points: 50 | ||
} | ||
]; | ||
dogapi.metric.send_all(metrics); | ||
``` | ||
## License | ||
@@ -105,0 +165,0 @@ |
@@ -22,3 +22,3 @@ var assert = require("assert"); | ||
var now = parseInt(new Date().getTime() / 1000); | ||
metric.send("metric.send", [now, 500]); | ||
metric.send("metric.send", [[now, 500]]); | ||
@@ -83,3 +83,146 @@ // Assert we properly called `client.request` | ||
}); | ||
it("should properly normalize array of values to points", function(){ | ||
// Make our api call | ||
metric.send("metrics.send.normalize", [500, 1000]); | ||
// Assert we called `client.request` with the correct `points` | ||
assert(stub_request.calledOnce); | ||
var call_args = stub_request.getCall(0).args; | ||
// { body: series: [ {points: [], }, ] } | ||
var body = call_args[2].body; | ||
assert.equal(body.series.length, 1); | ||
// points = [ [<timestamp>, 500], [<timestamp>, 1000] ] | ||
var points = body.series[0].points; | ||
assert(Array.isArray(points)); | ||
assert.equal(points.length, 2); | ||
// point = [<timestamp>, 500] | ||
var point = points[0]; | ||
assert(Array.isArray(point)); | ||
assert.equal(point.length, 2); | ||
assert.equal(point[1], 500); | ||
// point = [<timestamp>, 1000] | ||
point = points[1]; | ||
assert(Array.isArray(point)); | ||
assert.equal(point.length, 2); | ||
assert.equal(point[1], 1000); | ||
}); | ||
it("should not normalize correctly formatted points", function(){ | ||
// Make our api call | ||
var now = parseInt(new Date().getTime() / 1000); | ||
metric.send("metrics.send.normalize", [[now, 1000]]); | ||
// Assert we called `client.request` with the correct `points` | ||
assert(stub_request.calledOnce); | ||
var call_args = stub_request.getCall(0).args; | ||
// { body: series: [ {points: [], }, ] } | ||
var body = call_args[2].body; | ||
assert.equal(body.series.length, 1); | ||
// points = [ [<timestamp>, 1000], ] | ||
var points = body.series[0].points; | ||
assert(Array.isArray(points)); | ||
assert.equal(points.length, 1); | ||
// point = [<timestamp>, 500] | ||
var point = points[0]; | ||
assert(Array.isArray(point)); | ||
assert.deepEqual(point, [now, 1000]); | ||
}); | ||
}); | ||
describe("#send_all", function(){ | ||
it("should make a valid api call", function(){ | ||
// Make our api call | ||
var now = parseInt(new Date().getTime() / 1000); | ||
var metrics = [ | ||
{ | ||
metric: "metric.send_all", | ||
points: [[now, 500]] | ||
} | ||
]; | ||
metric.send_all(metrics); | ||
// Assert we properly called `client.request` | ||
assert(stub_request.calledOnce); | ||
var call_args = stub_request.getCall(0).args; | ||
// Method and endpoint are correct | ||
assert.equal(call_args[0], "POST"); | ||
assert.equal(call_args[1], "/series"); | ||
// Properly formatted body | ||
// { body: series: [ {metric: "metric.send_all", host: undefined, tags: undefined, metric_type: undefined} ] } | ||
// DEV: host/tags/metric_type are optional and should be undefined for this case | ||
var data = call_args[2]; | ||
assert(data.hasOwnProperty("body")); | ||
assert(data.body.hasOwnProperty("series")); | ||
// Assert we have only 1 series | ||
// series = [ {metric: "", ...}, ... ] | ||
var series = data.body.series; | ||
assert(Array.isArray(series)); | ||
assert.equal(series.length, 1); | ||
// Assert the first series is properly formatted | ||
// first_series = {metric: "", points: [], ...} | ||
var first_series = series[0] | ||
assert.equal(first_series.metric, "metric.send_all"); | ||
assert(Array.isArray(first_series.points)); | ||
assert.deepEqual(first_series.points, [[now, 500]]); | ||
}); | ||
it("should properly normalize metric points", function(){ | ||
// Make our api call | ||
var now = parseInt(new Date().getTime() / 1000); | ||
var metrics = [ | ||
{ | ||
metric: "metric.send_all.normalize", | ||
points: [[now, 500]] | ||
}, | ||
{ | ||
metric: "metric.send_all.normalize", | ||
points: [500, 1000] | ||
}, | ||
{ | ||
metric: "metric.send_all.normalize", | ||
points: 1000 | ||
} | ||
]; | ||
metric.send_all(metrics); | ||
// Assert we called `client.request` with the correct `points` | ||
assert(stub_request.calledOnce); | ||
var call_args = stub_request.getCall(0).args; | ||
// { body: series: [ {points: [], }, ] } | ||
var body = call_args[2].body; | ||
assert.equal(body.series.length, 3); | ||
// points = [ [<timestamp>, 500] ] | ||
var points = body.series[0].points; | ||
assert(Array.isArray(points)); | ||
assert.equal(points.length, 1); | ||
assert.equal(points[0].length, 2); | ||
assert.equal(points[0][1], 500); | ||
// points = [ [<timestamp>, 500], [<timestamp>, 1000] ] | ||
points = body.series[1].points; | ||
assert(Array.isArray(points)); | ||
assert.equal(points.length, 2); | ||
assert.equal(points[0].length, 2); | ||
assert.equal(points[0][1], 500); | ||
assert.equal(points[1].length, 2); | ||
assert.equal(points[1][1], 1000); | ||
// points = [ [<timestamp>, 1000] ] | ||
points = body.series[2].points; | ||
assert(Array.isArray(points)); | ||
assert.equal(points.length, 1); | ||
assert.equal(points[0].length, 2); | ||
assert.equal(points[0][1], 1000); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
160702
2912
173