Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
Add tests for writeToLegacy and top level tags
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanmikaelian committed Apr 25, 2017
1 parent 078b4bf commit f54c4de
Showing 1 changed file with 121 additions and 1 deletion.
122 changes: 121 additions & 1 deletion test/librato_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const config = {
},
};

module.exports = {
module.exports.tags = {
setUp: function(callback) {
this.emitter = new events.EventEmitter();

Expand Down Expand Up @@ -190,4 +190,124 @@ module.exports = {

this.emitter.emit('flush', 123, metrics);
},

testValidMeasurementTopLevelTag: function(test) {
config.librato.host = '127.0.0.1';
config.librato.tags = {test: true};
librato.init(null, config, this.emitter);

test.expect(5);
let metrics = {gauges: {'my_gauge#foo=bar': 1}};
this.apiServer.post('/v1/measurements')
.reply(200, (uri, requestBody) => {
// Top-level tags
test.deepEqual(requestBody.tags, {test: true, host: '127.0.0.1'});

let measurement = requestBody.measurements[0];
test.ok(requestBody);
test.equal(measurement.name, 'my_gauge');
test.equal(measurement.value, 1);
test.deepEqual(measurement.tags, {foo: 'bar'});
test.done();
});

this.emitter.emit('flush', 123, metrics);
},
};

module.exports.legacy = {
setUp: function(callback) {
config.librato.writeToLegacy = true;
config.librato.countersAsGauges = false;
this.emitter = new events.EventEmitter();

this.apiServer = nock('http://127.0.0.1:36001')
.defaultReplyHeaders({'Content-Type': 'application/json'});

librato.init(null, config, this.emitter);
callback();
},

tearDown: function(callback) {
callback();
},

testGauges: function(test) {
test.expect(7);
let metrics = {gauges: {my_gauge: 1}};
this.apiServer.post('/v1/measurements')
.reply(200, (uri, requestBody) => {
let measurement = requestBody.measurements[0];
test.ok(requestBody);
test.equal(measurement.name, 'my_gauge');
test.equal(measurement.value, 1);
test.deepEqual(measurement.tags, {});
});

this.apiServer.post('/v1/metrics')
.reply(200, (uri, requestBody) => {
let gauge = requestBody.gauges[0];
test.ok(requestBody);
test.equal(gauge.name, 'my_gauge');
test.equal(gauge.value, 1);
test.done();
});

this.emitter.emit('flush', 123, metrics);
},

testCounters: function(test) {
test.expect(7);
let metrics = {counters: {my_counter: 1}};
this.apiServer.post('/v1/measurements')
.reply(200, (uri, requestBody) => {
let measurement = requestBody.measurements[0];
test.ok(requestBody);
test.equal(measurement.name, 'my_counter');
test.equal(measurement.value, 1);
test.deepEqual(measurement.tags, {});
});

this.apiServer.post('/v1/metrics')
.reply(200, (uri, requestBody) => {
let counter = requestBody.counters[0];
test.ok(requestBody);
test.equal(counter.name, 'my_counter');
test.equal(counter.value, 1);
test.done();
});

this.emitter.emit('flush', 123, metrics);
},

testSource: function(test) {
config.librato.source = 'localhost';
librato.init(null, config, this.emitter);

test.expect(9);
let metrics = {gauges: {my_gauge: 1}};
this.apiServer.post('/v1/measurements')
.reply(200, (uri, requestBody) => {
// No top level source
test.equal(requestBody.source, undefined);

let measurement = requestBody.measurements[0];
test.ok(requestBody);
test.equal(measurement.name, 'my_gauge');
test.equal(measurement.value, 1);
test.deepEqual(measurement.tags, {source: 'localhost'});
});

this.apiServer.post('/v1/metrics')
.reply(200, (uri, requestBody) => {
let gauge = requestBody.gauges[0];
test.ok(requestBody);
test.equal(gauge.name, 'my_gauge');
test.equal(gauge.value, 1);
test.equal(gauge.source, 'localhost');
test.done();
});

this.emitter.emit('flush', 123, metrics);
},
};

0 comments on commit f54c4de

Please sign in to comment.