yaorg/node-measured

Invert Control for Gauge

skeggse opened this issue · 4 comments

It looks like the Gauge currently polls the provided function to obtain the current represented value. I think it also makes sense to provide a set method to follow node's evented model. I might want a Gauge of number of open connections in a mysql pool, but I know when that number increases or decreases, so it would be better to just tell the Gauge when that number changes. In some cases, though, like in the case of memoryUsage, it does make sense to poll for changes, so both usages should be supported IMO.

var gauge = new metrics.Gauge(function() {
  return process.memoryUsage().rss;
});

var gauge = new metrics.Gauge();

gauge.set(pool.openConnections());

pool.on('openConn', function() {
  gauge.set(pool.openConnections());
});

pool.on('closeConn', function() {
  gauge.set(pool.openConnections());
});

// or even

var gauge = new metrics.Gauge(function() {
  return pool.openConnections();
});

pool.on('openConn', function() {
  gauge.pull();
});

pool.on('closeConn', function() {
  gauge.pull();
});

You can just use a local variable to store the variable. I don't see the need for a new API here.

var connectionCount = pool.openConnections();

pool.on('openConn', function () {
  connectionCount = pool.openConnections();
});

// ...

var gauge = new metrics.Gauge(function () {
  return {
    connectionCount : connectionCount
  };
});

There wouldn't be any inefficiencies or inconsistencies introduced by polling at non-representative intervals?

Then you would want a counter instead, right?

Hmmm. Last I checked I don't remember the reset method being present on the counter (at least, a reset method that accepts an argument), but yes, the counter serves my purposes.