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] Use rounded bucket sizes for transaction distribution #42830

Merged
merged 2 commits into from Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/apm/index.ts
Expand Up @@ -68,7 +68,7 @@ export const apm: LegacyPluginInitializer = kibana => {

// buckets
minimumBucketSize: Joi.number().default(15),
bucketTargetCount: Joi.number().default(27)
bucketTargetCount: Joi.number().default(15)
}).default();
},

Expand Down
@@ -0,0 +1,52 @@
/*
* 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 { roundToNearestFiveOrTen } from './round_to_nearest_five_or_ten';

describe('roundToNearestFiveOrTen', () => {
[
{
input: 11,
output: 10
},
{
input: 45,
output: 50
},
{
input: 55,
output: 50
},
{
input: 400,
output: 500
},
{
input: 1001,
output: 1000
},
{
input: 2000,
output: 1000
},
{
input: 4000,
output: 5000
},
{
input: 20000,
output: 10000
},
{
input: 80000,
output: 100000
}
].forEach(({ input, output }) => {
it(`should convert ${input} to ${output}`, () => {
expect(roundToNearestFiveOrTen(input)).toBe(output);
});
});
});
@@ -0,0 +1,17 @@
/*
* 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.
*/

/*
* Examples:
* roundToNearestFiveOrTen(55) -> 50
* roundToNearestFiveOrTen(95) -> 100
* roundToNearestFiveOrTen(384) -> 500
*/
export function roundToNearestFiveOrTen(value: number) {
const five = Math.pow(10, Math.floor(Math.log10(value))) * 5;
const ten = Math.pow(10, Math.round(Math.log10(value)));
return Math.abs(five - value) < Math.abs(ten - value) ? five : ten;
}
Expand Up @@ -23,11 +23,11 @@ export function bucketFetcher(
transactionType: string,
transactionId: string,
traceId: string,
distributionMax: number,
bucketSize: number,
setup: Setup
) {
const { start, end, uiFiltersES, client, config } = setup;
const bucketTargetCount = config.get<number>('xpack.apm.bucketTargetCount');

const params = {
index: config.get<string>('apm_oss.transactionIndices'),
Expand Down Expand Up @@ -58,7 +58,7 @@ export function bucketFetcher(
min_doc_count: 0,
extended_bounds: {
min: 0,
max: bucketSize * bucketTargetCount
max: distributionMax
Copy link
Member Author

Choose a reason for hiding this comment

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

The previous max value was calculated from the rounded bucketSize so it was not accurate and would therefore miss buckets. Using the exact distributionMax from the stats request fixes this.

}
},
aggs: {
Expand Down
Expand Up @@ -14,6 +14,7 @@ export async function getBuckets(
transactionType: string,
transactionId: string,
traceId: string,
distributionMax: number,
bucketSize: number,
setup: Setup
) {
Expand All @@ -23,6 +24,7 @@ export async function getBuckets(
transactionType,
transactionId,
traceId,
distributionMax,
bucketSize,
setup
);
Expand Down
Expand Up @@ -13,7 +13,7 @@ import {
} from '../../../../common/elasticsearch_fieldnames';
import { Setup } from '../../helpers/setup_request';

export async function calculateBucketSize(
export async function getDistributionMax(
serviceName: string,
transactionName: string,
transactionType: string,
Expand Down Expand Up @@ -56,10 +56,5 @@ export async function calculateBucketSize(
};

const resp = await client.search(params);

const minBucketSize: number = config.get('xpack.apm.minimumBucketSize');
const bucketTargetCount: number = config.get('xpack.apm.bucketTargetCount');
const max = resp.aggregations.stats.max;
const bucketSize = Math.floor(max / bucketTargetCount);
return bucketSize > minBucketSize ? bucketSize : minBucketSize;
return resp.aggregations.stats.max;
}
Expand Up @@ -6,8 +6,20 @@

import { PromiseReturnType } from '../../../../typings/common';
import { Setup } from '../../helpers/setup_request';
import { calculateBucketSize } from './calculate_bucket_size';
import { getBuckets } from './get_buckets';
import { getDistributionMax } from './get_distribution_max';
import { roundToNearestFiveOrTen } from '../../helpers/round_to_nearest_five_or_ten';

function getBucketSize(max: number, { config }: Setup) {
const minBucketSize: number = config.get<number>(
'xpack.apm.minimumBucketSize'
);
const bucketTargetCount = config.get<number>('xpack.apm.bucketTargetCount');
const bucketSize = max / bucketTargetCount;
return roundToNearestFiveOrTen(
bucketSize > minBucketSize ? bucketSize : minBucketSize
);
}

export type TransactionDistributionAPIResponse = PromiseReturnType<
typeof getTransactionDistribution
Expand All @@ -27,19 +39,25 @@ export async function getTransactionDistribution({
traceId: string;
setup: Setup;
}) {
const bucketSize = await calculateBucketSize(
const distributionMax = await getDistributionMax(
serviceName,
transactionName,
transactionType,
setup
);

if (distributionMax == null) {
return { totalHits: 0, buckets: [], bucketSize: 0 };
}

const bucketSize = getBucketSize(distributionMax, setup);
const { buckets, totalHits } = await getBuckets(
serviceName,
transactionName,
transactionType,
transactionId,
traceId,
distributionMax,
bucketSize,
setup
);
Expand Down
16 changes: 8 additions & 8 deletions x-pack/legacy/plugins/apm/typings/elasticsearch.ts
Expand Up @@ -93,16 +93,16 @@ declare module 'elasticsearch' {
};
extended_stats: {
count: number;
min: number;
max: number;
avg: number;
min: number | null;
max: number | null;
avg: number | null;
sum: number;
sum_of_squares: number;
variance: number;
std_deviation: number;
sum_of_squares: number | null;
variance: number | null;
std_deviation: number | null;
std_deviation_bounds: {
upper: number;
lower: number;
upper: number | null;
lower: number | null;
};
};
}[AggregationType & keyof AggregationOption[AggregationName]];
Expand Down