Skip to content

Commit

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

closes #201124

<img width="1444" alt="Screenshot 2024-11-21 at 12 05 18"
src="https://github.com/user-attachments/assets/16fec2a6-6a59-45e5-8e98-5a961a2b6306">

---------

Co-authored-by: Carlos Crespo <[email protected]>
  • Loading branch information
cauemarcondes and crespocarlos authored Nov 21, 2024
1 parent 76e9359 commit b202b6b
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 b202b6b

Please sign in to comment.