From 0064ac5daba516fafa5bd198491bbcf9884c4593 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Fri, 2 Aug 2019 11:02:38 -0700 Subject: [PATCH] migrating to new http api --- .../infra/common/http_api/metadata_api.ts | 4 +- .../infra/server/routes/metadata/index.ts | 14 ++--- .../routes/metadata/lib/has_apm_data.ts | 53 +++++++++++++++++++ 3 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 x-pack/legacy/plugins/infra/server/routes/metadata/lib/has_apm_data.ts diff --git a/x-pack/legacy/plugins/infra/common/http_api/metadata_api.ts b/x-pack/legacy/plugins/infra/common/http_api/metadata_api.ts index 796960651122f..5b9389a073002 100644 --- a/x-pack/legacy/plugins/infra/common/http_api/metadata_api.ts +++ b/x-pack/legacy/plugins/infra/common/http_api/metadata_api.ts @@ -97,4 +97,6 @@ export type InfraMetadataMachine = rt.TypeOf; export type InfraMetadataHost = rt.TypeOf; -export type InfraMEtadataOS = rt.TypeOf; +export type InfraMetadataOS = rt.TypeOf; + +export type InfraMetadataNodeType = rt.TypeOf; diff --git a/x-pack/legacy/plugins/infra/server/routes/metadata/index.ts b/x-pack/legacy/plugins/infra/server/routes/metadata/index.ts index aaffe4390014e..14ab0513c4a64 100644 --- a/x-pack/legacy/plugins/infra/server/routes/metadata/index.ts +++ b/x-pack/legacy/plugins/infra/server/routes/metadata/index.ts @@ -16,7 +16,7 @@ import { import { InfraBackendLibs } from '../../lib/infra_types'; import { getMetricMetadata } from './lib/get_metric_metadata'; import { pickFeatureName } from './lib/pick_feature_name'; -// import { hasAPMData } from './lib/has_apm_data'; +import { hasAPMData } from './lib/has_apm_data'; import { getCloudMetricsMetadata } from './lib/get_cloud_metric_metadata'; import { getNodeInfo } from './lib/get_node_info'; import { throwErrors } from '../../../common/runtime_types'; @@ -55,21 +55,15 @@ export const initMetadataRoute = (libs: InfraBackendLibs) => { nameToFeature('metrics') ); - // const hasAPMData = await hasAPMData( - // framework, - // req, - // configuration, - // nodeId, - // nodeType - // ); - // const apmMetricData = hasAPMData ? [{ name: 'apm.transaction', source: 'apm' }] : []; + const hasAPM = await hasAPMData(framework, req, configuration, nodeId, nodeType); + const apmMetricFeatures = hasAPM ? [{ name: 'apm.transaction', source: 'apm' }] : []; const id = metricsMetadata.id; const name = metricsMetadata.name || id; return InfraMetadataRT.decode({ id, name, - features: [...metricFeatures, ...cloudMetricsFeatures], + features: [...metricFeatures, ...cloudMetricsFeatures, ...apmMetricFeatures], info, }).getOrElseL(throwErrors(Boom.badImplementation)); } catch (error) { diff --git a/x-pack/legacy/plugins/infra/server/routes/metadata/lib/has_apm_data.ts b/x-pack/legacy/plugins/infra/server/routes/metadata/lib/has_apm_data.ts new file mode 100644 index 0000000000000..3193cf83978b0 --- /dev/null +++ b/x-pack/legacy/plugins/infra/server/routes/metadata/lib/has_apm_data.ts @@ -0,0 +1,53 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { + InfraFrameworkRequest, + InfraBackendFrameworkAdapter, +} from '../../../lib/adapters/framework'; +import { InfraSourceConfiguration } from '../../../lib/sources'; +import { getIdFieldName } from './get_id_field_name'; + +export const hasAPMData = async ( + framework: InfraBackendFrameworkAdapter, + req: InfraFrameworkRequest, + sourceConfiguration: InfraSourceConfiguration, + nodeId: string, + nodeType: 'host' | 'pod' | 'container' +) => { + const config = framework.config(req); + const apmIndex = config.get('apm_oss.transactionIndices') || 'apm-*'; + // There is a bug in APM ECS data where host.name is not set. + // This will fixed with: https://github.com/elastic/apm-server/issues/2502 + const nodeFieldName = + nodeType === 'host' ? 'host.hostname' : getIdFieldName(sourceConfiguration, nodeType); + const params = { + allowNoIndices: true, + ignoreUnavailable: true, + terminateAfter: 1, + index: apmIndex, + body: { + size: 0, + query: { + bool: { + filter: [ + { + match: { [nodeFieldName]: nodeId }, + }, + { + exists: { field: 'service.name' }, + }, + { + exists: { field: 'transaction.type' }, + }, + ], + }, + }, + }, + }; + const response = await framework.callWithRequest<{}, {}>(req, 'search', params); + return response.hits.total.value !== 0; +};