[uptime] avoid loading uptime.chunk on every page load (#209815)

Part of https://github.com/elastic/kibana/issues/194171

PR removes all async imports run during uptime plugin setup and start
methods. This causes page load bundle size to increase and accurately
reflect its true size.

### Test instructions
* Start kibana locally
* Open network tab in browser
* Open home page. Verify `uptime.chunk` are not loaded. The screen shots
show the behavior in main where `uptime.chunk` are loaded on home page
<img width="600" alt="Screenshot 2025-02-05 at 9 06 56 AM"
src="https://github.com/user-attachments/assets/14218b85-3814-4e3c-9c04-bd73cf6c4dbd"
/>

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Nathan Reese 2025-02-07 12:37:40 -07:00 committed by GitHub
parent 3ee1fa3f61
commit 8efc247688
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 85 additions and 70 deletions

View file

@ -175,7 +175,7 @@ pageLoadAssetSize:
unifiedHistogram: 19928
unifiedSearch: 71059
upgradeAssistant: 81241
uptime: 37842
uptime: 60000
urlDrilldown: 30063
urlForwarding: 32579
usageCollection: 39762

View file

@ -0,0 +1,60 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type {
UptimeOverviewLocatorInfraParams,
UptimeOverviewLocatorParams,
} from '@kbn/deeplinks-observability';
import { OVERVIEW_ROUTE } from '../../common/constants';
const formatSearchKey = (key: string, value: string) => `${key}: "${value}"`;
function isUptimeOverviewLocatorParams(
args: UptimeOverviewLocatorInfraParams | UptimeOverviewLocatorParams
): args is UptimeOverviewLocatorParams {
return (
(args as UptimeOverviewLocatorParams).search !== undefined ||
(args as UptimeOverviewLocatorParams).dateRangeEnd !== undefined ||
(args as UptimeOverviewLocatorParams).dateRangeStart !== undefined
);
}
export function getLocation(
params: UptimeOverviewLocatorInfraParams | UptimeOverviewLocatorParams
) {
let qs = '';
if (isUptimeOverviewLocatorParams(params)) {
qs = Object.entries(params)
.map(([key, value]) => {
if (value) {
return `${key}=${value}`;
}
})
.join('&');
} else {
const searchParams: string[] = [];
if (params.host) searchParams.push(formatSearchKey('host.name', params.host));
if (params.container) searchParams.push(formatSearchKey('container.id', params.container));
if (params.pod) searchParams.push(formatSearchKey('kubernetes.pod.uid', params.pod));
if (params.ip) {
searchParams.push(formatSearchKey(`host.ip`, params.ip));
searchParams.push(formatSearchKey(`monitor.ip`, params.ip));
}
if (searchParams.length > 0) {
qs = `search=${searchParams.join(' OR ')}`;
}
}
const path = `${OVERVIEW_ROUTE}${qs ? `?${qs}` : ''}`;
return {
app: 'uptime',
path,
state: {},
};
}

View file

@ -11,22 +11,9 @@ import {
type UptimeOverviewLocatorParams,
} from '@kbn/deeplinks-observability';
import type { LocatorDefinition } from '@kbn/share-plugin/common';
import { OVERVIEW_ROUTE } from '../../common/constants';
export type { UptimeOverviewLocatorInfraParams, UptimeOverviewLocatorParams };
const formatSearchKey = (key: string, value: string) => `${key}: "${value}"`;
function isUptimeOverviewLocatorParams(
args: UptimeOverviewLocatorInfraParams | UptimeOverviewLocatorParams
): args is UptimeOverviewLocatorParams {
return (
(args as UptimeOverviewLocatorParams).search !== undefined ||
(args as UptimeOverviewLocatorParams).dateRangeEnd !== undefined ||
(args as UptimeOverviewLocatorParams).dateRangeStart !== undefined
);
}
export class UptimeOverviewLocatorDefinition
implements LocatorDefinition<UptimeOverviewLocatorInfraParams | UptimeOverviewLocatorParams>
{
@ -35,36 +22,7 @@ export class UptimeOverviewLocatorDefinition
public readonly getLocation = async (
params: UptimeOverviewLocatorInfraParams | UptimeOverviewLocatorParams
) => {
let qs = '';
if (isUptimeOverviewLocatorParams(params)) {
qs = Object.entries(params)
.map(([key, value]) => {
if (value) {
return `${key}=${value}`;
}
})
.join('&');
} else {
const searchParams: string[] = [];
if (params.host) searchParams.push(formatSearchKey('host.name', params.host));
if (params.container) searchParams.push(formatSearchKey('container.id', params.container));
if (params.pod) searchParams.push(formatSearchKey('kubernetes.pod.uid', params.pod));
if (params.ip) {
searchParams.push(formatSearchKey(`host.ip`, params.ip));
searchParams.push(formatSearchKey(`monitor.ip`, params.ip));
}
if (searchParams.length > 0) {
qs = `search=${searchParams.join(' OR ')}`;
}
}
const path = `${OVERVIEW_ROUTE}${qs ? `?${qs}` : ''}`;
return {
app: 'uptime',
path,
state: {},
};
const { getLocation } = await import('./get_location');
return getLocation(params);
};
}

View file

@ -68,6 +68,8 @@ import {
uptimeAlertTypeInitializers,
} from './legacy_uptime/lib/alert_types';
import { setStartServices } from './kibana_services';
import { UptimeOverviewLocatorDefinition } from './locators/overview';
import { UptimeDataHelper } from './legacy_uptime/app/uptime_overview_fetcher';
export interface ClientPluginsSetup {
home?: HomePublicPluginSetup;
@ -146,8 +148,6 @@ export class UptimePlugin
}
const getUptimeDataHelper = async () => {
const [coreStart] = await core.getStartServices();
const { UptimeDataHelper } = await import('./legacy_uptime/app/uptime_overview_fetcher');
return UptimeDataHelper(coreStart);
};
@ -243,7 +243,6 @@ export class UptimePlugin
function registerUptimeRoutesWithNavigation(coreStart: CoreStart, plugins: ClientPluginsStart) {
async function getUptimeSections() {
if (coreStart.application.capabilities.uptime?.show) {
const { UptimeOverviewLocatorDefinition } = await import('./locators/overview');
plugins.share.url.locators.create(new UptimeOverviewLocatorDefinition());
return [
@ -304,29 +303,27 @@ function setUptimeAppStatus(
pluginsStart: ClientPluginsStart,
updater: BehaviorSubject<AppUpdater>
) {
import('./legacy_uptime/app/uptime_overview_fetcher').then(({ UptimeDataHelper }) => {
const isEnabled = coreStart.uiSettings.get<boolean>(enableLegacyUptimeApp);
if (isEnabled) {
registerUptimeRoutesWithNavigation(coreStart, pluginsStart);
registerAlertRules(coreStart, pluginsStart, stackVersion, false);
updater.next(() => ({ status: AppStatus.accessible }));
} else {
const hasUptimePrivileges = coreStart.application.capabilities.uptime?.show;
if (hasUptimePrivileges) {
const indexStatusPromise = UptimeDataHelper(coreStart).indexStatus('now-7d/d', 'now/d');
indexStatusPromise.then((indexStatus) => {
if (indexStatus.indexExists) {
registerUptimeRoutesWithNavigation(coreStart, pluginsStart);
updater.next(() => ({ status: AppStatus.accessible }));
registerAlertRules(coreStart, pluginsStart, stackVersion, false);
} else {
updater.next(() => ({ status: AppStatus.inaccessible }));
registerAlertRules(coreStart, pluginsStart, stackVersion, true);
}
});
}
const isEnabled = coreStart.uiSettings.get<boolean>(enableLegacyUptimeApp);
if (isEnabled) {
registerUptimeRoutesWithNavigation(coreStart, pluginsStart);
registerAlertRules(coreStart, pluginsStart, stackVersion, false);
updater.next(() => ({ status: AppStatus.accessible }));
} else {
const hasUptimePrivileges = coreStart.application.capabilities.uptime?.show;
if (hasUptimePrivileges) {
const indexStatusPromise = UptimeDataHelper(coreStart).indexStatus('now-7d/d', 'now/d');
indexStatusPromise.then((indexStatus) => {
if (indexStatus.indexExists) {
registerUptimeRoutesWithNavigation(coreStart, pluginsStart);
updater.next(() => ({ status: AppStatus.accessible }));
registerAlertRules(coreStart, pluginsStart, stackVersion, false);
} else {
updater.next(() => ({ status: AppStatus.inaccessible }));
registerAlertRules(coreStart, pluginsStart, stackVersion, true);
}
});
}
});
}
}
function registerAlertRules(