From 511f8d9b1c659dde0946fa7a028348e213731b16 Mon Sep 17 00:00:00 2001 From: Caue Marcondes Date: Mon, 7 Oct 2024 10:47:50 +0100 Subject: [PATCH] [Inventory][ECO] Return metadata values --- .../inventory/common/entities.ts | 8 ++-- .../entities_grid/entity_name/index.tsx | 29 +++++++------- .../public/utils/parse_service_params.test.ts | 39 ------------------- .../public/utils/parse_service_params.ts | 30 -------------- .../common/field_names/elasticsearch.ts | 1 + .../observability_shared/common/index.ts | 1 + 6 files changed, 22 insertions(+), 86 deletions(-) delete mode 100644 x-pack/plugins/observability_solution/inventory/public/utils/parse_service_params.test.ts delete mode 100644 x-pack/plugins/observability_solution/inventory/public/utils/parse_service_params.ts diff --git a/x-pack/plugins/observability_solution/inventory/common/entities.ts b/x-pack/plugins/observability_solution/inventory/common/entities.ts index 01f17807c3486a..218e3d50905a93 100644 --- a/x-pack/plugins/observability_solution/inventory/common/entities.ts +++ b/x-pack/plugins/observability_solution/inventory/common/entities.ts @@ -6,15 +6,16 @@ */ import { ENTITY_LATEST, entitiesAliasPattern } from '@kbn/entities-schema'; import { - CONTAINER_ID, - HOST_NAME, AGENT_NAME, CLOUD_PROVIDER, + CONTAINER_ID, ENTITY_DEFINITION_ID, ENTITY_DISPLAY_NAME, ENTITY_ID, + ENTITY_IDENTITY_FIELDS, ENTITY_LAST_SEEN, ENTITY_TYPE, + HOST_NAME, SERVICE_ENVIRONMENT, SERVICE_NAME, } from '@kbn/observability-shared-plugin/common'; @@ -77,6 +78,7 @@ interface BaseEntity { [ENTITY_TYPE]: EntityType; [ENTITY_DISPLAY_NAME]: string; [ENTITY_DEFINITION_ID]: string; + [ENTITY_IDENTITY_FIELDS]: string[]; } /** @@ -85,7 +87,7 @@ interface BaseEntity { interface ServiceEntity extends BaseEntity { [ENTITY_TYPE]: 'service'; [SERVICE_NAME]: string; - [SERVICE_ENVIRONMENT]?: string | null; + [SERVICE_ENVIRONMENT]?: string | string[] | null; [AGENT_NAME]: string | string[] | null; } diff --git a/x-pack/plugins/observability_solution/inventory/public/components/entities_grid/entity_name/index.tsx b/x-pack/plugins/observability_solution/inventory/public/components/entities_grid/entity_name/index.tsx index 28fe38511fa9f1..debe91d52dec12 100644 --- a/x-pack/plugins/observability_solution/inventory/public/components/entities_grid/entity_name/index.tsx +++ b/x-pack/plugins/observability_solution/inventory/public/components/entities_grid/entity_name/index.tsx @@ -5,19 +5,20 @@ * 2.0. */ -import { EuiLink, EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; -import React, { useCallback } from 'react'; +import { EuiFlexGroup, EuiFlexItem, EuiLink } from '@elastic/eui'; import { - AssetDetailsLocatorParams, ASSET_DETAILS_LOCATOR_ID, - ServiceOverviewParams, - ENTITY_TYPE, + AssetDetailsLocatorParams, ENTITY_DISPLAY_NAME, + ENTITY_IDENTITY_FIELDS, + ENTITY_TYPE, + SERVICE_ENVIRONMENT, + ServiceOverviewParams, } from '@kbn/observability-shared-plugin/common'; +import React, { useCallback } from 'react'; +import { Entity } from '../../../../common/entities'; import { useKibana } from '../../../hooks/use_kibana'; import { EntityIcon } from '../../entity_icon'; -import { Entity } from '../../../../common/entities'; -import { parseServiceParams } from '../../../utils/parse_service_params'; interface EntityNameProps { entity: Entity; @@ -34,23 +35,23 @@ export function EntityName({ entity }: EntityNameProps) { const getEntityRedirectUrl = useCallback(() => { const type = entity[ENTITY_TYPE]; + // For service, host and container type there is only one identity field + const identityField = entity[ENTITY_IDENTITY_FIELDS][0]; // Any unrecognised types will always return undefined switch (type) { case 'host': case 'container': return assetDetailsLocator?.getRedirectUrl({ - assetId: entity[ENTITY_DISPLAY_NAME], + assetId: identityField, assetType: type, }); case 'service': - // For services, the format of the display name is `service.name:service.environment`. - // We just want the first part of the name for the locator. - // TODO: Replace this with a better approach for handling service names. See https://github.com/elastic/kibana/issues/194131 - return serviceOverviewLocator?.getRedirectUrl( - parseServiceParams(entity[ENTITY_DISPLAY_NAME]) - ); + return serviceOverviewLocator?.getRedirectUrl({ + serviceName: identityField, + environment: [entity[SERVICE_ENVIRONMENT] || undefined].flat()[0], + }); } }, [entity, assetDetailsLocator, serviceOverviewLocator]); diff --git a/x-pack/plugins/observability_solution/inventory/public/utils/parse_service_params.test.ts b/x-pack/plugins/observability_solution/inventory/public/utils/parse_service_params.test.ts deleted file mode 100644 index 217b28480feb14..00000000000000 --- a/x-pack/plugins/observability_solution/inventory/public/utils/parse_service_params.test.ts +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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 { parseServiceParams } from './parse_service_params'; - -describe('parseServiceParams', () => { - it('should return only serviceName with a simple name string', () => { - const params = parseServiceParams('service.name'); - - expect(params).toEqual({ serviceName: 'service.name' }); - }); - - it('should return both serviceName and environment with a full name string', () => { - const params = parseServiceParams('service.name:service.environment'); - - expect(params).toEqual({ serviceName: 'service.name', environment: 'service.environment' }); - }); - - it('should ignore multiple colons in the environment portion of the displayName', () => { - const params = parseServiceParams('service.name:synthtrace: service.environment'); - - expect(params).toEqual({ - serviceName: 'service.name', - environment: 'synthtrace: service.environment', - }); - }); - - it('should ignore empty environment names and return only the service.name', () => { - const params = parseServiceParams('service.name:'); - - expect(params).toEqual({ - serviceName: 'service.name', - }); - }); -}); diff --git a/x-pack/plugins/observability_solution/inventory/public/utils/parse_service_params.ts b/x-pack/plugins/observability_solution/inventory/public/utils/parse_service_params.ts deleted file mode 100644 index 637957c5782729..00000000000000 --- a/x-pack/plugins/observability_solution/inventory/public/utils/parse_service_params.ts +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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 type { ServiceOverviewParams } from '@kbn/observability-shared-plugin/common'; - -/** - * Parses a displayName string with the format `service.name:service.environment`, - * returning a valid `ServiceOverviewParams` object. - * @param displayName A string from a `entity.displayName` field. - * @returns - */ -export const parseServiceParams = (displayName: string): ServiceOverviewParams => { - const separatorIndex = displayName.indexOf(':'); - - const hasEnvironmentName = separatorIndex !== -1; - - const serviceName = hasEnvironmentName ? displayName.slice(0, separatorIndex) : displayName; - // Exclude the separator from the sliced string for the environment name. - // If the string is empty however, then we default to undefined. - const environment = (hasEnvironmentName && displayName.slice(separatorIndex + 1)) || undefined; - - return { - serviceName, - environment, - }; -}; diff --git a/x-pack/plugins/observability_solution/observability_shared/common/field_names/elasticsearch.ts b/x-pack/plugins/observability_solution/observability_shared/common/field_names/elasticsearch.ts index 741e9b6b0e2d37..5652671a872813 100644 --- a/x-pack/plugins/observability_solution/observability_shared/common/field_names/elasticsearch.ts +++ b/x-pack/plugins/observability_solution/observability_shared/common/field_names/elasticsearch.ts @@ -153,4 +153,5 @@ export const ENTITY_LAST_SEEN = 'entity.lastSeenTimestamp'; export const ENTITY_FIRST_SEEN = 'entity.firstSeenTimestamp'; export const ENTITY_DISPLAY_NAME = 'entity.displayName'; export const ENTITY_DEFINITION_ID = 'entity.definitionId'; +export const ENTITY_IDENTITY_FIELDS = 'entity.identityFields'; export const SOURCE_DATA_STREAM_TYPE = 'source_data_stream.type'; diff --git a/x-pack/plugins/observability_solution/observability_shared/common/index.ts b/x-pack/plugins/observability_solution/observability_shared/common/index.ts index a33c0aa99d1e2a..a359a3d862ce97 100644 --- a/x-pack/plugins/observability_solution/observability_shared/common/index.ts +++ b/x-pack/plugins/observability_solution/observability_shared/common/index.ts @@ -136,6 +136,7 @@ export { ENTITY_LAST_SEEN, ENTITY_TYPE, SOURCE_DATA_STREAM_TYPE, + ENTITY_IDENTITY_FIELDS, } from './field_names/elasticsearch'; export {