From 66dfafdebe0d777b96d2865973f457a16fccc855 Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 17 Nov 2014 13:12:49 +0000 Subject: [PATCH] Add metrics into all end points --- services/real-time/app/coffee/Router.coffee | 5 ++- .../app/coffee/WebsocketController.coffee | 13 +++++-- .../coffee/WebsocketControllerTests.coffee | 38 +++++++++++++++---- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/services/real-time/app/coffee/Router.coffee b/services/real-time/app/coffee/Router.coffee index 85a7940b7a..14d2f698a8 100644 --- a/services/real-time/app/coffee/Router.coffee +++ b/services/real-time/app/coffee/Router.coffee @@ -1,4 +1,4 @@ -Metrics = require "metrics-sharelatex" +metrics = require "metrics-sharelatex" logger = require "logger-sharelatex" WebsocketController = require "./WebsocketController" HttpController = require "./HttpController" @@ -26,7 +26,7 @@ module.exports = Router = client?.disconnect() return - Metrics.inc('socket-io.connection') + metrics.inc('socket-io.connection') logger.log session: session, client_id: client.id, "client connected" @@ -44,6 +44,7 @@ module.exports = Router = callback(null, args...) client.on "disconnect", () -> + metrics.inc('socket-io.disconnect') WebsocketController.leaveProject io, client, (err) -> if err? Router._handleError null, err, client, "leaveProject" diff --git a/services/real-time/app/coffee/WebsocketController.coffee b/services/real-time/app/coffee/WebsocketController.coffee index 9315c0f15e..73f327d22f 100644 --- a/services/real-time/app/coffee/WebsocketController.coffee +++ b/services/real-time/app/coffee/WebsocketController.coffee @@ -1,4 +1,5 @@ logger = require "logger-sharelatex" +metrics = require "metrics-sharelatex" WebApiManager = require "./WebApiManager" AuthorizationManager = require "./AuthorizationManager" DocumentUpdaterManager = require "./DocumentUpdaterManager" @@ -16,6 +17,7 @@ module.exports = WebsocketController = joinProject: (client, user, project_id, callback = (error, project, privilegeLevel, protocolVersion) ->) -> user_id = user?._id logger.log {user_id, project_id, client_id: client.id}, "user joining project" + metrics.inc "editor.join-project" WebApiManager.joinProject project_id, user_id, (error, project, privilegeLevel) -> return callback(error) if error? @@ -48,6 +50,7 @@ module.exports = WebsocketController = # is determined by FLUSH_IF_EMPTY_DELAY. FLUSH_IF_EMPTY_DELAY: 500 #ms leaveProject: (io, client, callback = (error) ->) -> + metrics.inc "editor.leave-project" Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) -> return callback(error) if error? logger.log {project_id, user_id, client_id: client.id}, "client leaving project" @@ -72,6 +75,7 @@ module.exports = WebsocketController = , WebsocketController.FLUSH_IF_EMPTY_DELAY joinDoc: (client, doc_id, fromVersion = -1, callback = (error, doclines, version, ops) ->) -> + metrics.inc "editor.join-doc" Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) -> return callback(error) if error? return callback(new Error("no project_id found on client")) if !project_id? @@ -96,12 +100,14 @@ module.exports = WebsocketController = logger.log {user_id, project_id, doc_id, fromVersion, client_id: client.id}, "client joined doc" leaveDoc: (client, doc_id, callback = (error) ->) -> + metrics.inc "editor.leave-doc" Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) -> logger.log {user_id, project_id, doc_id, client_id: client.id}, "client leaving doc" client.leave doc_id callback() updateClientPosition: (client, cursorData, callback = (error) ->) -> + metrics.inc "editor.update-client-position", 0.1 Utils.getClientAttributes client, [ "project_id", "first_name", "last_name", "email", "user_id" ], (error, {project_id, first_name, last_name, email, user_id}) -> @@ -128,6 +134,7 @@ module.exports = WebsocketController = WebsocketLoadBalancer.emitToRoom(project_id, "clientTracking.clientUpdated", cursorData) getConnectedUsers: (client, callback = (error, users) ->) -> + metrics.inc "editor.get-connected-users" Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) -> return callback(error) if error? return callback(new Error("no project_id found on client")) if !project_id? @@ -157,9 +164,9 @@ module.exports = WebsocketController = update.meta ||= {} update.meta.source = client.id update.meta.user_id = user_id - # metrics.inc "editor.doc-update", 0.3 - # metrics.set "editor.active-projects", project_id, 0.3 - # metrics.set "editor.active-users", user_id, 0.3 + metrics.inc "editor.doc-update", 0.3 + metrics.set "editor.active-projects", project_id, 0.3 + metrics.set "editor.active-users", user_id, 0.3 logger.log {user_id, doc_id, project_id, client_id: client.id, version: update.v}, "sending update to doc updater" diff --git a/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee b/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee index 3ff8b28f3b..5d2e63cc03 100644 --- a/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee +++ b/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee @@ -33,6 +33,10 @@ describe 'WebsocketController', -> "./ConnectedUsersManager": @ConnectedUsersManager = {} "./WebsocketLoadBalancer": @WebsocketLoadBalancer = {} "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() } + "metrics-sharelatex": @metrics = + inc: sinon.stub() + set: sinon.stub() + afterEach -> tk.reset() @@ -99,6 +103,9 @@ describe 'WebsocketController', -> @ConnectedUsersManager.updateUserPosition .calledWith(@project_id, @client.id, @user, null) .should.equal true + + it "should increment the join-project metric", -> + @metrics.inc.calledWith("editor.join-project").should.equal true describe "when not authorized", -> beforeEach -> @@ -151,6 +158,9 @@ describe 'WebsocketController', -> @TrackChangesManager.flushProject .calledWith(@project_id) .should.equal true + + it "should increment the leave-project metric", -> + @metrics.inc.calledWith("editor.leave-project").should.equal true describe "when the project is not empty", -> beforeEach -> @@ -202,6 +212,9 @@ describe 'WebsocketController', -> .calledWith(null, @doc_lines, @version, @ops) .should.equal true + it "should increment the join-doc metric", -> + @metrics.inc.calledWith("editor.join-doc").should.equal true + describe "with doclines that need escaping", -> beforeEach -> @doc_lines.push ["räksmörgås"] @@ -238,6 +251,9 @@ describe 'WebsocketController', -> it "should call the callback", -> @callback.called.should.equal true + it "should increment the leave-doc metric", -> + @metrics.inc.calledWith("editor.leave-doc").should.equal true + describe "getConnectedUsers", -> beforeEach -> @client.params.project_id = @project_id @@ -261,6 +277,9 @@ describe 'WebsocketController', -> it "should return the users", -> @callback.calledWith(null, @users).should.equal true + + it "should increment the get-connected-users metric", -> + @metrics.inc.calledWith("editor.get-connected-users").should.equal true describe "when not authorized", -> beforeEach -> @@ -321,6 +340,9 @@ describe 'WebsocketController', -> doc_id: @doc_id }).should.equal true done() + + it "should increment the update-client-position metric at 0.1 frequency", -> + @metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true describe "with an anonymous user", -> beforeEach -> @@ -371,14 +393,14 @@ describe 'WebsocketController', -> it "should call the callback", -> @callback.called.should.equal true - # it "should update the active users metric", -> - # @metrics.set.calledWith("editor.active-users", @user_id).should.equal true - # - # it "should update the active projects metric", -> - # @metrics.set.calledWith("editor.active-projects", @project_id).should.equal true - # - # it "should increment the doc updates", -> - # @metrics.inc.calledWith("editor.doc-update").should.equal true + it "should update the active users metric", -> + @metrics.set.calledWith("editor.active-users", @user_id).should.equal true + + it "should update the active projects metric", -> + @metrics.set.calledWith("editor.active-projects", @project_id).should.equal true + + it "should increment the doc updates", -> + @metrics.inc.calledWith("editor.doc-update").should.equal true describe "unsuccessfully", -> beforeEach ->