grafana/public/app/core/utils/metrics.ts
Marcus Andersson 047763978d
Instrumentation: Measure app init load times (#67900)
* adding load time metrics for app init.

* chore: add the `metrics.ts` to the CODEOWNERS

* moved file according to PR feedback.

---------

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>
2023-05-10 11:03:15 +02:00

36 lines
925 B
TypeScript

import { reportPerformance } from '../services/echo/EchoSrv';
export function startMeasure(eventName: string) {
if (!performance) {
return;
}
try {
performance.mark(`${eventName}_started`);
} catch (error) {
console.error(`[Metrics] Failed to startMeasure ${eventName}`, error);
}
}
export function stopMeasure(eventName: string) {
if (!performance) {
return;
}
try {
const started = `${eventName}_started`;
const completed = `${eventName}_completed`;
const measured = `${eventName}_measured`;
performance.mark(completed);
const measure = performance.measure(measured, started, completed);
reportPerformance(`${eventName}_ms`, measure.duration);
performance.clearMarks(started);
performance.clearMarks(completed);
performance.clearMeasures(measured);
} catch (error) {
console.error(`[Metrics] Failed to stopMeasure ${eventName}`, error);
}
}