Skip to content

Commit 8efc247

Browse files
nreesekibanamachineelasticmachine
authored
[uptime] avoid loading uptime.chunk on every page load (#209815)
Part of #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 <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
1 parent 3ee1fa3 commit 8efc247

File tree

4 files changed

+85
-70
lines changed

4 files changed

+85
-70
lines changed

packages/kbn-optimizer/limits.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pageLoadAssetSize:
175175
unifiedHistogram: 19928
176176
unifiedSearch: 71059
177177
upgradeAssistant: 81241
178-
uptime: 37842
178+
uptime: 60000
179179
urlDrilldown: 30063
180180
urlForwarding: 32579
181181
usageCollection: 39762
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import type {
9+
UptimeOverviewLocatorInfraParams,
10+
UptimeOverviewLocatorParams,
11+
} from '@kbn/deeplinks-observability';
12+
import { OVERVIEW_ROUTE } from '../../common/constants';
13+
14+
const formatSearchKey = (key: string, value: string) => `${key}: "${value}"`;
15+
16+
function isUptimeOverviewLocatorParams(
17+
args: UptimeOverviewLocatorInfraParams | UptimeOverviewLocatorParams
18+
): args is UptimeOverviewLocatorParams {
19+
return (
20+
(args as UptimeOverviewLocatorParams).search !== undefined ||
21+
(args as UptimeOverviewLocatorParams).dateRangeEnd !== undefined ||
22+
(args as UptimeOverviewLocatorParams).dateRangeStart !== undefined
23+
);
24+
}
25+
26+
export function getLocation(
27+
params: UptimeOverviewLocatorInfraParams | UptimeOverviewLocatorParams
28+
) {
29+
let qs = '';
30+
31+
if (isUptimeOverviewLocatorParams(params)) {
32+
qs = Object.entries(params)
33+
.map(([key, value]) => {
34+
if (value) {
35+
return `${key}=${value}`;
36+
}
37+
})
38+
.join('&');
39+
} else {
40+
const searchParams: string[] = [];
41+
if (params.host) searchParams.push(formatSearchKey('host.name', params.host));
42+
if (params.container) searchParams.push(formatSearchKey('container.id', params.container));
43+
if (params.pod) searchParams.push(formatSearchKey('kubernetes.pod.uid', params.pod));
44+
if (params.ip) {
45+
searchParams.push(formatSearchKey(`host.ip`, params.ip));
46+
searchParams.push(formatSearchKey(`monitor.ip`, params.ip));
47+
}
48+
if (searchParams.length > 0) {
49+
qs = `search=${searchParams.join(' OR ')}`;
50+
}
51+
}
52+
53+
const path = `${OVERVIEW_ROUTE}${qs ? `?${qs}` : ''}`;
54+
55+
return {
56+
app: 'uptime',
57+
path,
58+
state: {},
59+
};
60+
}

x-pack/solutions/observability/plugins/uptime/public/locators/overview.ts

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,9 @@ import {
1111
type UptimeOverviewLocatorParams,
1212
} from '@kbn/deeplinks-observability';
1313
import type { LocatorDefinition } from '@kbn/share-plugin/common';
14-
import { OVERVIEW_ROUTE } from '../../common/constants';
1514

1615
export type { UptimeOverviewLocatorInfraParams, UptimeOverviewLocatorParams };
1716

18-
const formatSearchKey = (key: string, value: string) => `${key}: "${value}"`;
19-
20-
function isUptimeOverviewLocatorParams(
21-
args: UptimeOverviewLocatorInfraParams | UptimeOverviewLocatorParams
22-
): args is UptimeOverviewLocatorParams {
23-
return (
24-
(args as UptimeOverviewLocatorParams).search !== undefined ||
25-
(args as UptimeOverviewLocatorParams).dateRangeEnd !== undefined ||
26-
(args as UptimeOverviewLocatorParams).dateRangeStart !== undefined
27-
);
28-
}
29-
3017
export class UptimeOverviewLocatorDefinition
3118
implements LocatorDefinition<UptimeOverviewLocatorInfraParams | UptimeOverviewLocatorParams>
3219
{
@@ -35,36 +22,7 @@ export class UptimeOverviewLocatorDefinition
3522
public readonly getLocation = async (
3623
params: UptimeOverviewLocatorInfraParams | UptimeOverviewLocatorParams
3724
) => {
38-
let qs = '';
39-
40-
if (isUptimeOverviewLocatorParams(params)) {
41-
qs = Object.entries(params)
42-
.map(([key, value]) => {
43-
if (value) {
44-
return `${key}=${value}`;
45-
}
46-
})
47-
.join('&');
48-
} else {
49-
const searchParams: string[] = [];
50-
if (params.host) searchParams.push(formatSearchKey('host.name', params.host));
51-
if (params.container) searchParams.push(formatSearchKey('container.id', params.container));
52-
if (params.pod) searchParams.push(formatSearchKey('kubernetes.pod.uid', params.pod));
53-
if (params.ip) {
54-
searchParams.push(formatSearchKey(`host.ip`, params.ip));
55-
searchParams.push(formatSearchKey(`monitor.ip`, params.ip));
56-
}
57-
if (searchParams.length > 0) {
58-
qs = `search=${searchParams.join(' OR ')}`;
59-
}
60-
}
61-
62-
const path = `${OVERVIEW_ROUTE}${qs ? `?${qs}` : ''}`;
63-
64-
return {
65-
app: 'uptime',
66-
path,
67-
state: {},
68-
};
25+
const { getLocation } = await import('./get_location');
26+
return getLocation(params);
6927
};
7028
}

x-pack/solutions/observability/plugins/uptime/public/plugin.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ import {
6868
uptimeAlertTypeInitializers,
6969
} from './legacy_uptime/lib/alert_types';
7070
import { setStartServices } from './kibana_services';
71+
import { UptimeOverviewLocatorDefinition } from './locators/overview';
72+
import { UptimeDataHelper } from './legacy_uptime/app/uptime_overview_fetcher';
7173

7274
export interface ClientPluginsSetup {
7375
home?: HomePublicPluginSetup;
@@ -146,8 +148,6 @@ export class UptimePlugin
146148
}
147149
const getUptimeDataHelper = async () => {
148150
const [coreStart] = await core.getStartServices();
149-
const { UptimeDataHelper } = await import('./legacy_uptime/app/uptime_overview_fetcher');
150-
151151
return UptimeDataHelper(coreStart);
152152
};
153153

@@ -243,7 +243,6 @@ export class UptimePlugin
243243
function registerUptimeRoutesWithNavigation(coreStart: CoreStart, plugins: ClientPluginsStart) {
244244
async function getUptimeSections() {
245245
if (coreStart.application.capabilities.uptime?.show) {
246-
const { UptimeOverviewLocatorDefinition } = await import('./locators/overview');
247246
plugins.share.url.locators.create(new UptimeOverviewLocatorDefinition());
248247

249248
return [
@@ -304,29 +303,27 @@ function setUptimeAppStatus(
304303
pluginsStart: ClientPluginsStart,
305304
updater: BehaviorSubject<AppUpdater>
306305
) {
307-
import('./legacy_uptime/app/uptime_overview_fetcher').then(({ UptimeDataHelper }) => {
308-
const isEnabled = coreStart.uiSettings.get<boolean>(enableLegacyUptimeApp);
309-
if (isEnabled) {
310-
registerUptimeRoutesWithNavigation(coreStart, pluginsStart);
311-
registerAlertRules(coreStart, pluginsStart, stackVersion, false);
312-
updater.next(() => ({ status: AppStatus.accessible }));
313-
} else {
314-
const hasUptimePrivileges = coreStart.application.capabilities.uptime?.show;
315-
if (hasUptimePrivileges) {
316-
const indexStatusPromise = UptimeDataHelper(coreStart).indexStatus('now-7d/d', 'now/d');
317-
indexStatusPromise.then((indexStatus) => {
318-
if (indexStatus.indexExists) {
319-
registerUptimeRoutesWithNavigation(coreStart, pluginsStart);
320-
updater.next(() => ({ status: AppStatus.accessible }));
321-
registerAlertRules(coreStart, pluginsStart, stackVersion, false);
322-
} else {
323-
updater.next(() => ({ status: AppStatus.inaccessible }));
324-
registerAlertRules(coreStart, pluginsStart, stackVersion, true);
325-
}
326-
});
327-
}
306+
const isEnabled = coreStart.uiSettings.get<boolean>(enableLegacyUptimeApp);
307+
if (isEnabled) {
308+
registerUptimeRoutesWithNavigation(coreStart, pluginsStart);
309+
registerAlertRules(coreStart, pluginsStart, stackVersion, false);
310+
updater.next(() => ({ status: AppStatus.accessible }));
311+
} else {
312+
const hasUptimePrivileges = coreStart.application.capabilities.uptime?.show;
313+
if (hasUptimePrivileges) {
314+
const indexStatusPromise = UptimeDataHelper(coreStart).indexStatus('now-7d/d', 'now/d');
315+
indexStatusPromise.then((indexStatus) => {
316+
if (indexStatus.indexExists) {
317+
registerUptimeRoutesWithNavigation(coreStart, pluginsStart);
318+
updater.next(() => ({ status: AppStatus.accessible }));
319+
registerAlertRules(coreStart, pluginsStart, stackVersion, false);
320+
} else {
321+
updater.next(() => ({ status: AppStatus.inaccessible }));
322+
registerAlertRules(coreStart, pluginsStart, stackVersion, true);
323+
}
324+
});
328325
}
329-
});
326+
}
330327
}
331328

332329
function registerAlertRules(

0 commit comments

Comments
 (0)