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

[7.9] [APM] Optimize traces overview (#70200) #73444

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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,7 +9,7 @@ import { i18n } from '@kbn/i18n';
import React from 'react';
import styled from 'styled-components';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { ITransactionGroup } from '../../../../server/lib/transaction_groups/transform';
import { TransactionGroup } from '../../../../server/lib/transaction_groups/fetcher';
import { fontSizes, truncate } from '../../../style/variables';
import { asMillisecondDuration } from '../../../utils/formatters';
import { EmptyMessage } from '../../shared/EmptyMessage';
Expand All @@ -24,28 +24,28 @@ const StyledTransactionLink = styled(TransactionDetailLink)`
`;

interface Props {
items: ITransactionGroup[];
items: TransactionGroup[];
isLoading: boolean;
}

const traceListColumns: Array<ITableColumn<ITransactionGroup>> = [
const traceListColumns: Array<ITableColumn<TransactionGroup>> = [
{
field: 'name',
name: i18n.translate('xpack.apm.tracesTable.nameColumnLabel', {
defaultMessage: 'Name',
}),
width: '40%',
sortable: true,
render: (name: string, { sample }: ITransactionGroup) => (
<EuiToolTip id="trace-transaction-link-tooltip" content={name}>
render: (_: string, { sample }: TransactionGroup) => (
<EuiToolTip content={sample.transaction.name}>
<StyledTransactionLink
serviceName={sample.service.name}
transactionId={sample.transaction.id}
traceId={sample.trace.id}
transactionName={sample.transaction.name}
transactionType={sample.transaction.type}
>
{name}
{sample.transaction.name}
</StyledTransactionLink>
</EuiToolTip>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React, { useMemo } from 'react';
import styled from 'styled-components';
import { NOT_AVAILABLE_LABEL } from '../../../../../common/i18n';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { ITransactionGroup } from '../../../../../server/lib/transaction_groups/transform';
import { TransactionGroup } from '../../../../../server/lib/transaction_groups/fetcher';
import { fontFamilyCode, truncate } from '../../../../style/variables';
import { asDecimal, asMillisecondDuration } from '../../../../utils/formatters';
import { ImpactBar } from '../../../shared/ImpactBar';
Expand All @@ -25,12 +25,12 @@ const TransactionNameLink = styled(TransactionDetailLink)`
`;

interface Props {
items: ITransactionGroup[];
items: TransactionGroup[];
isLoading: boolean;
}

export function TransactionList({ items, isLoading }: Props) {
const columns: Array<ITableColumn<ITransactionGroup>> = useMemo(
const columns: Array<ITableColumn<TransactionGroup>> = useMemo(
() => [
{
field: 'name',
Expand All @@ -39,11 +39,11 @@ export function TransactionList({ items, isLoading }: Props) {
}),
width: '50%',
sortable: true,
render: (transactionName: string, { sample }: ITransactionGroup) => {
render: (_, { sample }: TransactionGroup) => {
return (
<EuiToolTip
id="transaction-name-link-tooltip"
content={transactionName || NOT_AVAILABLE_LABEL}
content={sample.transaction.name || NOT_AVAILABLE_LABEL}
>
<TransactionNameLink
serviceName={sample.service.name}
Expand All @@ -52,7 +52,7 @@ export function TransactionList({ items, isLoading }: Props) {
transactionName={sample.transaction.name}
transactionType={sample.transaction.type}
>
{transactionName || NOT_AVAILABLE_LABEL}
{sample.transaction.name || NOT_AVAILABLE_LABEL}
</TransactionNameLink>
</EuiToolTip>
);
Expand Down
40 changes: 1 addition & 39 deletions x-pack/plugins/apm/public/hooks/useTransactionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { useMemo } from 'react';
import { IUrlParams } from '../context/UrlParamsContext/types';
import { useUiFilters } from '../context/UrlParamsContext';
import { useFetcher } from './useFetcher';
import { APIReturnType } from '../services/rest/createCallApmApi';

const getRelativeImpact = (
impact: number,
impactMin: number,
impactMax: number
) =>
Math.max(
((impact - impactMin) / Math.max(impactMax - impactMin, 1)) * 100,
1
);

type TransactionsAPIResponse = APIReturnType<
'/api/apm/services/{serviceName}/transaction_groups'
>;

function getWithRelativeImpact(items: TransactionsAPIResponse['items']) {
const impacts = items
.map(({ impact }) => impact)
.filter((impact) => impact !== null) as number[];

const impactMin = Math.min(...impacts);
const impactMax = Math.max(...impacts);

return items.map((item) => {
return {
...item,
impactRelative:
item.impact !== null
? getRelativeImpact(item.impact, impactMin, impactMax)
: null,
};
});
}

const DEFAULT_RESPONSE: TransactionsAPIResponse = {
items: [],
isAggregationAccurate: true,
Expand Down Expand Up @@ -72,16 +42,8 @@ export function useTransactionList(urlParams: IUrlParams) {
[serviceName, start, end, transactionType, uiFilters]
);

const memoizedData = useMemo(
() => ({
items: getWithRelativeImpact(data.items),
isAggregationAccurate: data.isAggregationAccurate,
bucketSize: data.bucketSize,
}),
[data]
);
return {
data: memoizedData,
data,
status,
error,
};
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/apm/scripts/aggregate-latency-metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import pLimit from 'p-limit';
import pRetry from 'p-retry';
import { parse, format } from 'url';
import { set } from '@elastic/safer-lodash-set';
import { unique, without, merge, flatten } from 'lodash';
import { uniq, without, merge, flatten } from 'lodash';
import * as histogram from 'hdr-histogram-js';
import { ESSearchResponse } from '../../typings/elasticsearch';
import {
Expand Down Expand Up @@ -114,8 +114,8 @@ export async function aggregateLatencyMetrics() {
.filter(Boolean) as string[];

const fields = only.length
? unique(only)
: without(unique([...include, ...defaultFields]), ...exclude);
? uniq(only)
: without(uniq([...include, ...defaultFields]), ...exclude);

const globalFilter = argv.filter ? JSON.parse(String(argv.filter)) : {};

Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/apm/scripts/shared/read-kibana-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import path from 'path';
import fs from 'fs';
import yaml from 'js-yaml';
import { identity, pick } from 'lodash';
import { identity, pickBy } from 'lodash';

export type KibanaConfig = ReturnType<typeof readKibanaConfig>;

Expand All @@ -22,7 +22,7 @@ export const readKibanaConfig = () => {
)
) || {}) as {};

const cliEsCredentials = pick(
const cliEsCredentials = pickBy(
{
'elasticsearch.username': process.env.ELASTICSEARCH_USERNAME,
'elasticsearch.password': process.env.ELASTICSEARCH_PASSWORD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function registerTransactionDurationAlertType({

const { agg } = response.aggregations;

const value = 'values' in agg ? agg.values[0] : agg?.value;
const value = 'values' in agg ? Object.values(agg.values)[0] : agg?.value;

if (value && value > alertParams.threshold * 1000) {
const alertInstance = services.alertInstanceFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Unionize } from 'utility-types';
import { Unionize, Overwrite } from 'utility-types';
import { ESSearchRequest } from '../../../typings/elasticsearch';
import {
Setup,
SetupTimeRange,
Expand All @@ -17,14 +18,28 @@ import { getMetricsProjection } from '../../../common/projections/metrics';
import { mergeProjection } from '../../../common/projections/util/merge_projection';
import { AggregationOptionsByType } from '../../../typings/elasticsearch/aggregations';

interface Aggs {
[key: string]: Unionize<{
min: AggregationOptionsByType['min'];
max: AggregationOptionsByType['max'];
sum: AggregationOptionsByType['sum'];
avg: AggregationOptionsByType['avg'];
}>;
}
type MetricsAggregationMap = Unionize<{
min: AggregationOptionsByType['min'];
max: AggregationOptionsByType['max'];
sum: AggregationOptionsByType['sum'];
avg: AggregationOptionsByType['avg'];
}>;

type MetricAggs = Record<string, MetricsAggregationMap>;

export type GenericMetricsRequest = Overwrite<
ESSearchRequest,
{
body: {
aggs: {
timeseriesData: {
date_histogram: AggregationOptionsByType['date_histogram'];
aggs: MetricAggs;
};
} & MetricAggs;
};
}
>;

interface Filter {
exists?: {
Expand All @@ -35,7 +50,7 @@ interface Filter {
};
}

export async function fetchAndTransformMetrics<T extends Aggs>({
export async function fetchAndTransformMetrics<T extends MetricAggs>({
setup,
serviceName,
serviceNodeName,
Expand All @@ -58,7 +73,7 @@ export async function fetchAndTransformMetrics<T extends Aggs>({
serviceNodeName,
});

const params = mergeProjection(projection, {
const params: GenericMetricsRequest = mergeProjection(projection, {
body: {
size: 0,
query: {
Expand Down
37 changes: 4 additions & 33 deletions x-pack/plugins/apm/server/lib/metrics/transform_metrics_chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/
import theme from '@elastic/eui/dist/eui_theme_light.json';
import { Unionize, Overwrite } from 'utility-types';
import { ChartBase } from './types';
import {
ESSearchResponse,
ESSearchRequest,
} from '../../../typings/elasticsearch';
import { AggregationOptionsByType } from '../../../typings/elasticsearch/aggregations';
import { ESSearchResponse } from '../../../typings/elasticsearch';
import { getVizColorForIndex } from '../../../common/viz_colors';
import { GenericMetricsRequest } from './fetch_and_transform_metrics';

export type GenericMetricsChart = ReturnType<
typeof transformDataToMetricsChart
>;

interface MetricsAggregationMap {
min: AggregationOptionsByType['min'];
max: AggregationOptionsByType['max'];
sum: AggregationOptionsByType['sum'];
avg: AggregationOptionsByType['avg'];
}

type GenericMetricsRequest = Overwrite<
ESSearchRequest,
{
body: {
aggs: {
timeseriesData: {
date_histogram: AggregationOptionsByType['date_histogram'];
aggs: Record<string, Unionize<MetricsAggregationMap>>;
};
} & Record<string, Unionize<MetricsAggregationMap>>;
};
}
>;

export function transformDataToMetricsChart(
result: ESSearchResponse<unknown, GenericMetricsRequest>,
chartBase: ChartBase
Expand All @@ -51,11 +26,7 @@ export function transformDataToMetricsChart(
yUnit: chartBase.yUnit,
noHits: hits.total.value === 0,
series: Object.keys(chartBase.series).map((seriesKey, i) => {
const overallValue = (aggregations?.[seriesKey] as
| {
value: number | null;
}
| undefined)?.value;
const overallValue = aggregations?.[seriesKey]?.value;

return {
title: chartBase.series[seriesKey].title,
Expand All @@ -66,7 +37,7 @@ export function transformDataToMetricsChart(
overallValue,
data:
timeseriesData?.buckets.map((bucket) => {
const { value } = bucket[seriesKey] as { value: number | null };
const { value } = bucket[seriesKey];
const y = value === null || isNaN(value) ? null : value;
return {
x: bucket.key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { arrayUnionToCallable } from '../../../../common/utils/array_union_to_callable';
import {
PROCESSOR_EVENT,
TRANSACTION_DURATION,
Expand Down Expand Up @@ -187,7 +186,7 @@ export const getTransactionRates = async ({

const deltaAsMinutes = getDeltaAsMinutes(setup);

return arrayUnionToCallable(aggregations.services.buckets).map((bucket) => {
return aggregations.services.buckets.map((bucket) => {
const transactionsPerMinute = bucket.doc_count / deltaAsMinutes;
return {
serviceName: bucket.key as string,
Expand Down
Loading