Skip to content

Commit

Permalink
breaking: Remove all support for passing host/port options.
Browse files Browse the repository at this point in the history
You now must pass an output in uri form or as a socket path.
Updated remaining docs & tests.

`app` now defaults to `numbat`, in the spirit of requiring as
little as possible to operate.
  • Loading branch information
ceejbot committed Mar 9, 2017
1 parent 407ba8f commit ee27137
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ emitter.metric({ name: 'httpd.latency', value: 30 });
emitter.metric({ name: 'disk.used.percent', value: 36 });
emitter.metric({ name: 'heartbeat'});


// if you don't have a reference to an emitter, you
// can broadcast a metric to all extant emitters:
process.emit('metric', { name: 'heartbeat' });
Expand Down
14 changes: 5 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ var Emitter = module.exports = function Emitter(opts)
{
if (this.constructor !== Emitter) return new Emitter(opts);
assert(opts && _.isObject(opts), 'you must pass an options object to the Emitter constructor');
assert((opts.host && opts.port) || opts.path || opts.uri, 'you must pass uri, path, or a host/port pair for the collector');
assert(opts.app && _.isString(opts.app), 'you must pass an `app` option naming this service or app');
assert(opts.path || opts.uri, 'you must pass an output uri or a socket path to specify metrics destinationr');

events.EventEmitter.call(this);
this.options = Object.assign({}, opts);

if (opts.uri) Emitter.parseURI(opts);
if (opts.uri) Emitter.parseURI(this.options);
if (opts.maxretries) this.maxretries = opts.maxretries;
if (opts.maxbacklog) this.maxbacklog = opts.maxbacklog;
if ('shouldUnref' in opts) this.shouldUnref = opts.shouldUnref;

this.options = opts;
this.defaults = { host: os.hostname() };
if (opts.node) this.defaults.node = opts.node;
this.app = opts.app;
this.app = opts.app || 'numbat';
this.input = discard({objectMode: true, maxBacklog: opts.maxbacklog});
this.output = JSONStringifyStream({ highWaterMark: opts.maxbacklog });
this.input.pipe(this.output);
Expand All @@ -48,10 +47,7 @@ Emitter.prototype.maxbacklog = 1000;
Emitter.prototype.shouldUnref = true;

Object.defineProperty(Emitter.prototype, 'backlog', {
get: function()
{
return this.input.backlog;
}
get: function backlogGetter() { return this.input.backlog; }
});

Emitter.setGlobalEmitter = function setGlobalEmitter(emitter)
Expand Down
32 changes: 13 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ var
describe('numbat-emitter', function()
{
var mockOpts = {
host: 'localhost',
port: 4333,
uri: 'tcp://localhost:4333',
app: 'testapp',
node: 'node-1'
};
Expand Down Expand Up @@ -128,34 +127,22 @@ describe('numbat-emitter', function()
done();
});

it('requires a host & port option', function(done)
it('requires at least one of path or uri', function(done)
{
function shouldThrow() { return new Emitter({ host: 'example.com' }); }
shouldThrow.must.throw(/host/);
function shouldThrow() { return new Emitter({ }); }
shouldThrow.must.throw(/output uri or/);
done();
});

it('requires a path option otherwise', function(done)
{
function shouldThrow() { return new Emitter({ path: '/tmp/numbat.sock' }); }
shouldThrow.must.throw(/app/);
done();
});

it('requires an app name option', function()
{
function shouldThrow() { return new Emitter({ host: 'localhost', port: 4000 }); }
shouldThrow.must.throw(/app/);
});

it('can be constructed', function(done)
{
var emitter = new Emitter(mockOpts);
emitter.must.be.an.object();

emitter.must.have.property('options');
emitter.options.must.be.an.object();
emitter.options.must.equal(mockOpts);
emitter.options.host.must.equal('localhost');
emitter.options.port.must.equal('4333');

emitter.must.have.property('defaults');
emitter.defaults.must.be.an.object();
Expand All @@ -175,6 +162,13 @@ describe('numbat-emitter', function()
e.options.must.have.property('path');
});

it('defaults `app` to `numbat`', function()
{
var e = new Emitter({ uri: 'sock:/tmp/foobar.sock' });
e.must.have.property('app');
e.app.must.equal('numbat');
});

it('adds the host name to its default fields', function()
{
var e = new Emitter({ uri: 'sock:/tmp/foobar.sock', app: 'test' });
Expand Down

0 comments on commit ee27137

Please sign in to comment.