[8.x] [Inventory][ECO] Return metadata values (#195204) (#195538)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Inventory][ECO] Return metadata values
(#195204)](https://github.com/elastic/kibana/pull/195204)

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

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

<!--BACKPORT [{"author":{"name":"Cauê
Marcondes","email":"55978943+cauemarcondes@users.noreply.github.com"},"sourceCommit":{"committedDate":"2024-10-09T07:43:52Z","message":"[Inventory][ECO]
Return metadata values
(#195204)\n\nhttps://github.com/elastic/kibana/issues/194131\r\n\r\nUse
`entity.identityFields` instead of host, container and
service\r\nspecific ones. Get the first environment
available.","sha":"9975fd63d38a4a6baa27ec34c32d49dab53ec854","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.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.16.0"],"title":"[Inventory][ECO]
Return metadata
values","number":195204,"url":"https://github.com/elastic/kibana/pull/195204","mergeCommit":{"message":"[Inventory][ECO]
Return metadata values
(#195204)\n\nhttps://github.com/elastic/kibana/issues/194131\r\n\r\nUse
`entity.identityFields` instead of host, container and
service\r\nspecific ones. Get the first environment
available.","sha":"9975fd63d38a4a6baa27ec34c32d49dab53ec854"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/195204","number":195204,"mergeCommit":{"message":"[Inventory][ECO]
Return metadata values
(#195204)\n\nhttps://github.com/elastic/kibana/issues/194131\r\n\r\nUse
`entity.identityFields` instead of host, container and
service\r\nspecific ones. Get the first environment
available.","sha":"9975fd63d38a4a6baa27ec34c32d49dab53ec854"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Cauê Marcondes <55978943+cauemarcondes@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2024-10-09 20:28:45 +11:00 committed by GitHub
parent 59c3b861a3
commit 128dcb6b4f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 22 additions and 86 deletions

View file

@ -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;
}

View file

@ -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]);

View file

@ -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',
});
});
});

View file

@ -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,
};
};

View file

@ -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';

View file

@ -136,6 +136,7 @@ export {
ENTITY_LAST_SEEN,
ENTITY_TYPE,
SOURCE_DATA_STREAM_TYPE,
ENTITY_IDENTITY_FIELDS,
} from './field_names/elasticsearch';
export {