Skip to content

Commit

Permalink
fix logging execution time for flamegraphs (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: inge4pres <francesco.gualazzi@elastic.co>
  • Loading branch information
inge4pres authored Mar 2, 2022
1 parent f69399f commit e0bf3d9
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/plugins/profiling/server/routes/search_flamechart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,25 @@ export function registerFlameChartSearchRoute(
}
}

const responseBody = logExecutionLatency(logger, 'building Flamegraph data', () => {
return new FlameGraph(
eventsIndex.sampleRate,
totalCount,
stackTraceEvents,
stackTraces,
stackFrames,
executables,
logger
).toElastic();
});
const responseBody = await logExecutionLatency<any>(
logger,
'building Flamegraph data',
() => {
return new Promise((resolve, _) => {
return resolve(
new FlameGraph(
eventsIndex.sampleRate,
totalCount,
stackTraceEvents,
stackTraces,
stackFrames,
executables,
logger
).toElastic()
);
});
}
);

return response.ok({
body: responseBody,
Expand All @@ -307,9 +315,14 @@ export function registerFlameChartSearchRoute(
);
}

function logExecutionLatency<T>(logger: Logger, activity: string, func: () => T): T {
async function logExecutionLatency<T>(
logger: Logger,
activity: string,
func: () => Promise<T>
): Promise<T> {
const start = new Date().getTime();
const result = func();
logger.info(activity + ' took ' + (new Date().getTime() - start) + 'ms');
return result;
return await func().then((res) => {
logger.info(activity + ' took ' + (new Date().getTime() - start) + 'ms');
return res;
});
}

0 comments on commit e0bf3d9

Please sign in to comment.