Skip to content

Commit

Permalink
[8.17] [Infra] Error for entities API, shown when navigating to infra…
Browse files Browse the repository at this point in the history
…structure entity details (#201136) (#201244)

# Backport

This will backport the following commits from `main` to `8.17`:
- [[Infra] Error for entities API, shown when navigating to
infrastructure entity details
(#201136)](#201136)

<!--- Backport version: 9.4.3 -->

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

<!--BACKPORT [{"author":{"name":"Cauê
Marcondes","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-21T16:52:20Z","message":"[Infra]
Error for entities API, shown when navigating to infrastructure entity
details (#201136)\n\ncloses
https://github.com/elastic/kibana/issues/201124\r\n\r\n<img
width=\"1444\" alt=\"Screenshot 2024-11-21 at 12 05
18\"\r\nsrc=\"https://github.com/user-attachments/assets/16fec2a6-6a59-45e5-8e98-5a961a2b6306\">\r\n\r\n---------\r\n\r\nCo-authored-by:
Carlos Crespo
<[email protected]>","sha":"b202b6ba2d0cf921e67a95f9d369b35684e45a04","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","ci:project-deploy-observability","v8.17.0"],"title":"[Infra]
Error for entities API, shown when navigating to infrastructure entity
details","number":201136,"url":"https://github.com/elastic/kibana/pull/201136","mergeCommit":{"message":"[Infra]
Error for entities API, shown when navigating to infrastructure entity
details (#201136)\n\ncloses
https://github.com/elastic/kibana/issues/201124\r\n\r\n<img
width=\"1444\" alt=\"Screenshot 2024-11-21 at 12 05
18\"\r\nsrc=\"https://github.com/user-attachments/assets/16fec2a6-6a59-45e5-8e98-5a961a2b6306\">\r\n\r\n---------\r\n\r\nCo-authored-by:
Carlos Crespo
<[email protected]>","sha":"b202b6ba2d0cf921e67a95f9d369b35684e45a04"}},"sourceBranch":"main","suggestedTargetBranches":["8.17"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/201136","number":201136,"mergeCommit":{"message":"[Infra]
Error for entities API, shown when navigating to infrastructure entity
details (#201136)\n\ncloses
https://github.com/elastic/kibana/issues/201124\r\n\r\n<img
width=\"1444\" alt=\"Screenshot 2024-11-21 at 12 05
18\"\r\nsrc=\"https://github.com/user-attachments/assets/16fec2a6-6a59-45e5-8e98-5a961a2b6306\">\r\n\r\n---------\r\n\r\nCo-authored-by:
Carlos Crespo
<[email protected]>","sha":"b202b6ba2d0cf921e67a95f9d369b35684e45a04"}},{"branch":"8.17","label":"v8.17.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Cauê Marcondes <[email protected]>
  • Loading branch information
kibanamachine and cauemarcondes authored Nov 21, 2024
1 parent 7739ecf commit cda6f17
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { type InfraMetricsClient } from '../../lib/helpers/get_infra_metrics_cli
import { getDataStreamTypes } from './get_data_stream_types';
import { getHasMetricsData } from './get_has_metrics_data';
import { getLatestEntity } from './get_latest_entity';
import { loggingSystemMock } from '@kbn/core/server/mocks';

jest.mock('./get_has_metrics_data', () => ({
getHasMetricsData: jest.fn(),
Expand All @@ -25,6 +26,7 @@ describe('getDataStreamTypes', () => {
let infraMetricsClient: jest.Mocked<InfraMetricsClient>;
let obsEsClient: jest.Mocked<ObservabilityElasticsearchClient>;
let entityManagerClient: jest.Mocked<EntityClient>;
const logger = loggingSystemMock.createLogger();

beforeEach(() => {
infraMetricsClient = {} as jest.Mocked<InfraMetricsClient>;
Expand All @@ -43,6 +45,7 @@ describe('getDataStreamTypes', () => {
infraMetricsClient,
obsEsClient,
entityManagerClient,
logger,
};

const result = await getDataStreamTypes(params);
Expand All @@ -65,6 +68,7 @@ describe('getDataStreamTypes', () => {
infraMetricsClient,
obsEsClient,
entityManagerClient,
logger,
};

const result = await getDataStreamTypes(params);
Expand All @@ -84,6 +88,7 @@ describe('getDataStreamTypes', () => {
infraMetricsClient,
obsEsClient,
entityManagerClient,
logger,
};

const result = await getDataStreamTypes(params);
Expand All @@ -95,6 +100,7 @@ describe('getDataStreamTypes', () => {
entityId: 'entity123',
entityType: 'host',
entityManagerClient,
logger,
});
});

Expand All @@ -109,6 +115,7 @@ describe('getDataStreamTypes', () => {
infraMetricsClient,
obsEsClient,
entityManagerClient,
logger,
};

const result = await getDataStreamTypes(params);
Expand All @@ -128,6 +135,7 @@ describe('getDataStreamTypes', () => {
infraMetricsClient,
obsEsClient,
entityManagerClient,
logger,
};

const result = await getDataStreamTypes(params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { findInventoryFields } from '@kbn/metrics-data-access-plugin/common';
import { EntityDataStreamType } from '@kbn/observability-shared-plugin/common';
import type { ObservabilityElasticsearchClient } from '@kbn/observability-utils-server/es/client/create_observability_es_client';
import { castArray } from 'lodash';
import { Logger } from '@kbn/logging';
import { type InfraMetricsClient } from '../../lib/helpers/get_infra_metrics_client';
import { getHasMetricsData } from './get_has_metrics_data';
import { getLatestEntity } from './get_latest_entity';
Expand All @@ -21,6 +22,7 @@ interface Params {
infraMetricsClient: InfraMetricsClient;
obsEsClient: ObservabilityElasticsearchClient;
entityManagerClient: EntityClient;
logger: Logger;
}

export async function getDataStreamTypes({
Expand All @@ -30,6 +32,7 @@ export async function getDataStreamTypes({
entityType,
infraMetricsClient,
obsEsClient,
logger,
}: Params) {
const hasMetricsData = await getHasMetricsData({
infraMetricsClient,
Expand All @@ -48,6 +51,7 @@ export async function getDataStreamTypes({
entityId,
entityType,
entityManagerClient,
logger,
});

if (latestEntity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ENTITY_LATEST, entitiesAliasPattern } from '@kbn/entities-schema';
import { type EntityClient } from '@kbn/entityManager-plugin/server/lib/entity_client';
import { ENTITY_TYPE, SOURCE_DATA_STREAM_TYPE } from '@kbn/observability-shared-plugin/common';
import type { ObservabilityElasticsearchClient } from '@kbn/observability-utils-server/es/client/create_observability_es_client';
import type { Logger } from '@kbn/logging';

const ENTITIES_LATEST_ALIAS = entitiesAliasPattern({
type: '*',
Expand All @@ -24,32 +25,38 @@ export async function getLatestEntity({
entityId,
entityType,
entityManagerClient,
logger,
}: {
inventoryEsClient: ObservabilityElasticsearchClient;
entityType: 'host' | 'container';
entityId: string;
entityManagerClient: EntityClient;
logger: Logger;
}): Promise<EntitySourceResponse | undefined> {
const { definitions } = await entityManagerClient.getEntityDefinitions({
builtIn: true,
type: entityType,
});
try {
const { definitions } = await entityManagerClient.getEntityDefinitions({
builtIn: true,
type: entityType,
});

const hostOrContainerIdentityField = definitions[0]?.identityFields?.[0]?.field;
if (hostOrContainerIdentityField === undefined) {
return undefined;
}
const hostOrContainerIdentityField = definitions[0]?.identityFields?.[0]?.field;
if (hostOrContainerIdentityField === undefined) {
return undefined;
}

const response = await inventoryEsClient.esql<{
source_data_stream?: { type?: string | string[] };
}>('get_latest_entities', {
query: `FROM ${ENTITIES_LATEST_ALIAS}
const response = await inventoryEsClient.esql<{
source_data_stream?: { type?: string | string[] };
}>('get_latest_entities', {
query: `FROM ${ENTITIES_LATEST_ALIAS}
| WHERE ${ENTITY_TYPE} == ?
| WHERE ${hostOrContainerIdentityField} == ?
| KEEP ${SOURCE_DATA_STREAM_TYPE}
`,
params: [entityType, entityId],
});
params: [entityType, entityId],
});

return { sourceDataStreamType: response[0].source_data_stream?.type };
return { sourceDataStreamType: response[0].source_data_stream?.type };
} catch (e) {
logger.error(e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export const initEntitiesConfigurationRoutes = (libs: InfraBackendLibs) => {
},
async (requestContext, request, response) => {
const { entityId, entityType } = request.params;
const coreContext = await requestContext.core;
const infraContext = await requestContext.infra;
const [coreContext, infraContext] = await Promise.all([
requestContext.core,
requestContext.infra,
]);

const entityManagerClient = await infraContext.entityManager.getScopedClient({ request });
const infraMetricsClient = await getInfraMetricsClient({
request,
Expand All @@ -63,6 +66,7 @@ export const initEntitiesConfigurationRoutes = (libs: InfraBackendLibs) => {
entityType,
infraMetricsClient,
obsEsClient,
logger,
});

return response.ok({
Expand Down

0 comments on commit cda6f17

Please sign in to comment.