Browse Source

Merge pull request #36 from brettlangdon/bug/metric_type.sqwished

Fix sending of metric_type for dogapi.metric.send
pull/37/head
Brett Langdon 10 years ago
parent
commit
604b5a3649
2 changed files with 148 additions and 9 deletions
  1. +13
    -3
      lib/api/metric.js
  2. +135
    -6
      test/api/metric.js

+ 13
- 3
lib/api/metric.js View File

@ -12,7 +12,7 @@ var client = require("../client");
* optional, object which can contain the following keys * optional, object which can contain the following keys
* * host: the host source of the metric * * host: the host source of the metric
* * tags: array of "tag:value"'s to use for the metric * * tags: array of "tag:value"'s to use for the metric
* * metric_type: which metric type to use ("gauge" or "counter") [default: gauge]
* * metric_type|type: which metric type to use ("gauge" or "counter") [default: gauge]
* callback: | * callback: |
* function(err, res) * function(err, res)
*example: | *example: |
@ -33,6 +33,9 @@ var client = require("../client");
* dogapi.metric.send("my.metric", [[now, 1000]], function(err, results){ * dogapi.metric.send("my.metric", [[now, 1000]], function(err, results){
* console.dir(results); * console.dir(results);
* }); * });
* dogapi.metric.send("my.counter", 5, {type: "counter"}, function(err, results){
* console.dir(results);
* });
* ``` * ```
*/ */
function send(metric, points, extra, callback){ function send(metric, points, extra, callback){
@ -47,7 +50,8 @@ function send(metric, points, extra, callback){
points: points, points: points,
host: extra.host, host: extra.host,
tags: extra.tags, tags: extra.tags,
metric_type: extra.metric_type
// DEV: For backwards compatibility, allow `metric_type`
type: extra.type || extra.metric_type
} }
]; ];
@ -64,7 +68,7 @@ function send(metric, points, extra, callback){
* * 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]]`) * * 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 * * tags: an array of "tag:value"'s
* * host: the source hostname to use for the metrics * * host: the source hostname to use for the metrics
* * metric_type: the type of metric to use ("gauge" or "counter") [default: gauge]
* * metric_type|type: the type of metric to use ("gauge" or "counter") [default: gauge]
* callback: | * callback: |
* function(err, res) * function(err, res)
*example: | *example: |
@ -121,6 +125,12 @@ function send_all(metrics, callback){
}); });
metrics[i].points = points; metrics[i].points = points;
// DEV: Change `metric_type` to `type` for backwards compatibility
metrics[i].type = metrics[i].type || metrics[i].metric_type;
// Remove `metric_type` if it was set
// DEV: This will not cause an error if `metric_type` does not exist
delete metrics[i].metric_type;
} }
var params = { var params = {


+ 135
- 6
test/api/metric.js View File

@ -29,8 +29,8 @@ describe("api/metrics", function(){
assert.equal(call_args[1], "/series"); assert.equal(call_args[1], "/series");
// Properly formatted body // Properly formatted body
// { body: series: [ {metric: "metric.send", host: undefined, tags: undefined, metric_type: undefined} ] }
// DEV: host/tags/metric_type are optional and should be undefined for this case
// { body: series: [ {metric: "metric.send", host: undefined, tags: undefined, type: undefined} ] }
// DEV: host/tags/type are optional and should be undefined for this case
var data = call_args[2]; var data = call_args[2];
assert(data.hasOwnProperty("body")); assert(data.hasOwnProperty("body"));
assert(data.body.hasOwnProperty("series")); assert(data.body.hasOwnProperty("series"));
@ -53,8 +53,8 @@ describe("api/metrics", function(){
assert.equal(first_series.host, undefined); assert.equal(first_series.host, undefined);
assert(first_series.hasOwnProperty("tags")); assert(first_series.hasOwnProperty("tags"));
assert.equal(first_series.tags, undefined); assert.equal(first_series.tags, undefined);
assert(first_series.hasOwnProperty("metric_type"));
assert.equal(first_series.metric_type, undefined);
assert(first_series.hasOwnProperty("type"));
assert.equal(first_series.type, undefined);
}); });
it("should properly normalize values to points", function(){ it("should properly normalize values to points", function(){
@ -131,6 +131,57 @@ describe("api/metrics", function(){
assert(Array.isArray(point)); assert(Array.isArray(point));
assert.deepEqual(point, [now, 1000]); assert.deepEqual(point, [now, 1000]);
}); });
it("should properly set metric type", function(){
// Make our api call
metric.send("metrics.send.counter", 5, {type: "counter"});
// 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);
// Assert we have only 1 series
// series = [ {metric: "", ...}, ... ]
var series = body.series;
assert(Array.isArray(series));
assert.equal(series.length, 1);
// Assert the first series is properly formatted
// first_series = {metric: "", type: "counter", points: [], ...}
var first_series = series[0]
assert.equal(first_series.metric, "metrics.send.counter");
assert(first_series.hasOwnProperty("type"));
assert.equal(first_series.type, "counter");
});
it("should properly set convert metric_type to type", function(){
// Make our api call
metric.send("metrics.send.counter", 5, {metric_type: "counter"});
// 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);
// Assert we have only 1 series
// series = [ {metric: "", ...}, ... ]
var series = body.series;
assert(Array.isArray(series));
assert.equal(series.length, 1);
// Assert the first series is properly formatted
// first_series = {metric: "", type: "counter", points: [], ...}
var first_series = series[0]
assert.equal(first_series.metric, "metrics.send.counter");
assert(first_series.hasOwnProperty("type"));
assert.equal(first_series.type, "counter");
});
}); });
describe("#send_all", function(){ describe("#send_all", function(){
@ -153,8 +204,8 @@ describe("api/metrics", function(){
assert.equal(call_args[1], "/series"); assert.equal(call_args[1], "/series");
// Properly formatted body // 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
// { body: series: [ {metric: "metric.send_all", host: undefined, tags: undefined, type: undefined} ] }
// DEV: host/tags/type are optional and should be undefined for this case
var data = call_args[2]; var data = call_args[2];
assert(data.hasOwnProperty("body")); assert(data.hasOwnProperty("body"));
assert(data.body.hasOwnProperty("series")); assert(data.body.hasOwnProperty("series"));
@ -222,5 +273,83 @@ describe("api/metrics", function(){
assert.equal(points[0].length, 2); assert.equal(points[0].length, 2);
assert.equal(points[0][1], 1000); assert.equal(points[0][1], 1000);
}); });
it("should properly send metric type", function(){
// Make our api call
var metrics = [
{
metric: "metric.send.counter",
points: 5,
type: "counter"
}
];
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.counter", host: undefined, tags: undefined, type: "counter"} ] }
// DEV: host/tags 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: "", type: "counter", points: [], ...}
var first_series = series[0]
assert.equal(first_series.metric, "metric.send.counter");
assert(Array.isArray(first_series.points));
assert.deepEqual(first_series.type, "counter");
});
it("should properly send metric_type as type", function(){
// Make our api call
var metrics = [
{
metric: "metric.send.counter",
points: 5,
metric_type: "counter"
}
];
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.counter", host: undefined, tags: undefined, type: "counter"} ] }
// DEV: host/tags 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: "", type: "counter", points: [], ...}
var first_series = series[0]
assert.equal(first_series.metric, "metric.send.counter");
assert(Array.isArray(first_series.points));
assert.deepEqual(first_series.type, "counter");
});
}); });
}); });

Loading…
Cancel
Save