Merge pull request #11036 from overleaf/jpa-cdn-blocked-metric

[web] add metric for blocked cdn

GitOrigin-RevId: bee0fa5af0cc3c5d91deb64c8e32bb7e04dbcc2b
This commit is contained in:
Mathias Jakobsen 2023-01-04 11:14:32 +00:00 committed by Copybot
parent da1d4aba6d
commit e8c677f7ad
3 changed files with 106 additions and 1 deletions

View file

@ -122,7 +122,8 @@ module.exports = function (webRouter, privateApiRouter, publicApiRouter) {
const cdnAvailable = const cdnAvailable =
Settings.cdn && Settings.cdn.web && !!Settings.cdn.web.host Settings.cdn && Settings.cdn.web && !!Settings.cdn.web.host
const cdnBlocked = req.query.nocdn === 'true' || req.session.cdnBlocked const cdnBlocked =
req.query.nocdn === 'true' || req.session.cdnBlocked || false
const userId = SessionManager.getLoggedInUserId(req.session) const userId = SessionManager.getLoggedInUserId(req.session)
if (cdnBlocked && req.session.cdnBlocked == null) { if (cdnBlocked && req.session.cdnBlocked == null) {
logger.debug( logger.debug(
@ -135,6 +136,10 @@ module.exports = function (webRouter, privateApiRouter, publicApiRouter) {
}) })
req.session.cdnBlocked = true req.session.cdnBlocked = true
} }
Metrics.inc('cdn_blocked', 1, {
path: userId ? 'logged-in' : 'pre-login',
method: String(cdnBlocked),
})
const host = req.headers && req.headers.host const host = req.headers && req.headers.host
const isSmoke = host.slice(0, 5).toLowerCase() === 'smoke' const isSmoke = host.slice(0, 5).toLowerCase() === 'smoke'
if (cdnAvailable && !isSmoke && !cdnBlocked) { if (cdnAvailable && !isSmoke && !cdnBlocked) {

View file

@ -23,6 +23,11 @@ module.exports = {
: ['example.com'], : ['example.com'],
statusPageUrl: 'status.example.com', statusPageUrl: 'status.example.com',
cdn: {
web: {
host: 'cdn.example.com',
},
},
apis: { apis: {
linkedUrlProxy: { linkedUrlProxy: {

View file

@ -0,0 +1,95 @@
const { expect } = require('chai')
const User = require('./helpers/User').promises
const {
promises: { getMetric },
} = require('./helpers/metrics')
describe('CDNMigration', function () {
let anon, user
beforeEach(async function () {
anon = new User()
user = new User()
await user.login()
})
let noCdnPreLogin, noCdnLoggedIn
let cdnBlockedTruePreLogin, cdnBlockedTrueLoggedIn
let cdnBlockedFalsePreLogin, cdnBlockedFalseLoggedIn
async function getNoCdn(path) {
return await getMetric(
line => line.includes('no_cdn') && line.includes(path)
)
}
async function getCdnBlocked(path, method) {
return await getMetric(
line =>
line.includes('cdn_blocked') &&
line.includes(`path="${path}"`) &&
line.includes(`method="${method}"`)
)
}
beforeEach(async function () {
noCdnPreLogin = await getNoCdn('pre-login')
noCdnLoggedIn = await getNoCdn('logged-in')
cdnBlockedTruePreLogin = await getCdnBlocked('pre-login', 'true')
cdnBlockedTrueLoggedIn = await getCdnBlocked('logged-in', 'true')
cdnBlockedFalsePreLogin = await getCdnBlocked('pre-login', 'false')
cdnBlockedFalseLoggedIn = await getCdnBlocked('logged-in', 'false')
})
describe('pre-login', function () {
it('should collect no_cdn', async function () {
await anon.doRequest('GET', '/login?nocdn=true')
expect(await getNoCdn('pre-login')).to.equal(noCdnPreLogin + 1)
})
it('should collect cdn_blocked', async function () {
await anon.doRequest('GET', '/login')
await anon.doRequest('GET', '/login')
await anon.doRequest('GET', '/login')
expect(await getCdnBlocked('pre-login', 'false')).to.equal(
cdnBlockedFalsePreLogin + 3
)
expect(await getCdnBlocked('pre-login', 'true')).to.equal(
cdnBlockedTruePreLogin
)
})
it('should collect cdn_blocked after nocdn', async function () {
await anon.doRequest('GET', '/login?nocdn=true')
await anon.doRequest('GET', '/login')
expect(await getCdnBlocked('pre-login', 'false')).to.equal(
cdnBlockedFalsePreLogin
)
expect(await getCdnBlocked('pre-login', 'true')).to.equal(
cdnBlockedTruePreLogin + 2
)
})
})
describe('logged-in', function () {
it('should collect no_cdn', async function () {
await user.doRequest('GET', '/project?nocdn=true')
expect(await getNoCdn('logged-in')).to.equal(noCdnLoggedIn + 1)
})
it('should collect cdn_blocked=false before nocdn', async function () {
await user.doRequest('GET', '/project')
await user.doRequest('GET', '/project')
await user.doRequest('GET', '/project')
expect(await getCdnBlocked('logged-in', 'false')).to.equal(
cdnBlockedFalseLoggedIn + 3
)
expect(await getCdnBlocked('logged-in', 'true')).to.equal(
cdnBlockedTrueLoggedIn
)
})
it('should collect cdn_blocked=true after nocdn=true', async function () {
await user.doRequest('GET', '/project?nocdn=true')
await user.doRequest('GET', '/project')
expect(await getCdnBlocked('logged-in', 'false')).to.equal(
cdnBlockedFalseLoggedIn
)
expect(await getCdnBlocked('logged-in', 'true')).to.equal(
cdnBlockedTrueLoggedIn + 2
)
})
})
})