From 8c5497d95ebb50c4da274a17ff16bae9bfd96d94 Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Wed, 29 Oct 2025 13:01:59 +0100 Subject: [PATCH] [Metrics][Discover] Discover input$ observable emission lost fix (#241092) fixes [240429](https://github.com/elastic/kibana/issues/240429) ## Summary This PR fixes a bug where the Discover `input$` observable emissions were not always captured by the Metrics Experience, depending on the interval between the observable emission and the subscription in the `use_lens_props` hook. ### Before ![obs-bug](https://github.com/user-attachments/assets/a45d26ec-c3ee-4f67-9484-fb5864f14b3c) ### After ![obs-fix](https://github.com/user-attachments/assets/0074ec26-f2b2-4535-847b-fff8a466fef5) ## How to test - Start a local Kibana instance and point it to an oblt cluster - the bug is more likely to occur when the performance is worse ```yml feature_flags.overrides: metricsExperienceEnabled: true ``` - Navigate to Discover and Switch to ESQL mode - Change the date picker to a range with no metrics data, then change it again to a range where metrics data is available. (cherry picked from commit 2ad7f51bc95dd4392a4c6cdec16a25650762aefc) # Conflicts: # src/platform/packages/shared/kbn-unified-metrics-grid/src/components/metrics_experience_grid.tsx --- .../src/components/metrics_experience_grid.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/platform/packages/shared/kbn-unified-metrics-grid/src/components/metrics_experience_grid.tsx b/src/platform/packages/shared/kbn-unified-metrics-grid/src/components/metrics_experience_grid.tsx index 9a2bcee1dea89..34f672d913e82 100644 --- a/src/platform/packages/shared/kbn-unified-metrics-grid/src/components/metrics_experience_grid.tsx +++ b/src/platform/packages/shared/kbn-unified-metrics-grid/src/components/metrics_experience_grid.tsx @@ -22,7 +22,7 @@ import { useEuiTheme, type EuiFlexGridProps, } from '@elastic/eui'; -import { Subject } from 'rxjs'; +import { Subject, shareReplay } from 'rxjs'; import { PAGE_SIZE } from '../common/constants'; import { MetricsGrid } from './metrics_grid'; import { Pagination } from './pagination'; @@ -62,11 +62,18 @@ export const MetricsExperienceGrid = ({ [originalInput$] ); - const discoverFetch$ = useFetch({ + const baseFetch$ = useFetch({ input$, beforeFetch: updateTimeRange, }); + const discoverFetch$ = useMemo( + // Buffer and replay emissions to child components that subscribe in later useEffects + // without this, child components would miss emissions that occurred before they subscribed + () => baseFetch$.pipe(shareReplay({ bufferSize: 1, refCount: false })), + [baseFetch$] + ); + const indexPattern = useMemo(() => dataView?.getIndexPattern() ?? 'metrics-*', [dataView]); const { data: fields = [], isFetching: isFieldsLoading } = useMetricFieldsQuery({ index: indexPattern,