Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] [Infra] Error for entities API, shown when navigating to infrastructure entity details (#201136) #201246

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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