Keep page load transaction open (#115410)

* keep page load transaction open

* cleanup

* code review

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Liza Katz 2021-10-20 20:43:49 +03:00 committed by GitHub
parent c0b852ff13
commit ab9f8d1b4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,6 +30,7 @@ interface StartDeps {
export class ApmSystem {
private readonly enabled: boolean;
private pageLoadTransaction?: Transaction;
/**
* `apmConfig` would be populated with relevant APM RUM agent
* configuration if server is started with elastic.apm.* config.
@ -49,10 +50,23 @@ export class ApmSystem {
this.addHttpRequestNormalization(apm);
init(apmConfig);
this.pageLoadTransaction = apm.getCurrentTransaction();
// Keep the page load transaction open until all resources finished loading
if (this.pageLoadTransaction && this.pageLoadTransaction.type === 'page-load') {
// @ts-expect-error 2339
this.pageLoadTransaction.block(true);
this.pageLoadTransaction.mark('apm-setup');
}
}
async start(start?: StartDeps) {
if (!this.enabled || !start) return;
if (this.pageLoadTransaction && this.pageLoadTransaction.type === 'page-load') {
this.pageLoadTransaction.mark('apm-start');
}
/**
* Register listeners for navigation changes and capture them as
* route-change transactions after Kibana app is bootstrapped
@ -60,6 +74,11 @@ export class ApmSystem {
start.application.currentAppId$.subscribe((appId) => {
const apmInstance = (window as any).elasticApm;
if (appId && apmInstance && typeof apmInstance.startTransaction === 'function') {
// Close the page load transaction
if (this.pageLoadTransaction && this.pageLoadTransaction.type === 'page-load') {
this.pageLoadTransaction.end();
this.pageLoadTransaction = undefined;
}
apmInstance.startTransaction(`/app/${appId}`, 'route-change', {
managed: true,
canReuse: true,