-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize getServiceTransactionGroups
- Loading branch information
1 parent
df606c5
commit 7d05875
Showing
7 changed files
with
462 additions
and
304 deletions.
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
134 changes: 134 additions & 0 deletions
134
...lib/services/get_service_transaction_groups/get_timeseries_data_for_transaction_groups.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,134 @@ | ||
/* | ||
* 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 { PromiseReturnType } from '../../../../../observability/typings/common'; | ||
import { EventOutcome } from '../../../../common/event_outcome'; | ||
import { rangeFilter } from '../../../../common/utils/range_filter'; | ||
import { | ||
EVENT_OUTCOME, | ||
SERVICE_NAME, | ||
TRANSACTION_NAME, | ||
TRANSACTION_TYPE, | ||
} from '../../../../common/elasticsearch_fieldnames'; | ||
|
||
import { ESFilter } from '../../../../../../typings/elasticsearch'; | ||
import { | ||
getProcessorEventForAggregatedTransactions, | ||
getTransactionDurationFieldForAggregatedTransactions, | ||
} from '../../helpers/aggregated_transactions'; | ||
import { APMEventClient } from '../../helpers/create_es_client/create_apm_event_client'; | ||
import { getBucketSize } from '../../helpers/get_bucket_size'; | ||
|
||
export type TransactionGroupTimeseriesData = PromiseReturnType< | ||
typeof getTimeseriesDataForTransactionGroups | ||
>; | ||
|
||
export async function getTimeseriesDataForTransactionGroups({ | ||
apmEventClient, | ||
start, | ||
end, | ||
serviceName, | ||
transactionNames, | ||
esFilter, | ||
searchAggregatedTransactions, | ||
size, | ||
numBuckets, | ||
}: { | ||
apmEventClient: APMEventClient; | ||
start: number; | ||
end: number; | ||
serviceName: string; | ||
transactionNames: string[]; | ||
esFilter: ESFilter[]; | ||
searchAggregatedTransactions: boolean; | ||
size: number; | ||
numBuckets: number; | ||
}) { | ||
const { intervalString } = getBucketSize(start, end, numBuckets); | ||
|
||
const timeseriesResponse = await apmEventClient.search({ | ||
apm: { | ||
events: [ | ||
getProcessorEventForAggregatedTransactions( | ||
searchAggregatedTransactions | ||
), | ||
], | ||
}, | ||
body: { | ||
size: 0, | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ terms: { [TRANSACTION_NAME]: transactionNames } }, | ||
{ term: { [SERVICE_NAME]: serviceName } }, | ||
{ range: rangeFilter(start, end) }, | ||
...esFilter, | ||
], | ||
}, | ||
}, | ||
aggs: { | ||
transaction_groups: { | ||
terms: { | ||
field: TRANSACTION_NAME, | ||
size, | ||
}, | ||
aggs: { | ||
transaction_types: { | ||
terms: { | ||
field: TRANSACTION_TYPE, | ||
}, | ||
}, | ||
timeseries: { | ||
date_histogram: { | ||
field: '@timestamp', | ||
fixed_interval: intervalString, | ||
min_doc_count: 0, | ||
extended_bounds: { | ||
min: start, | ||
max: end, | ||
}, | ||
}, | ||
aggs: { | ||
avg_latency: { | ||
avg: { | ||
field: getTransactionDurationFieldForAggregatedTransactions( | ||
searchAggregatedTransactions | ||
), | ||
}, | ||
}, | ||
transaction_count: { | ||
value_count: { | ||
field: getTransactionDurationFieldForAggregatedTransactions( | ||
searchAggregatedTransactions | ||
), | ||
}, | ||
}, | ||
[EVENT_OUTCOME]: { | ||
filter: { | ||
term: { | ||
[EVENT_OUTCOME]: EventOutcome.failure, | ||
}, | ||
}, | ||
aggs: { | ||
transaction_count: { | ||
value_count: { | ||
field: getTransactionDurationFieldForAggregatedTransactions( | ||
searchAggregatedTransactions | ||
), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return timeseriesResponse.aggregations?.transaction_groups.buckets ?? []; | ||
} |
163 changes: 163 additions & 0 deletions
163
...apm/server/lib/services/get_service_transaction_groups/get_transaction_groups_for_page.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,163 @@ | ||
/* | ||
* 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 { orderBy } from 'lodash'; | ||
import { ValuesType } from 'utility-types'; | ||
import { PromiseReturnType } from '../../../../../observability/typings/common'; | ||
import { EventOutcome } from '../../../../common/event_outcome'; | ||
import { ESFilter } from '../../../../../../typings/elasticsearch'; | ||
import { rangeFilter } from '../../../../common/utils/range_filter'; | ||
import { | ||
EVENT_OUTCOME, | ||
SERVICE_NAME, | ||
TRANSACTION_NAME, | ||
} from '../../../../common/elasticsearch_fieldnames'; | ||
import { | ||
getProcessorEventForAggregatedTransactions, | ||
getTransactionDurationFieldForAggregatedTransactions, | ||
} from '../../helpers/aggregated_transactions'; | ||
import { APMEventClient } from '../../helpers/create_es_client/create_apm_event_client'; | ||
import { ServiceOverviewTransactionGroupSortField } from '.'; | ||
|
||
export type TransactionGroupWithoutTimeseriesData = ValuesType< | ||
PromiseReturnType<typeof getTransactionGroupsForPage>['transactionGroups'] | ||
>; | ||
|
||
export async function getTransactionGroupsForPage({ | ||
apmEventClient, | ||
searchAggregatedTransactions, | ||
serviceName, | ||
start, | ||
end, | ||
esFilter, | ||
sortField, | ||
sortDirection, | ||
pageIndex, | ||
size, | ||
}: { | ||
apmEventClient: APMEventClient; | ||
searchAggregatedTransactions: boolean; | ||
serviceName: string; | ||
start: number; | ||
end: number; | ||
esFilter: ESFilter[]; | ||
sortField: ServiceOverviewTransactionGroupSortField; | ||
sortDirection: 'asc' | 'desc'; | ||
pageIndex: number; | ||
size: number; | ||
}) { | ||
const response = await apmEventClient.search({ | ||
apm: { | ||
events: [ | ||
getProcessorEventForAggregatedTransactions( | ||
searchAggregatedTransactions | ||
), | ||
], | ||
}, | ||
body: { | ||
size: 0, | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ term: { [SERVICE_NAME]: serviceName } }, | ||
{ range: rangeFilter(start, end) }, | ||
...esFilter, | ||
], | ||
}, | ||
}, | ||
aggs: { | ||
transaction_groups: { | ||
terms: { | ||
field: TRANSACTION_NAME, | ||
size: 500, | ||
order: { | ||
_count: 'desc', | ||
}, | ||
}, | ||
aggs: { | ||
avg_latency: { | ||
avg: { | ||
field: getTransactionDurationFieldForAggregatedTransactions( | ||
searchAggregatedTransactions | ||
), | ||
}, | ||
}, | ||
transaction_count: { | ||
value_count: { | ||
field: getTransactionDurationFieldForAggregatedTransactions( | ||
searchAggregatedTransactions | ||
), | ||
}, | ||
}, | ||
[EVENT_OUTCOME]: { | ||
filter: { | ||
term: { | ||
[EVENT_OUTCOME]: EventOutcome.failure, | ||
}, | ||
}, | ||
aggs: { | ||
transaction_count: { | ||
value_count: { | ||
field: getTransactionDurationFieldForAggregatedTransactions( | ||
searchAggregatedTransactions | ||
), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const transactionGroups = | ||
response.aggregations?.transaction_groups.buckets.map((bucket) => { | ||
const errorRate = | ||
bucket.transaction_count.value > 0 | ||
? (bucket[EVENT_OUTCOME].transaction_count.value ?? 0) / | ||
bucket.transaction_count.value | ||
: null; | ||
|
||
return { | ||
name: bucket.key as string, | ||
latency: bucket.avg_latency.value, | ||
traffic: bucket.transaction_count.value, | ||
errorRate, | ||
}; | ||
}) ?? []; | ||
|
||
const totalDurationValues = transactionGroups.map( | ||
(group) => (group.latency ?? 0) * group.traffic | ||
); | ||
|
||
const minTotalDuration = Math.min(...totalDurationValues); | ||
const maxTotalDuration = Math.max(...totalDurationValues); | ||
|
||
const transactionGroupsWithImpact = transactionGroups.map((group) => ({ | ||
...group, | ||
impact: | ||
(((group.latency ?? 0) * group.traffic - minTotalDuration) / | ||
(maxTotalDuration - minTotalDuration)) * | ||
100, | ||
})); | ||
|
||
// Sort transaction groups first, and only get timeseries for data in view. | ||
// This is to limit the possibility of creating too many buckets. | ||
|
||
const sortedAndSlicedTransactionGroups = orderBy( | ||
transactionGroupsWithImpact, | ||
sortField, | ||
[sortDirection] | ||
).slice(pageIndex * size, pageIndex * size + size); | ||
|
||
return { | ||
transactionGroups: sortedAndSlicedTransactionGroups, | ||
totalTransactionGroups: transactionGroups.length, | ||
isAggregationAccurate: | ||
(response.aggregations?.transaction_groups.sum_other_doc_count ?? 0) === | ||
0, | ||
}; | ||
} |
Oops, something went wrong.