From cb6cfc84551396edb94b5e4114ef8db1842cb5bf Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Wed, 2 Aug 2017 21:24:44 -0700 Subject: [PATCH] Instrument some more metrics with prometheus --- package.json | 2 +- src/channel/chat.js | 11 +++++++++++ src/database.js | 12 ++++++++++++ src/get-info.js | 7 +++++++ src/io/ioserver.js | 24 ++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 589028e2..35468f23 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Calvin Montgomery", "name": "CyTube", "description": "Online media synchronizer and chat", - "version": "3.44.0", + "version": "3.44.1", "repository": { "url": "http://github.com/calzoneman/sync" }, diff --git a/src/channel/chat.js b/src/channel/chat.js index 95edc63b..b8e378e9 100644 --- a/src/channel/chat.js +++ b/src/channel/chat.js @@ -7,6 +7,7 @@ var Flags = require("../flags"); var url = require("url"); var counters = require("../counters"); import { transformImgTags } from '../camo'; +import { Counter } from 'prom-client'; const SHADOW_TAG = "[shadow]"; const LINK = /(\w+:\/\/(?:[^:\/\[\]\s]+|\[[0-9a-f:]+\])(?::\d+)?(?:\/[^\/\s]*)*)/ig; @@ -149,9 +150,14 @@ ChatModule.prototype.restrictNewAccount = function restrictNewAccount(user, data return false; }; +const chatIncomingCount = new Counter({ + name: 'cytube_chat_incoming_count', + help: 'Number of incoming chatMsg frames' +}); ChatModule.prototype.handleChatMsg = function (user, data) { var self = this; counters.add("chat:incoming"); + chatIncomingCount.inc(); if (!this.channel || !this.channel.modules.permissions.canChat(user)) { return; @@ -281,6 +287,10 @@ ChatModule.prototype.handlePm = function (user, data) { user.socket.emit("pm", msgobj); }; +const chatSentCount = new Counter({ + name: 'cytube_chat_sent_count', + help: 'Number of broadcast chat messages' +}); ChatModule.prototype.processChatMsg = function (user, data) { if (data.msg.match(Config.get("link-domain-blacklist-regex"))) { this.channel.logger.log(user.displayip + " (" + user.getName() + ") was kicked for " + @@ -350,6 +360,7 @@ ChatModule.prototype.processChatMsg = function (user, data) { } this.sendMessage(msgobj); counters.add("chat:sent"); + chatSentCount.inc(); }; ChatModule.prototype.formatMessage = function (username, data) { diff --git a/src/database.js b/src/database.js index 61155c19..695202b1 100644 --- a/src/database.js +++ b/src/database.js @@ -7,8 +7,17 @@ var util = require("./utilities"); import * as Metrics from './metrics/metrics'; import knex from 'knex'; import { GlobalBanDB } from './db/globalban'; +import { Summary, Counter } from 'prom-client'; const LOGGER = require('@calzoneman/jsli')('database'); +const queryLatency = new Summary({ + name: 'cytube_db_query_latency', + help: 'DB query latency (including time spent acquiring connections)' +}); +const queryCount = new Counter({ + name: 'cytube_db_query_count', + help: 'DB query count' +}); let db = null; let globalBanDB = null; @@ -41,8 +50,11 @@ class Database { runTransaction(fn) { const timer = Metrics.startTimer('db:queryTime'); + const end = queryLatency.startTimer(); return this.knex.transaction(fn).finally(() => { + end(); Metrics.stopTimer(timer); + queryCount.inc(); }); } } diff --git a/src/get-info.js b/src/get-info.js index 76562757..a6a48be6 100644 --- a/src/get-info.js +++ b/src/get-info.js @@ -13,8 +13,14 @@ var Streamable = require("cytube-mediaquery/lib/provider/streamable"); var GoogleDrive = require("cytube-mediaquery/lib/provider/googledrive"); var TwitchVOD = require("cytube-mediaquery/lib/provider/twitch-vod"); var TwitchClip = require("cytube-mediaquery/lib/provider/twitch-clip"); +import { Counter } from 'prom-client'; const LOGGER = require('@calzoneman/jsli')('get-info'); +const lookupCounter = new Counter({ + name: 'cytube_media_lookup_count', + help: 'Count of media lookups', + labelNames: ['shortCode'] +}); var urlRetrieve = function (transport, options, callback) { var req = transport.request(options, function (res) { @@ -541,6 +547,7 @@ module.exports = { getMedia: function (id, type, callback) { if(type in this.Getters) { LOGGER.info("Looking up %s:%s", type, id); + lookupCounter.labels(type).inc(); this.Getters[type](id, callback); } else { callback("Unknown media type '" + type + "'", null); diff --git a/src/io/ioserver.js b/src/io/ioserver.js index f1cb2a95..66410946 100644 --- a/src/io/ioserver.js +++ b/src/io/ioserver.js @@ -169,6 +169,7 @@ class IOServer { } initSocketIO() { + patchSocketMetrics(); patchTypecheckedFunctions(); const io = this.io = sio.instance = sio(); @@ -194,6 +195,29 @@ class IOServer { } } +const incomingEventCount = new Counter({ + name: 'cytube_socketio_incoming_events', + help: 'Number of received socket.io events from clients' +}); +const outgoingPacketCount = new Counter({ + name: 'cytube_socketio_outgoing_packets', + help: 'Number of outgoing socket.io packets to clients' +}); +function patchSocketMetrics() { + const onevent = Socket.prototype.onevent; + const packet = Socket.prototype.packet; + + Socket.prototype.onevent = function patchedOnevent() { + onevent.apply(this, arguments); + incomingEventCount.inc(); + }; + + Socket.prototype.packet = function patchedPacket() { + packet.apply(this, arguments); + outgoingPacketCount.inc(); + }; +} + /* TODO: remove this crap */ function patchTypecheckedFunctions() { Socket.prototype.typecheckedOn = function typecheckedOn(msg, template, cb) {