Skip to content

Commit

Permalink
guard access to methods on variables that depend on initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan authored and dokmic committed May 19, 2021
1 parent 3f8ce24 commit 4d7f9f2
Showing 1 changed file with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ export class HeadlessChromiumDriverFactory {

let browser: puppeteer.Browser;
let page: puppeteer.Page;
let devTools: puppeteer.CDPSession;
let startMetrics: Metrics;
let devTools: puppeteer.CDPSession | undefined;
let startMetrics: Metrics | undefined;

try {
browser = await puppeteer.launch({
Expand Down Expand Up @@ -117,15 +117,19 @@ export class HeadlessChromiumDriverFactory {
const childProcess = {
async kill() {
try {
const endMetrics = await devTools.send('Performance.getMetrics');
const { cpu, cpuInPercentage, memory, memoryInMegabytes } = getMetrics(
startMetrics,
endMetrics
);

apm.currentTransaction?.setLabel('cpu', cpu, false);
apm.currentTransaction?.setLabel('memory', memory, false);
logger.debug(`Chromium consumed CPU ${cpuInPercentage}% Memory ${memoryInMegabytes}MB`);
if (devTools && startMetrics) {
const endMetrics = await devTools.send('Performance.getMetrics');
const { cpu, cpuInPercentage, memory, memoryInMegabytes } = getMetrics(
startMetrics,
endMetrics
);

apm.currentTransaction?.setLabel('cpu', cpu, false);
apm.currentTransaction?.setLabel('memory', memory, false);
logger.debug(
`Chromium consumed CPU ${cpuInPercentage}% Memory ${memoryInMegabytes}MB`
);
}
} catch (error) {
logger.error(error);
}
Expand Down

0 comments on commit 4d7f9f2

Please sign in to comment.