[8.12] [APM] Make useDataViewId optional and remove default values (#173278) (#173295)

# Backport

This will backport the following commits from `main` to `8.12`:
- [[APM] Make `useDataViewId` optional and remove default values
(#173278)](https://github.com/elastic/kibana/pull/173278)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Søren
Louv-Jansen","email":"soren.louv@elastic.co"},"sourceCommit":{"committedDate":"2023-12-13T15:31:56Z","message":"[APM]
Make `useDataViewId` optional and remove default values
(#173278)\n\nfollow-up to
https://github.com/elastic/kibana/pull/170857","sha":"4c79672181d54856d180f7cbb90d5e775ffc60a3","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:APM","release_note:skip","v8.12.0","v8.13.0"],"number":173278,"url":"https://github.com/elastic/kibana/pull/173278","mergeCommit":{"message":"[APM]
Make `useDataViewId` optional and remove default values
(#173278)\n\nfollow-up to
https://github.com/elastic/kibana/pull/170857","sha":"4c79672181d54856d180f7cbb90d5e775ffc60a3"}},"sourceBranch":"main","suggestedTargetBranches":["8.12"],"targetPullRequestStates":[{"branch":"8.12","label":"v8.12.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.13.0","labelRegex":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/173278","number":173278,"mergeCommit":{"message":"[APM]
Make `useDataViewId` optional and remove default values
(#173278)\n\nfollow-up to
https://github.com/elastic/kibana/pull/170857","sha":"4c79672181d54856d180f7cbb90d5e775ffc60a3"}}]}]
BACKPORT-->

Co-authored-by: Søren Louv-Jansen <soren.louv@elastic.co>
This commit is contained in:
Kibana Machine 2023-12-13 12:27:56 -05:00 committed by GitHub
parent 97bd709078
commit 5788e33bf8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 51 additions and 20 deletions

View file

@ -25,7 +25,7 @@ const scenario: Scenario<ApmFields> = async (runOptions) => {
const instances = [...Array(numServices).keys()].map((index) =>
apm
.service({ name: `synth-go-${index}`, environment: ENVIRONMENT, agentName: 'go' })
.service({ name: `synth-node-${index}`, environment: ENVIRONMENT, agentName: 'nodejs' })
.instance('instance')
);
const instanceSpans = (instance: Instance) => {

View file

@ -62,6 +62,10 @@ export function JsonMetricsDashboard(dashboardProps: MetricsDashboardProps) {
});
}, [dataView, serviceName, environment, dashboard]);
if (!dataViewId) {
return null;
}
return (
<DashboardRenderer
getCreationOptions={() =>

View file

@ -31,6 +31,12 @@ describe('Embedded Map', () => {
}),
}));
const mockSpaces = {
getActiveSpace: jest
.fn()
.mockImplementation(() => ({ id: 'mockSpaceId' })),
};
const { findByTestId } = render(
<MemoryRouter
initialEntries={[
@ -38,7 +44,9 @@ describe('Embedded Map', () => {
]}
>
<MockApmPluginContextWrapper>
<KibanaContextProvider services={{ embeddable: mockEmbeddable }}>
<KibanaContextProvider
services={{ embeddable: mockEmbeddable, spaces: mockSpaces }}
>
<EmbeddedMap
selectedMap={MapTypes.Http}
filters={[]}

View file

@ -129,7 +129,7 @@ function EmbeddedMapComponent({
useEffect(() => {
const setLayerList = async () => {
if (embeddable && !isErrorEmbeddable(embeddable)) {
if (embeddable && !isErrorEmbeddable(embeddable) && dataViewId) {
const layerList = await getLayerList({ selectedMap, maps, dataViewId });
await Promise.all([
embeddable.setLayerList(layerList),

View file

@ -66,6 +66,10 @@ export function DiscoverLink({ query = {}, ...rest }: Props) {
const location = useLocation();
const dataViewId = useDataViewId();
if (!dataViewId) {
return null;
}
const href = getDiscoverHref({
basePath: core.http.basePath,
query,

View file

@ -35,7 +35,7 @@ describe('DiscoverLinks', () => {
);
expect(href).toMatchInlineSnapshot(
`"/basepath/app/discover#/?_g=(refreshInterval:(pause:!t,value:0),time:(from:now/w,to:now))&_a=(index:apm_static_data_view_id_default,interval:auto,query:(language:kuery,query:'processor.event:\\"transaction\\" AND transaction.id:\\"8b60bd32ecc6e150\\" AND trace.id:\\"8b60bd32ecc6e1506735a8b6cfcf175c\\"'))"`
`"/basepath/app/discover#/?_g=(refreshInterval:(pause:!t,value:0),time:(from:now/w,to:now))&_a=(index:apm_static_data_view_id_mockSpaceId,interval:auto,query:(language:kuery,query:'processor.event:\\"transaction\\" AND transaction.id:\\"8b60bd32ecc6e150\\" AND trace.id:\\"8b60bd32ecc6e1506735a8b6cfcf175c\\"'))"`
);
});
@ -55,7 +55,7 @@ describe('DiscoverLinks', () => {
);
expect(href).toMatchInlineSnapshot(
`"/basepath/app/discover#/?_g=(refreshInterval:(pause:!t,value:0),time:(from:now/w,to:now))&_a=(index:apm_static_data_view_id_default,interval:auto,query:(language:kuery,query:'span.id:\\"test-span-id\\"'))"`
`"/basepath/app/discover#/?_g=(refreshInterval:(pause:!t,value:0),time:(from:now/w,to:now))&_a=(index:apm_static_data_view_id_mockSpaceId,interval:auto,query:(language:kuery,query:'span.id:\\"test-span-id\\"'))"`
);
});
@ -77,7 +77,7 @@ describe('DiscoverLinks', () => {
);
expect(href).toMatchInlineSnapshot(
`"/basepath/app/discover#/?_g=(refreshInterval:(pause:!t,value:0),time:(from:now/w,to:now))&_a=(index:apm_static_data_view_id_default,interval:auto,query:(language:kuery,query:'service.name:\\"service-name\\" AND error.grouping_key:\\"grouping-key\\"'),sort:('@timestamp':desc))"`
`"/basepath/app/discover#/?_g=(refreshInterval:(pause:!t,value:0),time:(from:now/w,to:now))&_a=(index:apm_static_data_view_id_mockSpaceId,interval:auto,query:(language:kuery,query:'service.name:\\"service-name\\" AND error.grouping_key:\\"grouping-key\\"'),sort:('@timestamp':desc))"`
);
});
@ -100,7 +100,7 @@ describe('DiscoverLinks', () => {
);
expect(href).toMatchInlineSnapshot(
`"/basepath/app/discover#/?_g=(refreshInterval:(pause:!t,value:0),time:(from:now/w,to:now))&_a=(index:apm_static_data_view_id_default,interval:auto,query:(language:kuery,query:'service.name:\\"service-name\\" AND error.grouping_key:\\"grouping-key\\" AND some:kuery-string'),sort:('@timestamp':desc))"`
`"/basepath/app/discover#/?_g=(refreshInterval:(pause:!t,value:0),time:(from:now/w,to:now))&_a=(index:apm_static_data_view_id_mockSpaceId,interval:auto,query:(language:kuery,query:'service.name:\\"service-name\\" AND error.grouping_key:\\"grouping-key\\" AND some:kuery-string'),sort:('@timestamp':desc))"`
);
});
});

View file

@ -69,9 +69,9 @@ export const getSections = ({
allDatasetsLocator: LocatorPublic<AllDatasetsLocatorParams>;
logsLocator: LocatorPublic<LogsLocatorParams>;
nodeLogsLocator: LocatorPublic<NodeLogsLocatorParams>;
dataViewId: string;
dataViewId?: string;
}) => {
if (!transaction) return [];
if (!transaction || !dataViewId) return [];
const hostName = transaction.host?.hostname;
const podId = transaction.kubernetes?.pod?.uid;

View file

@ -30,6 +30,7 @@ import {
} from '../../../utils/test_helpers';
import { TransactionActionMenu } from './transaction_action_menu';
import * as Transactions from './__fixtures__/mock_data';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
const apmContextMock = {
...mockApmPluginContextValue,
@ -60,10 +61,16 @@ history.replace(
);
function Wrapper({ children }: { children?: React.ReactNode }) {
const mockSpaces = {
getActiveSpace: jest.fn().mockImplementation(() => ({ id: 'mockSpaceId' })),
};
return (
<MemoryRouter>
<MockApmPluginContextWrapper value={apmContextMock} history={history}>
{children}
<KibanaContextProvider services={{ spaces: mockSpaces }}>
{children}
</KibanaContextProvider>
</MockApmPluginContextWrapper>
</MemoryRouter>
);

View file

@ -11,15 +11,15 @@ import { getDataViewId } from '../../common/data_view_constants';
import { ApmPluginStartDeps } from '../plugin';
export function useDataViewId() {
const [dataViewId, setDataViewId] = useState<string>(
getDataViewId('default')
);
const [dataViewId, setDataViewId] = useState<string | undefined>();
const { spaces } = useKibana<ApmPluginStartDeps>().services;
useEffect(() => {
const fetchSpaceId = async () => {
const space = await spaces?.getActiveSpace();
setDataViewId(getDataViewId(space?.id ?? 'default'));
if (space?.id) {
setDataViewId(getDataViewId(space?.id));
}
};
fetchSpaceId();

View file

@ -19,6 +19,7 @@ import { Moment } from 'moment-timezone';
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { EuiThemeProvider } from '@kbn/kibana-react-plugin/common';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { MockApmPluginContextWrapper } from '../context/apm_plugin/mock_apm_plugin_context';
import { UrlParamsProvider } from '../context/url_params_context/url_params_context';
@ -61,20 +62,27 @@ export function mockMoment() {
// Useful for getting the rendered href from any kind of link component
export async function getRenderedHref(Component: React.FC, location: Location) {
const mockSpaces = {
getActiveSpace: jest.fn().mockImplementation(() => ({ id: 'mockSpaceId' })),
};
const el = render(
<MemoryRouter initialEntries={[location]}>
<MockApmPluginContextWrapper>
<UrlParamsProvider>
<Component />
</UrlParamsProvider>
<KibanaContextProvider services={{ spaces: mockSpaces }}>
<UrlParamsProvider>
<Component />
</UrlParamsProvider>
</KibanaContextProvider>
</MockApmPluginContextWrapper>
</MemoryRouter>
);
const a = el.container.querySelector('a');
await waitFor(() => {}, { container: a! });
await waitFor(() => el.container.querySelector('a') !== null, {
container: el.container,
});
return a ? a.getAttribute('href') : '';
return el.container.querySelector('a')?.getAttribute('href');
}
export function mockNow(date: string | number | Date) {