Skip to content
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] Adds support for metrics for latency distribution histogram #136083

Merged
merged 4 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import { scaleLog } from 'd3-scale';

import { isFiniteNumber } from '@kbn/observability-plugin/common/utils/is_finite_number';
import { CommonCorrelationsQueryParams } from '../../../../common/correlations/types';
import {
SPAN_DURATION,
TRANSACTION_DURATION,
} from '../../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../../common/processor_event';
import { Setup } from '../../../lib/helpers/setup_request';
import { getCommonCorrelationsQuery } from './get_common_correlations_query';
import { getDurationField } from '../utils';

const getHistogramRangeSteps = (min: number, max: number, steps: number) => {
// A d3 based scale function as a helper to get equally distributed bins on a log scale.
Expand All @@ -42,8 +39,7 @@ export const fetchDurationHistogramRangeSteps = async ({

const steps = 100;

const durationField =
eventType === ProcessorEvent.span ? SPAN_DURATION : TRANSACTION_DURATION;
const durationField = getDurationField(eventType);

const resp = await apmEventClient.search(
'get_duration_histogram_range_steps',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
* 2.0.
*/

import {
SPAN_DURATION,
TRANSACTION_DURATION,
} from '../../../../common/elasticsearch_fieldnames';
import { SIGNIFICANT_VALUE_DIGITS } from '../../../../common/correlations/constants';
import { Setup } from '../../../lib/helpers/setup_request';
import { ProcessorEvent } from '../../../../common/processor_event';
import { getCommonCorrelationsQuery } from './get_common_correlations_query';
import { CommonCorrelationsQueryParams } from '../../../../common/correlations/types';
import { getDurationField } from '../utils';

export const fetchDurationPercentiles = async ({
eventType,
Expand All @@ -32,36 +29,35 @@ export const fetchDurationPercentiles = async ({
totalDocs: number;
percentiles: Record<string, number>;
}> => {
const response = await setup.apmEventClient.search(
'get_duration_percentiles',
{
apm: { events: [eventType] },
body: {
track_total_hits: true,
query: getCommonCorrelationsQuery({
start,
end,
environment,
kuery,
query,
}),
size: 0,
aggs: {
duration_percentiles: {
percentiles: {
hdr: {
number_of_significant_value_digits: SIGNIFICANT_VALUE_DIGITS,
},
field:
eventType === ProcessorEvent.span
? SPAN_DURATION
: TRANSACTION_DURATION,
...(Array.isArray(percents) ? { percents } : {}),
const params = {
apm: { events: [eventType] },
body: {
track_total_hits: true,
query: getCommonCorrelationsQuery({
start,
end,
environment,
kuery,
query,
}),
size: 0,
aggs: {
duration_percentiles: {
percentiles: {
hdr: {
number_of_significant_value_digits: SIGNIFICANT_VALUE_DIGITS,
},
field: getDurationField(eventType),
...(Array.isArray(percents) ? { percents } : {}),
},
},
},
}
},
};
console.log(JSON.stringify(params));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can delete this.

const response = await setup.apmEventClient.search(
'get_duration_percentiles',
params
);

// return early with no results if the search didn't return any documents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
*/

import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import {
SPAN_DURATION,
TRANSACTION_DURATION,
} from '../../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../../common/processor_event';
import { Setup } from '../../../lib/helpers/setup_request';
import { getCommonCorrelationsQuery } from './get_common_correlations_query';
import { Environment } from '../../../../common/environment_rt';
import { getDurationField } from '../utils';

export const fetchDurationRanges = async ({
rangeSteps,
Expand Down Expand Up @@ -64,10 +61,7 @@ export const fetchDurationRanges = async ({
aggs: {
logspace_ranges: {
range: {
field:
eventType === ProcessorEvent.span
? SPAN_DURATION
: TRANSACTION_DURATION,
field: getDurationField(eventType),
ranges,
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 {
SPAN_DURATION,
TRANSACTION_DURATION,
TRANSACTION_DURATION_HISTOGRAM,
} from '../../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../../common/processor_event';

export function getDurationField(eventType: ProcessorEvent) {
if (eventType === ProcessorEvent.metric) {
return TRANSACTION_DURATION_HISTOGRAM;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think ProcessorEvent is the right abstraction here anymore. We should be more specific, e.g.:

  • transaction_events
  • span_events
  • transaction_metrics
  • service_destination_metrics

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new util function was just some code cleanup since we had repeated switching logic to for the duration field between the latency distribution chart and the correlations distribution charts. If the abstractions were to change, we would want to tease apart the correlations from the regular latency distribution chart (1st tab vs 2nd & 3rd tabs) into distinct functions for their own use cases. We could consider this tech debt and out of scope for this bug fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that eventType in this case signals what data we want to show, not what documents we should use to display it. Transaction latency data can be based on both ProcessorEvent.transaction and ProcessorEvent.metric, and similarly, exit span latency data can be based on both ProcessorEvent.span and ProcessorEvent.metric. I don't think we should conflate those concepts.

}
if (eventType === ProcessorEvent.span) {
return SPAN_DURATION;
}
return TRANSACTION_DURATION;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have used a switch here, couldn't you? Up to you to do this change though!

Suggested change
if (eventType === ProcessorEvent.metric) {
return TRANSACTION_DURATION_HISTOGRAM;
}
if (eventType === ProcessorEvent.span) {
return SPAN_DURATION;
}
return TRANSACTION_DURATION;
switch(eventType){
case ProcessorEvent.metric:
return TRANSACTION_DURATION_HISTOGRAM;
case ProcessorEvent.span:
return SPAN_DURATION;
default:
return TRANSACTION_DURATION;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

export { computeExpectationsAndRanges } from './compute_expectations_and_ranges';
export { splitAllSettledPromises } from './split_all_settled_promises';
export { getDurationField } from './get_duration_field';
12 changes: 11 additions & 1 deletion x-pack/plugins/apm/server/routes/latency_distribution/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { termQuery } from '@kbn/observability-plugin/server';
import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { getOverallLatencyDistribution } from './get_overall_latency_distribution';
import { setupRequest } from '../../lib/helpers/setup_request';
import { getSearchAggregatedTransactions } from '../../lib/helpers/transactions';
import { createApmServerRoute } from '../apm_routes/create_apm_server_route';
import { environmentRt, kueryRt, rangeRt } from '../default_api_types';
import {
Expand Down Expand Up @@ -61,9 +62,18 @@ const latencyOverallTransactionDistributionRoute = createApmServerRoute({
termFilters,
} = resources.params.body;

const searchAggregatedTransactions = await getSearchAggregatedTransactions({
...setup,
kuery,
start,
end,
});

return getOverallLatencyDistribution({
setup,
eventType: ProcessorEvent.transaction,
eventType: searchAggregatedTransactions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is enough. I think you also need a filter for { exists: { field: 'transaction.duration.histogram' } }. We have a helper function for that purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to getSearchAggregatedTransactions on line 65 does call the helper getHasAggregatedTransactions which filters for this field.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only checks for the existence. The actual search itself needs a filter on { exists: { field: 'transaction.duration.histogram' } } otherwise it will run over all metric documents.

? ProcessorEvent.metric
: ProcessorEvent.transaction,
environment,
kuery,
start,
Expand Down