Просмотр исходного кода

Add metrics into all end points

James Allen 12 лет назад
Родитель
Сommit
66dfafdebe

+ 3 - 2
services/real-time/app/coffee/Router.coffee

@@ -1,4 +1,4 @@
-Metrics = require "metrics-sharelatex"
+metrics = require "metrics-sharelatex"
 logger = require "logger-sharelatex"
 logger = require "logger-sharelatex"
 WebsocketController = require "./WebsocketController"
 WebsocketController = require "./WebsocketController"
 HttpController = require "./HttpController"
 HttpController = require "./HttpController"
@@ -26,7 +26,7 @@ module.exports = Router =
 				client?.disconnect()
 				client?.disconnect()
 				return
 				return
 			
 			
-			Metrics.inc('socket-io.connection')
+			metrics.inc('socket-io.connection')
 			
 			
 			logger.log session: session, client_id: client.id, "client connected"
 			logger.log session: session, client_id: client.id, "client connected"
 			
 			
@@ -44,6 +44,7 @@ module.exports = Router =
 						callback(null, args...)
 						callback(null, args...)
 						
 						
 			client.on "disconnect", () ->
 			client.on "disconnect", () ->
+				metrics.inc('socket-io.disconnect')
 				WebsocketController.leaveProject io, client, (err) ->
 				WebsocketController.leaveProject io, client, (err) ->
 					if err?
 					if err?
 						Router._handleError null, err, client, "leaveProject"
 						Router._handleError null, err, client, "leaveProject"

+ 10 - 3
services/real-time/app/coffee/WebsocketController.coffee

@@ -1,4 +1,5 @@
 logger = require "logger-sharelatex"
 logger = require "logger-sharelatex"
+metrics = require "metrics-sharelatex"
 WebApiManager = require "./WebApiManager"
 WebApiManager = require "./WebApiManager"
 AuthorizationManager = require "./AuthorizationManager"
 AuthorizationManager = require "./AuthorizationManager"
 DocumentUpdaterManager = require "./DocumentUpdaterManager"
 DocumentUpdaterManager = require "./DocumentUpdaterManager"
@@ -16,6 +17,7 @@ module.exports = WebsocketController =
 	joinProject: (client, user, project_id, callback = (error, project, privilegeLevel, protocolVersion) ->) ->
 	joinProject: (client, user, project_id, callback = (error, project, privilegeLevel, protocolVersion) ->) ->
 		user_id = user?._id
 		user_id = user?._id
 		logger.log {user_id, project_id, client_id: client.id}, "user joining project"
 		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) ->
 		WebApiManager.joinProject project_id, user_id, (error, project, privilegeLevel) ->
 			return callback(error) if error?
 			return callback(error) if error?
 
 
@@ -48,6 +50,7 @@ module.exports = WebsocketController =
 	# is determined by FLUSH_IF_EMPTY_DELAY.
 	# is determined by FLUSH_IF_EMPTY_DELAY.
 	FLUSH_IF_EMPTY_DELAY: 500 #ms		
 	FLUSH_IF_EMPTY_DELAY: 500 #ms		
 	leaveProject: (io, client, callback = (error) ->) ->
 	leaveProject: (io, client, callback = (error) ->) ->
+		metrics.inc "editor.leave-project"
 		Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
 		Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
 			return callback(error) if error?
 			return callback(error) if error?
 			logger.log {project_id, user_id, client_id: client.id}, "client leaving project"
 			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
 			, WebsocketController.FLUSH_IF_EMPTY_DELAY
 			
 			
 	joinDoc: (client, doc_id, fromVersion = -1, callback = (error, doclines, version, ops) ->) ->
 	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}) ->
 		Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
 			return callback(error) if error?
 			return callback(error) if error?
 			return callback(new Error("no project_id found on client")) if !project_id?
 			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"
 					logger.log {user_id, project_id, doc_id, fromVersion, client_id: client.id}, "client joined doc"
 					
 					
 	leaveDoc: (client, doc_id, callback = (error) ->) ->
 	leaveDoc: (client, doc_id, callback = (error) ->) ->
+		metrics.inc "editor.leave-doc"
 		Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
 		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"
 			logger.log {user_id, project_id, doc_id, client_id: client.id}, "client leaving doc"
 		client.leave doc_id
 		client.leave doc_id
 		callback()
 		callback()
 		
 		
 	updateClientPosition: (client, cursorData, callback = (error) ->) ->
 	updateClientPosition: (client, cursorData, callback = (error) ->) ->
+		metrics.inc "editor.update-client-position", 0.1
 		Utils.getClientAttributes client, [
 		Utils.getClientAttributes client, [
 			"project_id", "first_name", "last_name", "email", "user_id"
 			"project_id", "first_name", "last_name", "email", "user_id"
 		], (error, {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)
 			WebsocketLoadBalancer.emitToRoom(project_id, "clientTracking.clientUpdated", cursorData)
 		
 		
 	getConnectedUsers: (client, callback = (error, users) ->) ->
 	getConnectedUsers: (client, callback = (error, users) ->) ->
+		metrics.inc "editor.get-connected-users"
 		Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
 		Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
 			return callback(error) if error?
 			return callback(error) if error?
 			return callback(new Error("no project_id found on client")) if !project_id?
 			return callback(new Error("no project_id found on client")) if !project_id?
@@ -157,9 +164,9 @@ module.exports = WebsocketController =
 				update.meta ||= {}
 				update.meta ||= {}
 				update.meta.source = client.id
 				update.meta.source = client.id
 				update.meta.user_id = user_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"
 				logger.log {user_id, doc_id, project_id, client_id: client.id, version: update.v}, "sending update to doc updater"
 
 

+ 30 - 8
services/real-time/test/unit/coffee/WebsocketControllerTests.coffee

@@ -33,6 +33,10 @@ describe 'WebsocketController', ->
 			"./ConnectedUsersManager": @ConnectedUsersManager = {}
 			"./ConnectedUsersManager": @ConnectedUsersManager = {}
 			"./WebsocketLoadBalancer": @WebsocketLoadBalancer = {}
 			"./WebsocketLoadBalancer": @WebsocketLoadBalancer = {}
 			"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
 			"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
+			"metrics-sharelatex": @metrics =
+				inc: sinon.stub()
+				set: sinon.stub()
+				
 	
 	
 	afterEach ->
 	afterEach ->
 		tk.reset()
 		tk.reset()
@@ -99,6 +103,9 @@ describe 'WebsocketController', ->
 				@ConnectedUsersManager.updateUserPosition
 				@ConnectedUsersManager.updateUserPosition
 					.calledWith(@project_id, @client.id, @user, null)
 					.calledWith(@project_id, @client.id, @user, null)
 					.should.equal true
 					.should.equal true
+					
+			it "should increment the join-project metric", ->
+				@metrics.inc.calledWith("editor.join-project").should.equal true
 				
 				
 		describe "when not authorized", ->
 		describe "when not authorized", ->
 			beforeEach ->
 			beforeEach ->
@@ -151,6 +158,9 @@ describe 'WebsocketController', ->
 				@TrackChangesManager.flushProject
 				@TrackChangesManager.flushProject
 					.calledWith(@project_id)
 					.calledWith(@project_id)
 					.should.equal true
 					.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", ->
 		describe "when the project is not empty", ->
 			beforeEach ->
 			beforeEach ->
@@ -202,6 +212,9 @@ describe 'WebsocketController', ->
 					.calledWith(null, @doc_lines, @version, @ops)
 					.calledWith(null, @doc_lines, @version, @ops)
 					.should.equal true
 					.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", ->
 		describe "with doclines that need escaping", ->
 			beforeEach ->
 			beforeEach ->
 				@doc_lines.push ["räksmörgås"]
 				@doc_lines.push ["räksmörgås"]
@@ -238,6 +251,9 @@ describe 'WebsocketController', ->
 		it "should call the callback", ->
 		it "should call the callback", ->
 			@callback.called.should.equal true
 			@callback.called.should.equal true
 			
 			
+		it "should increment the leave-doc metric", ->
+			@metrics.inc.calledWith("editor.leave-doc").should.equal true
+			
 	describe "getConnectedUsers", ->
 	describe "getConnectedUsers", ->
 		beforeEach ->
 		beforeEach ->
 			@client.params.project_id = @project_id
 			@client.params.project_id = @project_id
@@ -261,6 +277,9 @@ describe 'WebsocketController', ->
 					
 					
 			it "should return the users", ->
 			it "should return the users", ->
 				@callback.calledWith(null, @users).should.equal true
 				@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", ->
 		describe "when not authorized", ->
 			beforeEach ->
 			beforeEach ->
@@ -321,6 +340,9 @@ describe 'WebsocketController', ->
 					doc_id: @doc_id
 					doc_id: @doc_id
 				}).should.equal true
 				}).should.equal true
 				done()
 				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", ->
 		describe "with an anonymous user", ->
 			beforeEach ->
 			beforeEach ->
@@ -371,14 +393,14 @@ describe 'WebsocketController', ->
 			it "should call the callback", ->
 			it "should call the callback", ->
 				@callback.called.should.equal true
 				@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", ->
 		describe "unsuccessfully", ->
 			beforeEach ->
 			beforeEach ->