Skip to content

Commit 28cf679

Browse files
Add null checks for Logs locators (#242116)
Resolves elastic/observability-error-backlog#116 We're seeing a few `TypeError: Cannot read properties of undefined (reading 'getRedirectUrl')` errors coming from Kibana, specifically from `logs` app. Most of the errors are coming from clusters on `8.15`, it's hard to pinpoint the specific code that triggers the error as there are no stack traces, but `get_logs_locators.ts` file seems like it could be the root cause as it has non-null assertions. While `8.15`-`8.18` branchs are already closed and we cannot fix it there, the same code is present in `8.19` versions as well. I've removed the assertions and adjusted the code to expect optional locators and fallback gracefully.
1 parent c6c9a1e commit 28cf679

File tree

11 files changed

+27
-26
lines changed

11 files changed

+27
-26
lines changed

x-pack/platform/plugins/shared/fleet/public/custom_logs_assets_extension.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const CustomLogsAssetsExtension: PackageAssetsComponent = () => {
2424
const views: CustomAssetsAccordionProps['views'] = [
2525
{
2626
name: i18n.translate('xpack.fleet.assets.customLogs.name', { defaultMessage: 'Logs' }),
27-
url: logsLocator.getRedirectUrl({}),
27+
url: logsLocator?.getRedirectUrl({}) ?? '',
2828
description: i18n.translate('xpack.fleet.assets.customLogs.description', {
2929
defaultMessage: 'View Custom logs data in Logs Explorer',
3030
}),

x-pack/platform/plugins/shared/logs_shared/common/locators/get_logs_locators.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import { TRACE_LOGS_LOCATOR_ID } from './trace_logs_locator';
1515

1616
export const getLogsLocatorsFromUrlService = (urlService: UrlService) => {
1717
const discoverLogsLocator =
18-
urlService.locators.get<DiscoverLogsLocatorParams>(DISCOVER_LOGS_LOCATOR_ID)!;
19-
const logsLocator = urlService.locators.get<LogsLocatorParams>(LOGS_LOCATOR_ID)!;
20-
const nodeLogsLocator = urlService.locators.get<NodeLogsLocatorParams>(NODE_LOGS_LOCATOR_ID)!;
21-
const traceLogsLocator = urlService.locators.get<TraceLogsLocatorParams>(TRACE_LOGS_LOCATOR_ID)!;
18+
urlService.locators.get<DiscoverLogsLocatorParams>(DISCOVER_LOGS_LOCATOR_ID);
19+
const logsLocator = urlService.locators.get<LogsLocatorParams>(LOGS_LOCATOR_ID);
20+
const nodeLogsLocator = urlService.locators.get<NodeLogsLocatorParams>(NODE_LOGS_LOCATOR_ID);
21+
const traceLogsLocator = urlService.locators.get<TraceLogsLocatorParams>(TRACE_LOGS_LOCATOR_ID);
2222

2323
return {
2424
discoverLogsLocator,

x-pack/solutions/observability/plugins/apm/public/components/app/service_overview/service_overview_instances_table/instance_actions_menu/menu_sections.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function getMenuSections({
5050
onFilterByInstanceClick: () => void;
5151
metricsHref: string;
5252
allDatasetsLocator: LocatorPublic<AllDatasetsLocatorParams>;
53-
nodeLogsLocator: LocatorPublic<NodeLogsLocatorParams>;
53+
nodeLogsLocator?: LocatorPublic<NodeLogsLocatorParams>;
5454
assetDetailsLocator?: AssetDetailsLocator;
5555
}) {
5656
const podId = instanceDetails.kubernetes?.pod?.uid;
@@ -60,13 +60,13 @@ export function getMenuSections({
6060
: undefined;
6161
const infraMetricsQuery = getInfraMetricsQuery(instanceDetails['@timestamp']);
6262

63-
const podLogsHref = nodeLogsLocator.getRedirectUrl({
63+
const podLogsHref = nodeLogsLocator?.getRedirectUrl({
6464
nodeField: findInventoryFields('pod').id,
6565
nodeId: podId!,
6666
time,
6767
});
6868

69-
const containerLogsHref = nodeLogsLocator.getRedirectUrl({
69+
const containerLogsHref = nodeLogsLocator?.getRedirectUrl({
7070
nodeField: findInventoryFields('container').id,
7171
nodeId: containerId!,
7272
time,

x-pack/solutions/observability/plugins/apm/public/components/shared/transaction_action_menu/sections.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,23 @@ export const getSections = ({
8585
);
8686

8787
// Logs hrefs
88-
const podLogsHref = logsLocators.nodeLogsLocator.getRedirectUrl({
88+
const podLogsHref = logsLocators.nodeLogsLocator?.getRedirectUrl({
8989
nodeField: findInventoryFields('pod').id,
9090
nodeId: podId!,
9191
time,
9292
});
93-
const containerLogsHref = logsLocators.nodeLogsLocator.getRedirectUrl({
93+
const containerLogsHref = logsLocators.nodeLogsLocator?.getRedirectUrl({
9494
nodeField: findInventoryFields('container').id,
9595
nodeId: containerId!,
9696
time,
9797
});
98-
const hostLogsHref = logsLocators.nodeLogsLocator.getRedirectUrl({
98+
const hostLogsHref = logsLocators.nodeLogsLocator?.getRedirectUrl({
9999
nodeField: findInventoryFields('host').id,
100100
nodeId: hostName!,
101101
time,
102102
});
103103

104-
const traceLogsHref = logsLocators.traceLogsLocator.getRedirectUrl({
104+
const traceLogsHref = logsLocators.traceLogsLocator?.getRedirectUrl({
105105
traceId: transaction.trace.id!,
106106
time,
107107
});

x-pack/solutions/observability/plugins/infra/public/alerting/log_threshold/log_threshold_rule_type.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const logThresholdDefaultRecoveryMessage = i18n.translate(
3434
'xpack.infra.logs.alerting.threshold.defaultRecoveryMessage',
3535
{
3636
defaultMessage: `Recovered: '{{context.reason}}'
37-
37+
3838
'{{rule.name}}' has recovered.
3939
4040
[View alert details]('{{context.alertDetailsUrl}}')
@@ -73,7 +73,7 @@ export function createLogThresholdRuleType(
7373
defaultActionMessage: logThresholdDefaultActionMessage,
7474
defaultRecoveryMessage: logThresholdDefaultRecoveryMessage,
7575
requiresAppContext: false,
76-
format: createRuleFormatter(logsLocator),
76+
format: logsLocator ? createRuleFormatter(logsLocator) : () => ({ reason: '' }),
7777
priority: 30,
7878
};
7979
}

x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/logs/logs.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const Logs = () => {
4242
share: { url },
4343
},
4444
} = useKibanaContextForPlugin();
45-
const { logsLocator } = getLogsLocatorsFromUrlService(url)!;
45+
const { logsLocator } = getLogsLocatorsFromUrlService(url);
4646
const [textQuery, setTextQuery] = useState(urlState?.logsSearch ?? '');
4747
const [textQueryDebounced, setTextQueryDebounced] = useState(urlState?.logsSearch ?? '');
4848

@@ -87,7 +87,7 @@ export const Logs = () => {
8787
nodeId: asset.id,
8888
filter: textQueryDebounced,
8989
});
90-
return logsLocator.getRedirectUrl({
90+
return logsLocator?.getRedirectUrl({
9191
filter: nodeQuery.query,
9292
timeRange: {
9393
startTime: state.startTimestamp,

x-pack/solutions/observability/plugins/infra/public/pages/link_to/redirect_to_logs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const RedirectToLogs = () => {
2323
const time = getTimeFromLocation(location);
2424

2525
useEffect(() => {
26-
logsLocator.navigate(
26+
logsLocator?.navigate(
2727
{
2828
time,
2929
filter,

x-pack/solutions/observability/plugins/infra/public/pages/link_to/redirect_to_node_logs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const RedirectToNodeLogs = ({
3535
const time = getTimeFromLocation(location);
3636

3737
useEffect(() => {
38-
nodeLogsLocator.navigate(
38+
nodeLogsLocator?.navigate(
3939
{
4040
nodeField: findInventoryFields(nodeType).id,
4141
nodeId,

x-pack/solutions/observability/plugins/infra/public/pages/metrics/inventory_view/components/waffle/node_context_menu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const NodeContextMenu: React.FC<Props & { theme?: EuiTheme }> = withTheme
124124
defaultMessage: '{inventoryName} logs',
125125
values: { inventoryName: inventoryModel.singularDisplayName },
126126
}),
127-
href: nodeLogsLocator.getRedirectUrl({
127+
href: nodeLogsLocator?.getRedirectUrl({
128128
nodeField: findInventoryFields(nodeType).id,
129129
nodeId: node.id,
130130
time: currentTime,

x-pack/solutions/observability/plugins/infra/public/utils/logs_overview_fetchers.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ export function getLogsOverviewDataFetcher(
7070
const { logsLocator } = getLogsLocatorsFromUrlService(share.url);
7171
const timeSpanInMinutes = (params.absoluteTime.end - params.absoluteTime.start) / (1000 * 60);
7272

73-
const appLink = logsLocator.getRedirectUrl({
74-
timeRange: {
75-
startTime: params.absoluteTime.start,
76-
endTime: params.absoluteTime.end,
77-
},
78-
});
73+
const appLink =
74+
logsLocator?.getRedirectUrl({
75+
timeRange: {
76+
startTime: params.absoluteTime.start,
77+
endTime: params.absoluteTime.end,
78+
},
79+
}) ?? '';
7980

8081
return {
8182
appLink,

0 commit comments

Comments
 (0)