configurable caching in console.js #278

Closed
wants to merge 5 commits into from
View
5 backends/console.js
@@ -5,7 +5,6 @@ function ConsoleBackend(startupTime, config, emitter){
this.lastFlush = startupTime;
this.lastException = startupTime;
this.config = config.console || {};
-
this.statsCache = {
counters: {},
timers: {}
@@ -31,8 +30,8 @@ ConsoleBackend.prototype.flush = function(timestamp, metrics) {
});
var out = {
- counters: this.statsCache.counters,
- timers: this.statsCache.timers,
+ counters: this.config.useCounterCache === true ? this.statsCache.counters : metrics.counters,
+ timers: this.config.useTimerCache === true ? this.statsCache.timers : metrics.timers,
gauges: metrics.gauges,
timer_data: metrics.timer_data,
counter_rates: metrics.counter_rates,
View
38 backends/http.js
@@ -0,0 +1,38 @@
+/** This backend pushes the metrics to browser.
+ * Uses socket.io and the stats are pushed to browser every 10s.
+ */
+
+var util = require('util')
+var app = require('express')();
+var server = require('http').createServer(app);
+var socketIO = require('socket.io').listen(server);
+
+server.listen(8000);
+
+app.get('/', function(req, res) {
+ res.sendfile(__dirname + '/index.html');
+});
+
+function HttpBackend(startUpTime, config, emitter){
+ console.log("configuring httpbackend");
+ var self = this;
+ this.lastFlush = startUpTime;
+ this.lastException = startUpTime;
+ this.config = config.http || {};
+}
+
+push_stats = function(timestamp, metrics){
+ socketIO.sockets.emit("counters", metrics);
+}
+
+HttpBackend.prototype.status = function(write) {
+ ['lastFlush', 'lastException'].forEach(function(key) {
+ write(null, 'console', key, this[key]);
+ }, this);
+};
+
+exports.init = function(startupTime, config, events) {
+ var instance = new HttpBackend(startupTime, config, events);
+ events.on("flush", push_stats);
+ return true;
+};
View
33 backends/index.html
@@ -0,0 +1,33 @@
+<html>
+ <head>
+ <style type="text/css">
+ #counters{
+ width: 700px;
+ height: 600px;
+ overflow: scroll;
+ margin-left: 300px;
+ background-color: #d6d6c2;
+ text-shadow:1px -1px 1px #333333;
+ box-shadow:2px 2px 2px 2px #333333;
+ }
+ </style>
+ <script src="/socket.io/socket.io.js"></script>
+ <script type="text/javascript">
+ var socket = io.connect('http://localhost:8000');
+ socket.on("counters", function(metrics){
+ document.getElementById("counters").innerHTML = "<h1>Stats</h1>";
+ for(metric in metrics){
+ document.getElementById("counters").innerHTML += "<b><h3>" + metric + "</h3></b>";
+ for(key in metrics[metric]){
+ document.getElementById("counters").innerHTML += " " + key + " : " + metrics[metric][key] + "</br>";
+ }
+ }
+ });
+ </script>
+ </head>
+ <body>
+ <div id="counters">
+ <h1>Stats</h1>
+ </div>
+ </body>
+</html>
View
13 exampleConfig.js
@@ -45,8 +45,12 @@ Optional Variables:
applies to both legacy and new namespacing
console:
- prettyprint: whether to prettyprint the console backend
- output [true or false, default: true]
+ prettyprint: whether to prettyprint the console backend
+ output [true or false, default: true]
+ useCounterCache: whether to use cache for console.js counters or not
+ values [true or false]
+ useTimerCache: whether to use cache for console.js timers or not
+ values [true or false]
log: log settings [object, default: undefined]
backend: where to log: stdout or syslog [string, default: stdout]
@@ -88,7 +92,8 @@ Optional Variables:
*/
{
graphitePort: 2003
-, graphiteHost: "graphite.example.com"
+, console: { useCounterCache : true, useTimerCache : false}
+, graphiteHost: "localhost"
, port: 8125
-, backends: [ "./backends/graphite" ]
+, backends: [ "./backends/graphite", "./backends/console", "./backends/http" ]
}
View
4 package.json
@@ -18,7 +18,9 @@
"devDependencies": {
"nodeunit": "0.7.x",
"underscore": "1.4.x",
- "temp": "0.4.x"
+ "temp": "0.4.x",
+ "express": "3.0.0",
+ "socket.io": "0.9.x"
},
"optionalDependencies": {
"node-syslog":"1.1.7",