Skip to content

Commit

Permalink
[Infra UI] Add APM to Metadata Endpoint (elastic#42197)
Browse files Browse the repository at this point in the history
* [Infra UI] Add APM to Metadata Endpoint

- Adds APM feature to metadata
- Closes elastic#42167
- Based on elastic#41836

* migrating to new http api
  • Loading branch information
simianhacker committed Aug 14, 2019
1 parent b8fa391 commit 8a6b6dd
Show file tree
Hide file tree
Showing 8 changed files with 17,892 additions and 162 deletions.
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/infra/common/http_api/metadata_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,6 @@ export type InfraMetadataMachine = rt.TypeOf<typeof InfraMetadataMachineRT>;

export type InfraMetadataHost = rt.TypeOf<typeof InfraMetadataHostRT>;

export type InfraMEtadataOS = rt.TypeOf<typeof InfraMetadataOSRT>;
export type InfraMetadataOS = rt.TypeOf<typeof InfraMetadataOSRT>;

export type InfraMetadataNodeType = rt.TypeOf<typeof InfraMetadataNodeTypeRT>;
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { GraphQLSchema } from 'graphql';
import { Lifecycle, ResponseToolkit, RouteOptions } from 'hapi';
import { Legacy } from 'kibana';

import { KibanaConfig } from 'src/legacy/server/kbn_server';
import { JsonObject } from '../../../../common/typed_json';
import { InfraMetricModel } from '../metrics/adapter_types';

Expand Down Expand Up @@ -66,6 +67,7 @@ export interface InfraBackendFrameworkAdapter {
timerange: { min: number; max: number },
filters: JsonObject[]
): Promise<InfraTSVBResponse>;
config(req: InfraFrameworkRequest): KibanaConfig;
}
/* eslint-enable @typescript-eslint/unified-signatures */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GenericParams } from 'elasticsearch';
import { GraphQLSchema } from 'graphql';
import { Legacy } from 'kibana';

import { KibanaConfig } from 'src/legacy/server/kbn_server';
import { InfraMetricModel } from '../metrics/adapter_types';
import {
InfraBackendFrameworkAdapter,
Expand Down Expand Up @@ -36,6 +37,11 @@ export class InfraKibanaBackendFrameworkAdapter implements InfraBackendFramework
this.version = server.config().get('pkg.version');
}

public config(req: InfraFrameworkRequest<Legacy.Request>): KibanaConfig {
const internalRequest = req[internalInfraFrameworkRequest];
return internalRequest.server.config();
}

public exposeStaticDir(urlPath: string, dir: string): void {
this.server.route({
handler: {
Expand Down
6 changes: 5 additions & 1 deletion x-pack/legacy/plugins/infra/server/routes/metadata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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 { getCloudMetricsMetadata } from './lib/get_cloud_metric_metadata';
import { getNodeInfo } from './lib/get_node_info';
import { throwErrors } from '../../../common/runtime_types';
Expand Down Expand Up @@ -54,12 +55,15 @@ export const initMetadataRoute = (libs: InfraBackendLibs) => {
nameToFeature('metrics')
);

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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
};
Loading

0 comments on commit 8a6b6dd

Please sign in to comment.