Skip to content
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 @@ -6,3 +6,5 @@
*/

export const OBSERVABILITY_NAVIGATION_OVERRIDES = 'observability-navigation-overrides';
export const OBSERVABILITY_ENTITY_DEFINITIONS = 'observability-entity-definitions';
export const OBSERVABILITY_METRIC_DEFINITIONS = 'observability-metric-definitions';
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
interface NavigationItemBase {
id: string;
title: string;
entityType?: string;
entityId?: string;
dashboardId?: string;
order?: number;
}

export interface DynamicNavigationItem extends NavigationItemBase {
Expand All @@ -19,3 +20,75 @@ export interface DynamicNavigationItem extends NavigationItemBase {
export interface ObservabilityDynamicNavigation extends NavigationItemBase {
subItems?: DynamicNavigationItem[];
}

/*
* 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.
*/

export interface Attribute {
ref: string;
requirement_level?:
| string
| {
conditionally_required: string;
};
}

export interface RelationShipAttributeMapping {
source_attribute: string;
target_attribute: string;
}

export interface Relationship {
type: string;
target: string;
brief?: string;
attribute_mapping?: RelationShipAttributeMapping;
}

export interface EntityDefinition {
id: string;
type: string;
stability: string;
name: string;
brief?: string;
attributes?: Attribute[];

// custom attributes
relationships?: Relationship[];
}

export interface MetricDefinition {
id: string;
type: 'metric';
metric_name: string;
stability: 'development' | 'stable' | 'experimental';

brief?: string;
entity_associations?: string[];
note?: string;
instrument: 'gauge' | 'counter' | 'updowncounter';
unit?: string;
attributes?: Attribute[];
}

export type MetricDefinitionsResponse = Pick<
MetricDefinition,
'id' | 'type' | 'instrument' | 'unit'
> & { metricName: string };

export interface EntityDefinitionsResponse {
id: string;
name: string;
attributes: string[];
query?: string;
metrics: MetricDefinitionsResponse[];
relationships: string[];
}

export interface EnrichedEntityDefinitionsResponse extends EntityDefinitionsResponse {
navigation?: ObservabilityDynamicNavigation;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
import { PluginInitializer, PluginInitializerContext } from '@kbn/core/public';
import { Plugin } from './plugin';
import { ObservabilityNavigationPluginSetup, ObservabilityNavigationPluginStart } from './types';
import { ObservabilityDynamicNavigation } from '../common/types';
import {
ObservabilityDynamicNavigation,
EnrichedEntityDefinitionsResponse,
EntityDefinitionsResponse,
} from '../common/types';

export type {
ObservabilityNavigationPluginSetup,
ObservabilityNavigationPluginStart,
ObservabilityDynamicNavigation,
EnrichedEntityDefinitionsResponse,
EntityDefinitionsResponse,
};

export const plugin: PluginInitializer<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const createObservabilityNavigationItemsObservable = once(
): Observable<ObservabilityDynamicNavigation[]> => {
return from(
repositoryClient
.fetch('GET /internal/observability_navigation', {
.fetch('GET /internal/observability/navigation', {
signal: new AbortController().signal,
})
.then(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
CoreStart,
Plugin,
Logger,
KibanaRequest,
} from '@kbn/core/server';
import { registerRoutes } from '@kbn/server-route-repository';
import { mapValues } from 'lodash';
Expand All @@ -21,8 +22,18 @@ import {
ObservabilityNavigationPluginStartDependencies,
} from './types';
import { observabilityNavigationRouteRepository } from './routes';
import { ObservabilityNavigationRouteHandlerResources } from './routes/types';
import { navigationOverrides, createNavigationOverrides } from './saved_objects';
import {
ObservabilityNavigationRouteHandlerResources,
RouteHandlerScopedClients,
} from './routes/types';
import {
navigationOverrides,
createNavigationOverrides,
createEntityDefinitions,
createMetricDefinitions,
observabilityEntityDefinitions,
observabilityMetricDefinitions,
} from './saved_objects';

export class ObservabilityNavigationPlugin
implements
Expand All @@ -46,8 +57,12 @@ export class ObservabilityNavigationPlugin
plugins: ObservabilityNavigationPluginSetupDependencies
) {
core.savedObjects.registerType(navigationOverrides);
core.savedObjects.registerType(observabilityEntityDefinitions);
core.savedObjects.registerType(observabilityMetricDefinitions);

createNavigationOverrides(core);
createEntityDefinitions(core);
createMetricDefinitions(core);

const routeHandlerPlugins = mapValues(plugins, (value, key) => {
return {
Expand Down Expand Up @@ -78,6 +93,18 @@ export class ObservabilityNavigationPlugin
repository: observabilityNavigationRouteRepository,
dependencies: {
plugins: withCore,
getScopedClients: async ({
request,
}: {
request: KibanaRequest;
}): Promise<RouteHandlerScopedClients> => {
const [coreStart, plugin] = await core.getStartServices();
return {
scopedClusterClient: coreStart.elasticsearch.client.asScoped(request),
soClient: coreStart.savedObjects.getScopedClient(request),
packageClient: plugin.fleet?.packageService.asScoped(request),
};
},
},
logger: this.logger,
runDevModeChecks: this.isDev,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
*/

import { internalSetupRoutes } from './internal/setup/route';
import { internalEntityDefinitionRoutes } from './internal/entity_definitions/route';

export const observabilityNavigationRouteRepository = {
...internalSetupRoutes,
...internalEntityDefinitionRoutes,
};

export type ObservabilityNavigationRouteRepository = typeof observabilityNavigationRouteRepository;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 {
EnrichedEntityDefinitionsResponse,
EntityDefinitionsResponse,
ObservabilityDynamicNavigation,
} from '../../../../common/types';

export async function enrichEntityDefinitions(
entityDefinitions: EntityDefinitionsResponse[],
navigation: ObservabilityDynamicNavigation[]
): Promise<EnrichedEntityDefinitionsResponse[]> {
const navigationItemsMap = new Map<string, ObservabilityDynamicNavigation>();

for (const item of navigation) {
if (item.entityId) {
navigationItemsMap.set(item.entityId, item);
}
for (const subItem of item.subItems ?? []) {
if (subItem.entityId) {
navigationItemsMap.set(subItem.entityId, subItem);
}
}
}

return entityDefinitions.map((definition) => {
const navigationItem = navigationItemsMap.get(definition.id);
if (navigationItem) {
return {
...definition,
navigation: {
id: navigationItem.id,
title: navigationItem.title,
dashboardId: navigationItem.dashboardId,
},
};
}
return definition;
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 { SavedObjectsClientContract } from '@kbn/core/server';
import { OBSERVABILITY_METRIC_DEFINITIONS } from '../../../../common/saved_object_contants';

import { MetricDefinitionsResponse } from '../../../../common/types';

import { MetricDefinitionSavedObject } from '../../../saved_objects/metric_definition';

export async function getEntityAssociations({
namespace,

soClient,
}: {
namespace: string;
soClient: SavedObjectsClientContract;
}): Promise<Map<string, MetricDefinitionsResponse[]>> {
const metricDefinitionSavedObject = await soClient.get<MetricDefinitionSavedObject>(
OBSERVABILITY_METRIC_DEFINITIONS,
namespace
);

if (!metricDefinitionSavedObject) {
throw new Error(`Entity definition for type "${namespace}" not found`);
}

const entityAssociationsMap = metricDefinitionSavedObject.attributes.groups.reduce(
(acc, definition) => {
for (const association of definition.entity_associations ?? []) {
if (!association) {
continue;
}

const metricDefinition: MetricDefinitionsResponse = {
id: definition.id,
instrument: definition.instrument,
metricName: definition.metric_name,
unit: definition.unit,
type: definition.type,
};

const existing = acc.get(association);
if (existing) {
existing.push(metricDefinition);
} else {
acc.set(association, [metricDefinition]);
}
}
return acc;
},
new Map<string, MetricDefinitionsResponse[]>()
);

return entityAssociationsMap;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 { SavedObjectsClientContract } from '@kbn/core/server';
import { OBSERVABILITY_ENTITY_DEFINITIONS } from '../../../../common/saved_object_contants';
import { EntityDefinitionsResponse } from '../../../../common/types';
import { EntityDefinitionSavedObject } from '../../../saved_objects/entity_definition';
import { getEntityAssociations } from './get_entity_associations';

export async function getEntityDefinitions({
namespace,
soClient,
}: {
namespace: string;
soClient: SavedObjectsClientContract;
}): Promise<EntityDefinitionsResponse[]> {
const entityDefinitionSavedObject = await soClient.get<EntityDefinitionSavedObject>(
OBSERVABILITY_ENTITY_DEFINITIONS,
namespace
);

if (!entityDefinitionSavedObject) {
throw new Error(`Entity definition for namespace "${namespace}" not found`);
}

const entitiesRelationshipsMap = new Map<string, string[]>();
for (const definition of entityDefinitionSavedObject.attributes.groups) {
for (const relationship of definition.relationships ?? []) {
const existingRelationships = entitiesRelationshipsMap.get(relationship.target) || [];
existingRelationships.push(definition.id);
entitiesRelationshipsMap.set(relationship.target, existingRelationships);
}
}

const entityAssociationsMap = await getEntityAssociations({
namespace,
soClient,
});

return entityDefinitionSavedObject.attributes.groups.map((entityDefinition) => {
return {
id: entityDefinition.id,
name: entityDefinition.name,
attributes:
entityDefinition.attributes
?.filter((attributes) => attributes.requirement_level !== 'opt_in')
.map((attribute) => attribute.ref) ?? [],
metrics: entityAssociationsMap.get(entityDefinition.name) ?? [],
relationships: entitiesRelationshipsMap.get(entityDefinition.id) ?? [],
};
});
}
Loading
Loading