Merge pull request #4922 from overleaf/jpa-web-clsi-metrics

[web] add new metrics for tracking initial assign/switch of clsi backend

GitOrigin-RevId: 4fb15ee8727cb397d1e44a86efb7d4833626bc6b
This commit is contained in:
Alexandre Bourdin 2021-08-31 14:46:23 +02:00 committed by Copybot
parent 46780e5144
commit 1094b0570b
4 changed files with 32 additions and 12 deletions

View file

@ -22,6 +22,7 @@ if (Settings.redis.clsi_cookie_secondary != null) {
} }
const Cookie = require('cookie') const Cookie = require('cookie')
const logger = require('logger-sharelatex') const logger = require('logger-sharelatex')
const Metrics = require('@overleaf/metrics')
const clsiCookiesEnabled = const clsiCookiesEnabled =
(Settings.clsiCookie != null ? Settings.clsiCookie.key : undefined) != null && (Settings.clsiCookie != null ? Settings.clsiCookie.key : undefined) != null &&
@ -72,7 +73,12 @@ module.exports = function (backendGroup) {
}) })
return callback(err) return callback(err)
} }
this.setServerId(project_id, user_id, res, function (err, serverId) { this.setServerId(
project_id,
user_id,
res,
null,
function (err, serverId) {
if (err != null) { if (err != null) {
logger.warn( logger.warn(
{ err, project_id }, { err, project_id },
@ -80,7 +86,8 @@ module.exports = function (backendGroup) {
) )
} }
return callback(err, serverId) return callback(err, serverId)
}) }
)
}) })
}, },
@ -93,7 +100,7 @@ module.exports = function (backendGroup) {
return cookies != null ? cookies[Settings.clsiCookie.key] : undefined return cookies != null ? cookies[Settings.clsiCookie.key] : undefined
}, },
setServerId(project_id, user_id, response, callback) { setServerId(project_id, user_id, response, previous, callback) {
if (callback == null) { if (callback == null) {
callback = function (err, serverId) {} callback = function (err, serverId) {}
} }
@ -109,6 +116,12 @@ module.exports = function (backendGroup) {
err => callback(err, undefined) err => callback(err, undefined)
) )
} }
if (!previous) {
// Initial assignment of a user+project or after clearing cache.
Metrics.inc('clsi-lb-assign-initial-backend')
} else {
Metrics.inc('clsi-lb-switch-backend')
}
if (rclient_secondary != null) { if (rclient_secondary != null) {
this._setServerIdInRedis( this._setServerIdInRedis(
rclient_secondary, rclient_secondary,

View file

@ -328,6 +328,7 @@ const ClsiManager = {
projectId, projectId,
userId, userId,
response, response,
clsiServerId,
(err, newClsiServerId) => { (err, newClsiServerId) => {
if (err != null) { if (err != null) {
callback( callback(
@ -420,7 +421,7 @@ const ClsiManager = {
NewBackendCloudClsiCookieManager.getCookieJar( NewBackendCloudClsiCookieManager.getCookieJar(
projectId, projectId,
userId, userId,
(err, jar) => { (err, jar, clsiServerId) => {
if (err != null) { if (err != null) {
return callback( return callback(
OError.tag(err, 'error getting cookie jar for CLSI request', { OError.tag(err, 'error getting cookie jar for CLSI request', {
@ -444,6 +445,7 @@ const ClsiManager = {
projectId, projectId,
userId, userId,
response, response,
clsiServerId,
err => { err => {
if (err != null) { if (err != null) {
return callback( return callback(

View file

@ -118,7 +118,7 @@ describe('ClsiCookieManager', function () {
this.request.post.callsArgWith(1, null, this.response) this.request.post.callsArgWith(1, null, this.response)
return (this.ClsiCookieManager.setServerId = sinon return (this.ClsiCookieManager.setServerId = sinon
.stub() .stub()
.callsArgWith(3, null, 'clsi-9')) .yields(null, 'clsi-9'))
}) })
it('should make a request to the clsi', function (done) { it('should make a request to the clsi', function (done) {
@ -160,6 +160,7 @@ describe('ClsiCookieManager', function () {
this.project_id, this.project_id,
this.user_id, this.user_id,
this.response, this.response,
null,
err => { err => {
this.redis.setex this.redis.setex
.calledWith( .calledWith(
@ -178,6 +179,7 @@ describe('ClsiCookieManager', function () {
this.project_id, this.project_id,
this.user_id, this.user_id,
this.response, this.response,
null,
(err, serverId) => { (err, serverId) => {
serverId.should.equal('clsi-8') serverId.should.equal('clsi-8')
return done() return done()
@ -197,6 +199,7 @@ describe('ClsiCookieManager', function () {
this.project_id, this.project_id,
this.user_id, this.user_id,
this.response, this.response,
null,
(err, serverId) => { (err, serverId) => {
this.redis.setex.called.should.equal(false) this.redis.setex.called.should.equal(false)
return done() return done()
@ -212,6 +215,7 @@ describe('ClsiCookieManager', function () {
this.project_id, this.project_id,
this.user_id, this.user_id,
this.response, this.response,
null,
(err, serverId) => { (err, serverId) => {
this.redis.setex.called.should.equal(false) this.redis.setex.called.should.equal(false)
return done() return done()
@ -240,6 +244,7 @@ describe('ClsiCookieManager', function () {
this.project_id, this.project_id,
this.user_id, this.user_id,
this.response, this.response,
null,
(err, serverId) => { (err, serverId) => {
this.redis_secondary.setex this.redis_secondary.setex
.calledWith( .calledWith(

View file

@ -9,7 +9,7 @@ describe('ClsiManager', function () {
this.ClsiCookieManager = { this.ClsiCookieManager = {
clearServerId: sinon.stub().yields(), clearServerId: sinon.stub().yields(),
getCookieJar: sinon.stub().callsArgWith(2, null, this.jar), getCookieJar: sinon.stub().callsArgWith(2, null, this.jar),
setServerId: sinon.stub().callsArgWith(3), setServerId: sinon.stub().yields(null),
_getServerId: sinon.stub(), _getServerId: sinon.stub(),
} }
this.ClsiStateManager = { this.ClsiStateManager = {