1 import io from 'socket.io';
2 import Socket from 'socket.io/lib/socket';
3 import * as Metrics from './metrics/metrics';
4 import { JSONFileMetricsReporter } from './metrics/jsonfilemetricsreporter';
6 const LOGGER = require('@calzoneman/jsli')('counters');
10 exports.add = Metrics.incCounter;
12 Socket.prototype._packet = Socket.prototype.packet;
13 Socket.prototype.packet = function () {
14 this._packet.apply(this, arguments);
15 exports.add('socket.io:packet');
18 function getConnectedSockets() {
19 var sockets = io.instance.sockets.sockets;
20 if (typeof sockets.length === 'number') {
21 return sockets.length;
23 return Object.keys(sockets).length;
27 function setChannelCounts(metrics) {
28 if (server === null) {
29 server = require('./server').getServer();
35 server.channels.forEach(function (c) {
37 if (c.modules.options && c.modules.options.get("show_public")) {
42 metrics.addProperty('channelCount:all', allCount);
43 metrics.addProperty('channelCount:public', publicCount);
45 LOGGER.error(error.stack);
49 const reporter = new JSONFileMetricsReporter('counters.log');
50 Metrics.setReporter(reporter);
51 Metrics.setReportInterval(60000);
52 Metrics.addReportHook((metrics) => {
53 metrics.addProperty('socket.io:count', getConnectedSockets());
54 setChannelCounts(metrics);