mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 22:23:42 +01:00
[web] Add startup metrics (#25277)
* [web] refactor startup sequence The primary objective here is to call loadGlobalBlobs() only once. But to get there, we need to reorder things and add extra try/catch sections to ensure we are not letting the global uncaughtException handler catch startup errors. Co-authored-by: Antoine Clausse <antoine.clausse@overleaf.com> * [web] add metrics for startup steps Co-authored-by: Antoine Clausse <antoine.clausse@overleaf.com> --------- Co-authored-by: Jakob Ackermann <jakob.ackermann@overleaf.com> GitOrigin-RevId: c73edea02516e919d55b896588dcd1862835fedf
This commit is contained in:
committed by
Copybot
co-authored by
Jakob Ackermann
parent
531b03e808
commit
5b47fffcee
@@ -5,6 +5,8 @@
|
|||||||
* before any other module to support code instrumentation.
|
* before any other module to support code instrumentation.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const metricsModuleImportStartTime = performance.now()
|
||||||
|
|
||||||
const APP_NAME = process.env.METRICS_APP_NAME || 'unknown'
|
const APP_NAME = process.env.METRICS_APP_NAME || 'unknown'
|
||||||
const BUILD_VERSION = process.env.BUILD_VERSION
|
const BUILD_VERSION = process.env.BUILD_VERSION
|
||||||
const ENABLE_PROFILE_AGENT = process.env.ENABLE_PROFILE_AGENT === 'true'
|
const ENABLE_PROFILE_AGENT = process.env.ENABLE_PROFILE_AGENT === 'true'
|
||||||
@@ -103,3 +105,5 @@ function recordProcessStart() {
|
|||||||
const metrics = require('.')
|
const metrics = require('.')
|
||||||
metrics.inc('process_startup')
|
metrics.inc('process_startup')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = { metricsModuleImportStartTime }
|
||||||
|
|||||||
+56
-35
@@ -1,5 +1,5 @@
|
|||||||
// Metrics must be initialized before importing anything else
|
// Metrics must be initialized before importing anything else
|
||||||
import '@overleaf/metrics/initialize.js'
|
import { metricsModuleImportStartTime } from '@overleaf/metrics/initialize.js'
|
||||||
|
|
||||||
import Modules from './app/src/infrastructure/Modules.js'
|
import Modules from './app/src/infrastructure/Modules.js'
|
||||||
import metrics from '@overleaf/metrics'
|
import metrics from '@overleaf/metrics'
|
||||||
@@ -20,6 +20,13 @@ import FileWriter from './app/src/infrastructure/FileWriter.js'
|
|||||||
import { fileURLToPath } from 'node:url'
|
import { fileURLToPath } from 'node:url'
|
||||||
import Features from './app/src/infrastructure/Features.js'
|
import Features from './app/src/infrastructure/Features.js'
|
||||||
|
|
||||||
|
metrics.gauge(
|
||||||
|
'web_startup',
|
||||||
|
performance.now() - metricsModuleImportStartTime,
|
||||||
|
1,
|
||||||
|
{ path: 'imports' }
|
||||||
|
)
|
||||||
|
|
||||||
logger.initialize(process.env.METRICS_APP_NAME || 'web')
|
logger.initialize(process.env.METRICS_APP_NAME || 'web')
|
||||||
logger.logger.serializers.user = Serializers.user
|
logger.logger.serializers.user = Serializers.user
|
||||||
logger.logger.serializers.docs = Serializers.docs
|
logger.logger.serializers.docs = Serializers.docs
|
||||||
@@ -58,6 +65,29 @@ if (
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle SIGTERM for graceful shutdown in kubernetes
|
||||||
|
process.on('SIGTERM', function (signal) {
|
||||||
|
triggerGracefulShutdown(Server.server, signal)
|
||||||
|
})
|
||||||
|
|
||||||
|
const beforeWaitForMongoAndGlobalBlobs = performance.now()
|
||||||
|
try {
|
||||||
|
await Promise.all([
|
||||||
|
mongodb.connectionPromise,
|
||||||
|
mongoose.connectionPromise,
|
||||||
|
HistoryManager.promises.loadGlobalBlobs(),
|
||||||
|
])
|
||||||
|
} catch (err) {
|
||||||
|
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
metrics.gauge(
|
||||||
|
'web_startup',
|
||||||
|
performance.now() - beforeWaitForMongoAndGlobalBlobs,
|
||||||
|
1,
|
||||||
|
{ path: 'waitForMongoAndGlobalBlobs' }
|
||||||
|
)
|
||||||
|
|
||||||
const port = Settings.port || Settings.internal.web.port || 3000
|
const port = Settings.port || Settings.internal.web.port || 3000
|
||||||
const host = Settings.internal.web.host || '127.0.0.1'
|
const host = Settings.internal.web.host || '127.0.0.1'
|
||||||
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
||||||
@@ -69,42 +99,33 @@ if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|||||||
|
|
||||||
PlansLocator.ensurePlansAreSetupCorrectly()
|
PlansLocator.ensurePlansAreSetupCorrectly()
|
||||||
|
|
||||||
Promise.all([
|
Server.server.listen(port, host, function () {
|
||||||
mongodb.connectionPromise,
|
logger.debug(`web starting up, listening on ${host}:${port}`)
|
||||||
mongoose.connectionPromise,
|
logger.debug(`${http.globalAgent.maxSockets} sockets enabled`)
|
||||||
HistoryManager.promises.loadGlobalBlobs(),
|
// wait until the process is ready before monitoring the event loop
|
||||||
])
|
metrics.event_loop.monitor(logger)
|
||||||
.then(async () => {
|
|
||||||
Server.server.listen(port, host, function () {
|
// Record metrics for the total startup time before listening on HTTP.
|
||||||
logger.debug(`web starting up, listening on ${host}:${port}`)
|
metrics.gauge(
|
||||||
logger.debug(`${http.globalAgent.maxSockets} sockets enabled`)
|
'web_startup',
|
||||||
// wait until the process is ready before monitoring the event loop
|
performance.now() - metricsModuleImportStartTime,
|
||||||
metrics.event_loop.monitor(logger)
|
1,
|
||||||
})
|
{ path: 'metricsModuleImportToHTTPListen' }
|
||||||
QueueWorkers.start()
|
)
|
||||||
await Modules.start()
|
})
|
||||||
})
|
try {
|
||||||
.catch(err => {
|
QueueWorkers.start()
|
||||||
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
} catch (err) {
|
||||||
process.exit(1)
|
logger.fatal({ err }, 'failed to start queue processing')
|
||||||
})
|
}
|
||||||
|
try {
|
||||||
|
await Modules.start()
|
||||||
|
} catch (err) {
|
||||||
|
logger.fatal({ err }, 'failed to start web module background jobs')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialise site admin tasks
|
// initialise site admin tasks
|
||||||
Promise.all([
|
SiteAdminHandler.initialise()
|
||||||
mongodb.connectionPromise,
|
|
||||||
mongoose.connectionPromise,
|
|
||||||
HistoryManager.promises.loadGlobalBlobs(),
|
|
||||||
])
|
|
||||||
.then(() => SiteAdminHandler.initialise())
|
|
||||||
.catch(err => {
|
|
||||||
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
|
||||||
process.exit(1)
|
|
||||||
})
|
|
||||||
|
|
||||||
// handle SIGTERM for graceful shutdown in kubernetes
|
|
||||||
process.on('SIGTERM', function (signal) {
|
|
||||||
triggerGracefulShutdown(Server.server, signal)
|
|
||||||
})
|
|
||||||
|
|
||||||
export default Server.server
|
export default Server.server
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ const { promisify, callbackify } = require('util')
|
|||||||
const Settings = require('@overleaf/settings')
|
const Settings = require('@overleaf/settings')
|
||||||
const Views = require('./Views')
|
const Views = require('./Views')
|
||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
|
const Metrics = require('@overleaf/metrics')
|
||||||
|
|
||||||
const MODULE_BASE_PATH = Path.join(__dirname, '/../../../modules')
|
const MODULE_BASE_PATH = Path.join(__dirname, '/../../../modules')
|
||||||
|
|
||||||
@@ -15,7 +16,11 @@ let _viewIncludes = {}
|
|||||||
|
|
||||||
async function modules() {
|
async function modules() {
|
||||||
if (!_modulesLoaded) {
|
if (!_modulesLoaded) {
|
||||||
|
const beforeLoadModules = performance.now()
|
||||||
await loadModules()
|
await loadModules()
|
||||||
|
Metrics.gauge('web_startup', performance.now() - beforeLoadModules, 1, {
|
||||||
|
path: 'loadModules',
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return _modules
|
return _modules
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -372,6 +372,10 @@ if (Settings.enabledServices.includes('web')) {
|
|||||||
metrics.injectMetricsRoute(webRouter)
|
metrics.injectMetricsRoute(webRouter)
|
||||||
metrics.injectMetricsRoute(privateApiRouter)
|
metrics.injectMetricsRoute(privateApiRouter)
|
||||||
|
|
||||||
|
const beforeRouterInitialize = performance.now()
|
||||||
await Router.initialize(webRouter, privateApiRouter, publicApiRouter)
|
await Router.initialize(webRouter, privateApiRouter, publicApiRouter)
|
||||||
|
metrics.gauge('web_startup', performance.now() - beforeRouterInitialize, 1, {
|
||||||
|
path: 'Router.initialize',
|
||||||
|
})
|
||||||
|
|
||||||
export default { app, server }
|
export default { app, server }
|
||||||
|
|||||||
Reference in New Issue
Block a user