Skip to content

Commit

Permalink
[Infra] apm_data_access service to return host names (elastic#189894)
Browse files Browse the repository at this point in the history
closes [elastic#188752](elastic#188752)

## Summary

This PR creates a service that will return host names collected by APM. 

**NOTE**: At first, we'll use this service to join the host names from
APM with metrics collected by the system module to build the hosts with
metrics lists - not necessarily to be used in the total hosts count
functionality. This service may be adjusted as we start using it in
Infra.

---------

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
crespocarlos and elasticmachine committed Aug 7, 2024
1 parent beef9ba commit b265604
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 { estypes } from '@elastic/elasticsearch';
import { rangeQuery } from '@kbn/observability-plugin/server';
import { HOST_NAME } from '@kbn/apm-types/es_fields';
import { getBucketSize, type TimeRangeMetadata } from '../../../common';
import { getPreferredBucketSizeAndDataSource } from '../../../common/utils/get_preferred_bucket_size_and_data_source';
import { ApmDocumentType } from '../../../common/document_type';
import type { ApmDataAccessServicesParams } from '../get_services';

const MAX_SIZE = 1000;

export interface HostNamesRequest {
query: estypes.QueryDslQueryContainer;
kuery?: string;
start: number;
end: number;
size?: number;
documentSources: TimeRangeMetadata['sources'];
}

const suitableTypes = [ApmDocumentType.TransactionMetric];

export function createGetHostNames({ apmEventClient }: ApmDataAccessServicesParams) {
return async ({ start, end, size = MAX_SIZE, query, documentSources }: HostNamesRequest) => {
const sourcesToUse = getPreferredBucketSizeAndDataSource({
sources: documentSources.filter((s) => suitableTypes.includes(s.documentType)),
bucketSizeInSeconds: getBucketSize({ start, end, numBuckets: 50 }).bucketSize,
});

const esResponse = await apmEventClient.search('get_apm_host_names', {
apm: {
sources: [
{
documentType: sourcesToUse.source.documentType,
rollupInterval: sourcesToUse.source.rollupInterval,
},
],
},
body: {
track_total_hits: false,
size: 0,
query: {
bool: {
filter: [query, ...rangeQuery(start, end)],
},
},
aggs: {
hostNames: {
terms: {
field: HOST_NAME,
size: Math.min(size, MAX_SIZE),
},
},
},
},
});

return esResponse.aggregations?.hostNames.buckets.map((bucket) => bucket.key as string) ?? [];
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { APMEventClient } from '../lib/helpers/create_es_client/create_apm_event_client';
import { createGetDocumentSources } from './get_document_sources';
import { createGetHostNames } from './get_host_names';

export interface ApmDataAccessServicesParams {
apmEventClient: APMEventClient;
Expand All @@ -15,5 +16,6 @@ export interface ApmDataAccessServicesParams {
export function getServices(params: ApmDataAccessServicesParams) {
return {
getDocumentSources: createGetDocumentSources(params),
getHostNames: createGetHostNames(params),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface ApmDataAccessPluginStart {}
export type ApmDataAccessServices = ReturnType<typeof getServices>;
export type { ApmDataAccessServicesParams } from './services/get_services';
export type { DocumentSourcesRequest } from './services/get_document_sources';
export type { HostNamesRequest } from './services/get_host_names';
export type {
APMEventClientConfig,
APMEventESSearchRequest,
Expand Down

0 comments on commit b265604

Please sign in to comment.