forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Stack Monitoring] Remove 'observability' and 'observabilityShared' p…
…lugin dependencies (elastic#203492) ### Summary A recent [bug](elastic#199902) that affected some of the pages in Stack Monitoring was caused by changes related to the locators of the logs-related apps. The goal of this PR is to reduce the number of Observability dependencies that could potentially cause issues in the app by removing the `observability` and `observabilityShared` plugin dependencies from the `monitoring` plugin. Currently, the `monitoring` plugin is [categorised as observability](https://github.com/elastic/kibana/blob/main/x-pack/plugins/monitoring/kibana.jsonc#L7) but when the dependency on the `infra` plugin is removed, it can be marked as a `platform` plugin. ### Notes for reviewers - The components used to render the header menu as well as the [use_track_metric](https://github.com/elastic/kibana/pull/203492/files#diff-7e39fc60ca80ee551d824ca97f9f879e3364a368a5736cf9178b5943a12ca7a7) hook were copied from the `observabilityShared` plugin - There should be no UX and functionality changes in the stack monitoring header - Usage collection could be verified by searching for UI counters sent by the cluster created for this PR, once telemetry has been sent ### Testing The stateful environment deployed by this PR includes logs and metrics for stack monitoring. Please make sure to select a larger time range (e.g. last 14 days). --------- Co-authored-by: kibanamachine <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
305bb1b
commit 7e2f67e
Showing
15 changed files
with
192 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
x-pack/plugins/monitoring/public/application/hooks/use_track_metric.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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 { useEffect, useMemo } from 'react'; | ||
import { METRIC_TYPE, UiCounterMetricType } from '@kbn/analytics'; | ||
import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/public'; | ||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
import { USAGE_COLLECTION_APP_NAME } from '../../../common/constants'; | ||
|
||
/** | ||
* Note: The usage_collection plugin will take care of sending this data to the telemetry server. | ||
* You can find the metrics that are collected by these hooks in Stack Telemetry. | ||
* Search the index `kibana-ui-counter`. You can filter for `eventName` and/or `appName`. | ||
*/ | ||
|
||
interface TrackOptions { | ||
metricType?: UiCounterMetricType; | ||
delay?: number; // in ms | ||
} | ||
type EffectDeps = unknown[]; | ||
|
||
interface ServiceDeps { | ||
usageCollection: UsageCollectionSetup; // TODO: This should really be start. Looking into it. | ||
} | ||
|
||
export type TrackMetricOptions = TrackOptions & { metric: string }; | ||
export type UiTracker = ReturnType<typeof useUiTracker>; | ||
export type TrackEvent = (options: TrackMetricOptions) => void; | ||
|
||
export { METRIC_TYPE }; | ||
|
||
export function useUiTracker<Services extends ServiceDeps>(): TrackEvent { | ||
const reportUiCounter = useKibana<Services>().services?.usageCollection?.reportUiCounter; | ||
const trackEvent = useMemo(() => { | ||
return ({ metric, metricType = METRIC_TYPE.COUNT }: TrackMetricOptions) => { | ||
if (reportUiCounter) { | ||
reportUiCounter(USAGE_COLLECTION_APP_NAME, metricType, metric); | ||
} | ||
}; | ||
}, [reportUiCounter]); | ||
return trackEvent; | ||
} | ||
|
||
export function useTrackMetric<Services extends ServiceDeps>( | ||
{ metric, metricType = METRIC_TYPE.COUNT, delay = 0 }: TrackMetricOptions, | ||
effectDependencies: EffectDeps = [] | ||
) { | ||
const reportUiCounter = useKibana<Services>().services?.usageCollection?.reportUiCounter; | ||
|
||
useEffect(() => { | ||
if (!reportUiCounter) { | ||
// eslint-disable-next-line no-console | ||
console.log( | ||
'usageCollection.reportUiCounter is unavailable. Ensure this is setup via <KibanaContextProvider />.' | ||
); | ||
} else { | ||
let decoratedMetric = metric; | ||
if (delay > 0) { | ||
decoratedMetric += `__delayed_${delay}ms`; | ||
} | ||
const id = setTimeout( | ||
() => reportUiCounter(USAGE_COLLECTION_APP_NAME, metricType, decoratedMetric), | ||
Math.max(delay, 0) | ||
); | ||
return () => clearTimeout(id); | ||
} | ||
// the dependencies are managed externally | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, effectDependencies); | ||
} | ||
|
||
/** | ||
* useTrackPageview is a convenience wrapper for tracking a pageview | ||
* Its metrics will be found at: | ||
* stack_stats.kibana.plugins.ui_metric.{app}.pageview__{path}(__delayed_{n}ms)? | ||
*/ | ||
type TrackPageviewProps = TrackOptions & { path: string }; | ||
|
||
export function useTrackPageview<Services extends ServiceDeps>( | ||
{ path, ...rest }: TrackPageviewProps, | ||
effectDependencies: EffectDeps = [] | ||
) { | ||
useTrackMetric<Services>({ ...rest, metric: `pageview__${path}` }, effectDependencies); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
x-pack/plugins/monitoring/public/components/header_menu/header_menu_portal.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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 { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import HeaderMenuPortal from './header_menu_portal'; | ||
import { themeServiceMock } from '@kbn/core/public/mocks'; | ||
|
||
describe('HeaderMenuPortal', () => { | ||
describe('when unmounted', () => { | ||
it('calls setHeaderActionMenu with undefined', () => { | ||
const setHeaderActionMenu = jest.fn(); | ||
const theme$ = themeServiceMock.createTheme$(); | ||
|
||
const { unmount } = render( | ||
<HeaderMenuPortal setHeaderActionMenu={setHeaderActionMenu} theme$={theme$}> | ||
test | ||
</HeaderMenuPortal> | ||
); | ||
|
||
unmount(); | ||
|
||
expect(setHeaderActionMenu).toHaveBeenCalledWith(undefined); | ||
}); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
x-pack/plugins/monitoring/public/components/header_menu/header_menu_portal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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 React, { useEffect, useMemo } from 'react'; | ||
import { createHtmlPortalNode, InPortal, OutPortal } from 'react-reverse-portal'; | ||
import { toMountPoint } from '@kbn/kibana-react-plugin/public'; | ||
import type { HeaderMenuPortalProps } from '../../types'; | ||
|
||
// eslint-disable-next-line import/no-default-export | ||
export default function HeaderMenuPortal({ | ||
children, | ||
setHeaderActionMenu, | ||
theme$, | ||
}: HeaderMenuPortalProps) { | ||
const portalNode = useMemo(() => createHtmlPortalNode(), []); | ||
|
||
useEffect(() => { | ||
setHeaderActionMenu((element) => { | ||
const mount = toMountPoint(<OutPortal node={portalNode} />, { theme$ }); | ||
return mount(element); | ||
}); | ||
|
||
return () => { | ||
portalNode.unmount(); | ||
setHeaderActionMenu(undefined); | ||
}; | ||
}, [portalNode, setHeaderActionMenu, theme$]); | ||
|
||
return <InPortal node={portalNode}>{children}</InPortal>; | ||
} |
20 changes: 20 additions & 0 deletions
20
x-pack/plugins/monitoring/public/components/header_menu/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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 React, { lazy, Suspense } from 'react'; | ||
import { EuiLoadingSpinner } from '@elastic/eui'; | ||
import { HeaderMenuPortalProps } from '../../types'; | ||
|
||
const HeaderMenuPortalLazy = lazy(() => import('./header_menu_portal')); | ||
|
||
export function HeaderMenuPortal(props: HeaderMenuPortalProps) { | ||
return ( | ||
<Suspense fallback={<EuiLoadingSpinner />}> | ||
<HeaderMenuPortalLazy {...props} /> | ||
</Suspense> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters