Skip to content

Commit

Permalink
feat: add metric to count number of contexts cleaned up for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
albanm committed Apr 2, 2024
1 parent fe9d9d6 commit 25fae20
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
21 changes: 18 additions & 3 deletions server/utils/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const config = require('config')
const puppeteer = require('puppeteer')
const genericPool = require('generic-pool')
const createError = require('http-errors')
const prometheus = require('./prometheus')
const debug = require('debug')('capture')

const contextFactory = {
Expand Down Expand Up @@ -127,9 +128,22 @@ exports.withPage = async (target, lang, timezone, cookies, viewport, animate, ca
try {
if (cookies || config.concurrencyPublic === 0) {
debug(`[${target}] use incognito context from pool`)
context = await _contextPool.acquire()
timer.step('acquire-context')
page = await context.newPage()
let pageAttempts = 0
while (!page) {
pageAttempts++
context = await _contextPool.acquire()
timer.step('acquire-context')
try {
page = await context.newPage()
} catch (err) {
if (pageAttempts === config.concurrency) {
throw err
} else {
debug(`[${target}] delete incognito context from pool because it couldn't be used to open a page`)
await _contextPool.destroy(context)
}
}
}
debugPage(page)
timer.step('newPage')
} else {
Expand Down Expand Up @@ -174,6 +188,7 @@ const safeCleanContext = async (page, cookies, context, openSuccess) => {
'timed out while cleaning page context'
)
await _contextPool.release(context)
prometheus.contextsCleanups.inc()
} catch (err) {
console.error('Failed to clean page properly, do not reuse this context', err)
await _contextPool.destroy(context)
Expand Down
5 changes: 5 additions & 0 deletions server/utils/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ exports.tasks = new client.Histogram({
labelNames: ['step', 'type'],
registers: [localRegister]
})
exports.contextsCleanups = new client.Counter({
name: 'df_capture_contexts_cleanups',
help: 'Number of times a browser context was cleaned up for later reuse',
registers: [localRegister]
})

exports.acquireContextPending = new client.Gauge({
name: 'df_capture_acquire_context_pending',
Expand Down

0 comments on commit 25fae20

Please sign in to comment.