{"size": 1701, "ext": "js", "max_stars_repo_path": "lib/SocketIoCollector.js", "max_stars_repo_name": "imatefx/socket.io-prometheus-v3", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/SocketIoCollector.js", "max_issues_repo_name": "imatefx/socket.io-prometheus-v3", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/SocketIoCollector.js", "max_forks_repo_name": "imatefx/socket.io-prometheus-v3", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 40.5, "max_line_length": 93, "alphanum_fraction": 0.6355085244}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const metrics_1 = require("./metrics");
const helpers_1 = require("./helpers");
class SocketIoCollector {
    static collectSocketIoMetrics(io) {
        io.on('connect', (socket) => {
            SocketIoCollector.collectConcurrentConnections(socket);
            SocketIoCollector.collectTotalSentsMessages(socket, io);
            SocketIoCollector.collectTotalReceivedMessages(socket, io);
        });
    }
    static collectConcurrentConnections(socket) {
        socket.on('disconnect', () => {
            metrics_1.default.concurrentConnections.dec();
        });
        metrics_1.default.concurrentConnections.inc();
    }
    static collectTotalSentsMessages(socket, io) {
        const orgEmit = socket.emit;
        socket.emit = (event, ...args) => {
            metrics_1.default.totalSentsEvents.inc();
            metrics_1.default.totalSentsBytes.inc(helpers_1.default.dataToBytes(args));
            return orgEmit.apply(socket, [event, ...args]);
        };
        const orgServerEmit = io.emit;
        io.emit = (event, ...args) => {
            metrics_1.default.totalSentsEvents.inc();
            metrics_1.default.totalSentsBytes.inc(helpers_1.default.dataToBytes(args));
            return orgServerEmit.apply(io, [event, ...args]);
        };
    }
    static collectTotalReceivedMessages(socket, io) {
        socket.onAny((...args) => {
            const [eventName, message] = args;
            metrics_1.default.totalReceivedEvents.inc();
            metrics_1.default.totalReceivedBytes.inc(helpers_1.default.dataToBytes(message));
        });
    }
}
exports.default = SocketIoCollector;
