diff --git a/x-pack/plugins/asset_manager/kibana.jsonc b/x-pack/plugins/asset_manager/kibana.jsonc index ca019d0cceaa8..a3bfb9261456f 100644 --- a/x-pack/plugins/asset_manager/kibana.jsonc +++ b/x-pack/plugins/asset_manager/kibana.jsonc @@ -12,6 +12,7 @@ "optionalPlugins": [ ], "requiredPlugins": [ + "apmDataAccess" ], "browser": false, "server": true, diff --git a/x-pack/plugins/asset_manager/server/lib/accessors/index.ts b/x-pack/plugins/asset_manager/server/lib/accessors/index.ts index a097703fb3b51..d75490b002e97 100644 --- a/x-pack/plugins/asset_manager/server/lib/accessors/index.ts +++ b/x-pack/plugins/asset_manager/server/lib/accessors/index.ts @@ -6,14 +6,18 @@ */ import { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; +import { APMDataAccessConfig } from '@kbn/apm-data-access-plugin/server'; +import { SavedObjectsClientContract } from '@kbn/core/server'; import { AssetManagerConfig } from '../../types'; export interface InjectedValues { sourceIndices: AssetManagerConfig['sourceIndices']; + getApmIndices: (soClient: SavedObjectsClientContract) => Promise; } export type OptionsWithInjectedValues = T & InjectedValues; export interface AccessorOptions { esClient: ElasticsearchClient; + soClient: SavedObjectsClientContract; } diff --git a/x-pack/plugins/asset_manager/server/lib/accessors/services/get_services_by_signals.ts b/x-pack/plugins/asset_manager/server/lib/accessors/services/get_services_by_signals.ts index e368ec97e9aaf..8618feb288b7b 100644 --- a/x-pack/plugins/asset_manager/server/lib/accessors/services/get_services_by_signals.ts +++ b/x-pack/plugins/asset_manager/server/lib/accessors/services/get_services_by_signals.ts @@ -26,11 +26,12 @@ export async function getServicesBySignals( }); } + const apmIndices = await options.getApmIndices(options.soClient); const { assets } = await collectServices({ client: options.esClient, from: options.from, to: options.to, - sourceIndices: options.sourceIndices, + apmIndices, filters, }); diff --git a/x-pack/plugins/asset_manager/server/lib/asset_accessor.ts b/x-pack/plugins/asset_manager/server/lib/asset_accessor.ts index 5710ec562bac1..9f4f33097879d 100644 --- a/x-pack/plugins/asset_manager/server/lib/asset_accessor.ts +++ b/x-pack/plugins/asset_manager/server/lib/asset_accessor.ts @@ -5,6 +5,8 @@ * 2.0. */ +import { APMDataAccessConfig } from '@kbn/apm-data-access-plugin/server'; +import { SavedObjectsClientContract } from '@kbn/core/server'; import { Asset } from '../../common/types_api'; import { AssetManagerConfig } from '../types'; import { OptionsWithInjectedValues } from './accessors'; @@ -18,6 +20,7 @@ import { getServicesBySignals } from './accessors/services/get_services_by_signa interface AssetAccessorClassOptions { sourceIndices: AssetManagerConfig['sourceIndices']; source: AssetManagerConfig['lockedSource']; + getApmIndices: (soClient: SavedObjectsClientContract) => Promise; } export class AssetAccessor { @@ -27,6 +30,7 @@ export class AssetAccessor { return { ...options, sourceIndices: this.options.sourceIndices, + getApmIndices: this.options.getApmIndices, }; } diff --git a/x-pack/plugins/asset_manager/server/lib/collectors/containers.ts b/x-pack/plugins/asset_manager/server/lib/collectors/containers.ts index 18a55af232dfb..c693fc47302d7 100644 --- a/x-pack/plugins/asset_manager/server/lib/collectors/containers.ts +++ b/x-pack/plugins/asset_manager/server/lib/collectors/containers.ts @@ -16,9 +16,9 @@ export async function collectContainers({ sourceIndices, afterKey, }: CollectorOptions) { - const { metrics, logs, traces } = sourceIndices; + const { metrics, logs } = sourceIndices; const dsl: estypes.SearchRequest = { - index: [traces, logs, metrics], + index: [logs, metrics], size: QUERY_MAX_SIZE, collapse: { field: 'container.id', diff --git a/x-pack/plugins/asset_manager/server/lib/collectors/hosts.ts b/x-pack/plugins/asset_manager/server/lib/collectors/hosts.ts index 0151567a36982..f6821d73f251d 100644 --- a/x-pack/plugins/asset_manager/server/lib/collectors/hosts.ts +++ b/x-pack/plugins/asset_manager/server/lib/collectors/hosts.ts @@ -16,9 +16,9 @@ export async function collectHosts({ sourceIndices, afterKey, }: CollectorOptions) { - const { metrics, logs, traces } = sourceIndices; + const { metrics, logs } = sourceIndices; const dsl: estypes.SearchRequest = { - index: [metrics, logs, traces], + index: [metrics, logs], size: QUERY_MAX_SIZE, collapse: { field: 'host.hostname' }, sort: [{ 'host.hostname': 'asc' }], diff --git a/x-pack/plugins/asset_manager/server/lib/collectors/index.ts b/x-pack/plugins/asset_manager/server/lib/collectors/index.ts index 5e0b300e601db..3b426200657c5 100644 --- a/x-pack/plugins/asset_manager/server/lib/collectors/index.ts +++ b/x-pack/plugins/asset_manager/server/lib/collectors/index.ts @@ -6,6 +6,7 @@ */ import { estypes } from '@elastic/elasticsearch'; +import type { APMIndices } from '@kbn/apm-data-access-plugin/server'; import { ElasticsearchClient } from '@kbn/core/server'; import { AssetManagerConfig } from '../../types'; import { Asset } from '../../../common/types_api'; @@ -23,6 +24,12 @@ export interface CollectorOptions { filters?: estypes.QueryDslQueryContainer[]; } +type OmitSourceIndices = Omit; + +export type ServicesCollectorOptions = OmitSourceIndices & { + apmIndices: APMIndices; +}; + export interface CollectorResult { assets: Asset[]; afterKey?: estypes.SortResults; diff --git a/x-pack/plugins/asset_manager/server/lib/collectors/pods.ts b/x-pack/plugins/asset_manager/server/lib/collectors/pods.ts index d3d1af91326c7..104cab1d361e4 100644 --- a/x-pack/plugins/asset_manager/server/lib/collectors/pods.ts +++ b/x-pack/plugins/asset_manager/server/lib/collectors/pods.ts @@ -10,9 +10,9 @@ import { Asset } from '../../../common/types_api'; import { CollectorOptions, QUERY_MAX_SIZE } from '.'; export async function collectPods({ client, from, to, sourceIndices, afterKey }: CollectorOptions) { - const { metrics, logs, traces } = sourceIndices; + const { metrics, logs } = sourceIndices; const dsl: estypes.SearchRequest = { - index: [metrics, logs, traces], + index: [metrics, logs], size: QUERY_MAX_SIZE, collapse: { field: 'kubernetes.pod.uid', diff --git a/x-pack/plugins/asset_manager/server/lib/collectors/services.ts b/x-pack/plugins/asset_manager/server/lib/collectors/services.ts index c351f49f3a8f7..062c3a06054cd 100644 --- a/x-pack/plugins/asset_manager/server/lib/collectors/services.ts +++ b/x-pack/plugins/asset_manager/server/lib/collectors/services.ts @@ -7,17 +7,17 @@ import { estypes } from '@elastic/elasticsearch'; import { Asset } from '../../../common/types_api'; -import { CollectorOptions, QUERY_MAX_SIZE } from '.'; +import { ServicesCollectorOptions, QUERY_MAX_SIZE } from '.'; export async function collectServices({ client, from, to, - sourceIndices, + apmIndices, afterKey, filters = [], -}: CollectorOptions) { - const { traces, serviceMetrics, serviceLogs } = sourceIndices; +}: ServicesCollectorOptions) { + const { transaction, error, metric } = apmIndices; const musts: estypes.QueryDslQueryContainer[] = [ ...filters, { @@ -28,7 +28,7 @@ export async function collectServices({ ]; const dsl: estypes.SearchRequest = { - index: [traces, serviceMetrics, serviceLogs], + index: [transaction, error, metric], size: 0, _source: false, query: { diff --git a/x-pack/plugins/asset_manager/server/plugin.ts b/x-pack/plugins/asset_manager/server/plugin.ts index 56a880b0da080..41114fadbbc7b 100644 --- a/x-pack/plugins/asset_manager/server/plugin.ts +++ b/x-pack/plugins/asset_manager/server/plugin.ts @@ -20,6 +20,7 @@ import { setupRoutes } from './routes'; import { assetsIndexTemplateConfig } from './templates/assets_template'; import { AssetManagerConfig, configSchema } from './types'; import { AssetAccessor } from './lib/asset_accessor'; +import { AssetManagerPluginSetupDependencies, AssetManagerPluginStartDependencies } from './types'; export type AssetManagerServerPluginSetup = ReturnType; export type AssetManagerServerPluginStart = ReturnType; @@ -29,7 +30,13 @@ export const config: PluginConfigDescriptor = { }; export class AssetManagerServerPlugin - implements Plugin + implements + Plugin< + AssetManagerServerPluginSetup, + AssetManagerServerPluginStart, + AssetManagerPluginSetupDependencies, + AssetManagerPluginStartDependencies + > { public config: AssetManagerConfig; public logger: Logger; @@ -39,7 +46,7 @@ export class AssetManagerServerPlugin this.logger = context.logger.get(); } - public setup(core: CoreSetup) { + public setup(core: CoreSetup, plugins: AssetManagerPluginSetupDependencies) { // Check for config value and bail out if not "alpha-enabled" if (!this.config.alphaEnabled) { this.logger.info('Asset manager plugin [tech preview] is NOT enabled'); @@ -51,6 +58,7 @@ export class AssetManagerServerPlugin const assetAccessor = new AssetAccessor({ source: this.config.lockedSource, sourceIndices: this.config.sourceIndices, + getApmIndices: plugins.apmDataAccess.getApmIndices, }); const router = core.http.createRouter(); diff --git a/x-pack/plugins/asset_manager/server/routes/assets/services.ts b/x-pack/plugins/asset_manager/server/routes/assets/services.ts index 60f282a219c05..d7edf3b6f7f3c 100644 --- a/x-pack/plugins/asset_manager/server/routes/assets/services.ts +++ b/x-pack/plugins/asset_manager/server/routes/assets/services.ts @@ -48,13 +48,15 @@ export function servicesRoutes({ async (context, req, res) => { const { from = 'now-24h', to = 'now', parent } = req.query || {}; const esClient = await getEsClientFromContext(context); - + const coreContext = await context.core; + const soClient = coreContext.savedObjects.client; try { const response = await assetAccessor.getServices({ from: datemath.parse(from)!.toISOString(), to: datemath.parse(to)!.toISOString(), parent, esClient, + soClient, }); return res.ok({ body: response }); diff --git a/x-pack/plugins/asset_manager/server/types.ts b/x-pack/plugins/asset_manager/server/types.ts index a20ad1981b548..d964d3133c61e 100644 --- a/x-pack/plugins/asset_manager/server/types.ts +++ b/x-pack/plugins/asset_manager/server/types.ts @@ -7,6 +7,10 @@ import { schema, TypeOf } from '@kbn/config-schema'; import { ElasticsearchClient } from '@kbn/core/server'; +import { + ApmDataAccessPluginSetup, + ApmDataAccessPluginStart, +} from '@kbn/apm-data-access-plugin/server'; export interface ElasticsearchAccessorOptions { esClient: ElasticsearchClient; @@ -15,9 +19,6 @@ export interface ElasticsearchAccessorOptions { export const INDEX_DEFAULTS = { metrics: 'metricbeat-*,metrics-*', logs: 'filebeat-*,logs-*', - traces: 'traces-*', - serviceMetrics: 'metrics-apm*', - serviceLogs: 'logs-apm*', }; export const configSchema = schema.object({ @@ -30,9 +31,6 @@ export const configSchema = schema.object({ { metrics: schema.string({ defaultValue: INDEX_DEFAULTS.metrics }), logs: schema.string({ defaultValue: INDEX_DEFAULTS.logs }), - traces: schema.string({ defaultValue: INDEX_DEFAULTS.traces }), - serviceMetrics: schema.string({ defaultValue: INDEX_DEFAULTS.serviceMetrics }), - serviceLogs: schema.string({ defaultValue: INDEX_DEFAULTS.serviceLogs }), }, { defaultValue: INDEX_DEFAULTS } ), @@ -47,3 +45,10 @@ export const configSchema = schema.object({ }); export type AssetManagerConfig = TypeOf; + +export interface AssetManagerPluginSetupDependencies { + apmDataAccess: ApmDataAccessPluginSetup; +} +export interface AssetManagerPluginStartDependencies { + apmDataAccess: ApmDataAccessPluginStart; +} diff --git a/x-pack/plugins/asset_manager/tsconfig.json b/x-pack/plugins/asset_manager/tsconfig.json index 68bc19d31204e..9527af19396e5 100644 --- a/x-pack/plugins/asset_manager/tsconfig.json +++ b/x-pack/plugins/asset_manager/tsconfig.json @@ -19,6 +19,7 @@ "@kbn/io-ts-utils", "@kbn/core-elasticsearch-server", "@kbn/core-http-request-handler-context-server", - "@kbn/datemath" + "@kbn/datemath", + "@kbn/apm-data-access-plugin" ] }