-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[APM] Include services with only metric documents #92378
Merged
dgieselaar
merged 6 commits into
elastic:master
from
dgieselaar:include-metrics-only-services
Feb 27, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3bf2864
[APM] Include services with only metric documents
dgieselaar 4491846
Merge branch 'master' of github.com:elastic/kibana into include-metri…
dgieselaar e643725
Explain as_mutable_array
dgieselaar b05a27f
Merge branch 'master' of github.com:elastic/kibana into include-metri…
dgieselaar 1e2bebf
Merge branch 'master' of github.com:elastic/kibana into include-metri…
dgieselaar 22baa96
Use kuery instead of uiFilters for API tests
dgieselaar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// Sometimes we use `as const` to have a more specific type, | ||
// because TypeScript by default will widen the value type of an | ||
// array literal. Consider the following example: | ||
// | ||
// const filter = [ | ||
// { term: { 'agent.name': 'nodejs' } }, | ||
// { range: { '@timestamp': { gte: 'now-15m ' }} | ||
// ]; | ||
|
||
// The result value type will be: | ||
|
||
// const filter: ({ | ||
// term: { | ||
// 'agent.name'?: string | ||
// }; | ||
// range?: undefined | ||
// } | { | ||
// term?: undefined; | ||
// range: { | ||
// '@timestamp': { | ||
// gte: string | ||
// } | ||
// } | ||
// })[]; | ||
|
||
// This can sometimes leads to issues. In those cases, we can | ||
// use `as const`. However, the Readonly<any> type is not compatible | ||
// with Array<any>. This function returns a mutable version of a type. | ||
|
||
export function asMutableArray<T extends Readonly<any>>( | ||
arr: T | ||
): T extends Readonly<[...infer U]> ? U : unknown[] { | ||
return arr as any; | ||
} |
51 changes: 50 additions & 1 deletion
51
x-pack/plugins/apm/server/lib/services/__snapshots__/queries.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
x-pack/plugins/apm/server/lib/services/get_services/get_services_from_metric_documents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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 { AgentName } from '../../../../typings/es_schemas/ui/fields/agent'; | ||
import { | ||
AGENT_NAME, | ||
SERVICE_ENVIRONMENT, | ||
SERVICE_NAME, | ||
} from '../../../../common/elasticsearch_fieldnames'; | ||
import { environmentQuery, kqlQuery, rangeQuery } from '../../../utils/queries'; | ||
import { ProcessorEvent } from '../../../../common/processor_event'; | ||
import { Setup, SetupTimeRange } from '../../helpers/setup_request'; | ||
import { withApmSpan } from '../../../utils/with_apm_span'; | ||
|
||
export function getServicesFromMetricDocuments({ | ||
environment, | ||
setup, | ||
maxNumServices, | ||
kuery, | ||
}: { | ||
setup: Setup & SetupTimeRange; | ||
environment?: string; | ||
maxNumServices: number; | ||
kuery?: string; | ||
}) { | ||
return withApmSpan('get_services_from_metric_documents', async () => { | ||
const { apmEventClient, start, end } = setup; | ||
|
||
const response = await apmEventClient.search({ | ||
apm: { | ||
events: [ProcessorEvent.metric], | ||
}, | ||
body: { | ||
size: 0, | ||
query: { | ||
bool: { | ||
filter: [ | ||
...rangeQuery(start, end), | ||
...environmentQuery(environment), | ||
...kqlQuery(kuery), | ||
], | ||
}, | ||
}, | ||
aggs: { | ||
services: { | ||
terms: { | ||
field: SERVICE_NAME, | ||
size: maxNumServices, | ||
}, | ||
aggs: { | ||
environments: { | ||
terms: { | ||
field: SERVICE_ENVIRONMENT, | ||
}, | ||
}, | ||
latest: { | ||
top_metrics: { | ||
metrics: { field: AGENT_NAME } as const, | ||
sort: { '@timestamp': 'desc' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return ( | ||
response.aggregations?.services.buckets.map((bucket) => { | ||
return { | ||
serviceName: bucket.key as string, | ||
environments: bucket.environments.buckets.map( | ||
(envBucket) => envBucket.key as string | ||
), | ||
agentName: bucket.latest.top[0].metrics[AGENT_NAME] as AgentName, | ||
}; | ||
}) ?? [] | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,18 @@ | |
*/ | ||
|
||
import { Logger } from '@kbn/logging'; | ||
import { asMutableArray } from '../../../../common/utils/as_mutable_array'; | ||
import { joinByKey } from '../../../../common/utils/join_by_key'; | ||
import { getServicesProjection } from '../../../projections/services'; | ||
import { withApmSpan } from '../../../utils/with_apm_span'; | ||
import { Setup, SetupTimeRange } from '../../helpers/setup_request'; | ||
import { getHealthStatuses } from './get_health_statuses'; | ||
import { getServicesFromMetricDocuments } from './get_services_from_metric_documents'; | ||
import { getServiceTransactionStats } from './get_service_transaction_stats'; | ||
|
||
export type ServicesItemsSetup = Setup & SetupTimeRange; | ||
|
||
const MAX_NUMBER_OF_SERVICES = 500; | ||
|
||
export async function getServicesItems({ | ||
environment, | ||
kuery, | ||
|
@@ -32,33 +35,49 @@ export async function getServicesItems({ | |
const params = { | ||
environment, | ||
kuery, | ||
projection: getServicesProjection({ | ||
kuery, | ||
setup, | ||
searchAggregatedTransactions, | ||
}), | ||
setup, | ||
searchAggregatedTransactions, | ||
maxNumServices: MAX_NUMBER_OF_SERVICES, | ||
}; | ||
|
||
const [transactionStats, healthStatuses] = await Promise.all([ | ||
const [ | ||
transactionStats, | ||
servicesFromMetricDocuments, | ||
healthStatuses, | ||
] = await Promise.all([ | ||
getServiceTransactionStats(params), | ||
getServicesFromMetricDocuments(params), | ||
getHealthStatuses(params).catch((err) => { | ||
logger.error(err); | ||
return []; | ||
}), | ||
Comment on lines
50
to
53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: shouldn't the error handling happen in |
||
]); | ||
|
||
const apmServices = transactionStats.map(({ serviceName }) => serviceName); | ||
const foundServiceNames = transactionStats.map( | ||
({ serviceName }) => serviceName | ||
); | ||
|
||
const servicesWithOnlyMetricDocuments = servicesFromMetricDocuments.filter( | ||
({ serviceName }) => !foundServiceNames.includes(serviceName) | ||
); | ||
|
||
const allServiceNames = foundServiceNames.concat( | ||
servicesWithOnlyMetricDocuments.map(({ serviceName }) => serviceName) | ||
); | ||
|
||
// make sure to exclude health statuses from services | ||
// that are not found in APM data | ||
const matchedHealthStatuses = healthStatuses.filter(({ serviceName }) => | ||
apmServices.includes(serviceName) | ||
allServiceNames.includes(serviceName) | ||
); | ||
|
||
const allMetrics = [...transactionStats, ...matchedHealthStatuses]; | ||
|
||
return joinByKey(allMetrics, 'serviceName'); | ||
return joinByKey( | ||
asMutableArray([ | ||
...transactionStats, | ||
...servicesWithOnlyMetricDocuments, | ||
...matchedHealthStatuses, | ||
] as const), | ||
'serviceName' | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment to describe what this helper does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure thing 👍