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 1 commit
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
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/apm/index.ts
Original file line number Diff line number Diff line change
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
52 changes: 52 additions & 0 deletions x-pack/legacy/plugins/apm/server/lib/helpers/round_nice.test.ts
Original file line number Diff line number Diff line change
@@ -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 { roundNice } from './round_nice';

describe('roundNice', () => {
[
{
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(roundNice(input)).toBe(output);
});
});
});
11 changes: 11 additions & 0 deletions x-pack/legacy/plugins/apm/server/lib/helpers/round_nice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.
*/

export function roundNice(v: number) {
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this file could use a more descriptive name (maybe roundToNearestFiveOrTen?) and v could be something like value. Maybe a comment w/ some examples as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I did add a test which I feel is fairly descriptive but adding examples as comments directly next to the implementation will be an improvement.

const five = Math.pow(10, Math.floor(Math.log10(v))) * 5;
const ten = Math.pow(10, Math.round(Math.log10(v)));
return Math.abs(five - v) < Math.abs(ten - v) ? five : ten;
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@

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 { roundNice } from '../../helpers/round_nice';

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 roundNice(bucketSize > minBucketSize ? bucketSize : minBucketSize);
}

export type TransactionDistributionAPIResponse = PromiseReturnType<
typeof getTransactionDistribution
Expand All @@ -27,19 +37,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
Original file line number Diff line number Diff line change
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