Skip to content

Commit

Permalink
[APM] Remove projections (elastic#118327) (elastic#118675)
Browse files Browse the repository at this point in the history
* Delete errors projection

* Remove `getMetricsProjection`

* Remove `getServiceNodesProjection`

* Fix tests

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Søren Louv-Jansen <soren.louv@elastic.co>
  • Loading branch information
kibanamachine and sorenlouv authored Nov 16, 2021
1 parent 24f8717 commit 0365722
Show file tree
Hide file tree
Showing 22 changed files with 292 additions and 553 deletions.
20 changes: 19 additions & 1 deletion x-pack/plugins/apm/common/utils/environment_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
*/

import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { SERVICE_ENVIRONMENT } from '../elasticsearch_fieldnames';
import {
SERVICE_ENVIRONMENT,
SERVICE_NODE_NAME,
} from '../elasticsearch_fieldnames';
import {
ENVIRONMENT_ALL,
ENVIRONMENT_NOT_DEFINED,
} from '../environment_filter_values';
import { SERVICE_NODE_NAME_MISSING } from '../service_nodes';

export function environmentQuery(
environment: string
Expand All @@ -25,3 +29,17 @@ export function environmentQuery(

return [{ term: { [SERVICE_ENVIRONMENT]: environment } }];
}

export function serviceNodeNameQuery(
serviceNodeName?: string
): QueryDslQueryContainer[] {
if (!serviceNodeName) {
return [];
}

if (serviceNodeName === SERVICE_NODE_NAME_MISSING) {
return [{ bool: { must_not: [{ exists: { field: SERVICE_NODE_NAME } }] } }];
}

return [{ term: { [SERVICE_NODE_NAME]: serviceNodeName } }];
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@

import { EuiTitle } from '@elastic/eui';
import React from 'react';
import { APIReturnType } from '../../../../services/rest/createCallApmApi';
import {
asDecimal,
asInteger,
asPercent,
getDurationFormatter,
getFixedByteFormatter,
} from '../../../../../common/utils/formatters';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { GenericMetricsChart } from '../../../../../server/lib/metrics/transform_metrics_chart';
import { Maybe } from '../../../../../typings/common';
import { FETCH_STATUS } from '../../../../hooks/use_fetcher';
import { TimeseriesChart } from '../timeseries_chart';
Expand All @@ -24,7 +23,11 @@ import {
getResponseTimeTickFormatter,
} from '../transaction_charts/helper';

function getYTickFormatter(chart: GenericMetricsChart) {
type MetricChartApiResponse =
APIReturnType<'GET /internal/apm/services/{serviceName}/metrics/charts'>;
type MetricChart = MetricChartApiResponse['charts'][0];

function getYTickFormatter(chart: MetricChart) {
const max = getMaxY(chart.series);

switch (chart.yUnit) {
Expand All @@ -50,7 +53,7 @@ function getYTickFormatter(chart: GenericMetricsChart) {
interface Props {
start: Maybe<number | string>;
end: Maybe<number | string>;
chart: GenericMetricsChart;
chart: MetricChart;
fetchStatus: FETCH_STATUS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
* 2.0.
*/

// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { MetricsChartsByAgentAPIResponse } from '../../server/lib/metrics/get_metrics_chart_data_by_agent';
import type { APIReturnType } from '../services/rest/createCallApmApi';
import { useApmServiceContext } from '../context/apm_service/use_apm_service_context';
import { useFetcher } from './use_fetcher';
import { useTimeRange } from './use_time_range';
import { useApmParams } from './use_apm_params';

const INITIAL_DATA: MetricsChartsByAgentAPIResponse = {
type MetricChartApiResponse =
APIReturnType<'GET /internal/apm/services/{serviceName}/metrics/charts'>;

const INITIAL_DATA: MetricChartApiResponse = {
charts: [],
};

Expand Down
49 changes: 25 additions & 24 deletions x-pack/plugins/apm/server/lib/errors/get_error_groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
* 2.0.
*/

import { AggregationsTermsAggregationOrder } from '@elastic/elasticsearch/lib/api/types';
import { ProcessorEvent } from '../../../common/processor_event';
import { environmentQuery } from '../../../common/utils/environment_query';
import { kqlQuery, rangeQuery } from '../../../../observability/server';
import {
ERROR_CULPRIT,
ERROR_EXC_HANDLED,
ERROR_EXC_MESSAGE,
ERROR_EXC_TYPE,
ERROR_GROUP_ID,
ERROR_LOG_MESSAGE,
SERVICE_NAME,
} from '../../../common/elasticsearch_fieldnames';
import { getErrorGroupsProjection } from '../../projections/errors';
import { mergeProjection } from '../../projections/util/merge_projection';
import { getErrorName } from '../helpers/get_error_name';
import { Setup } from '../helpers/setup_request';

Expand Down Expand Up @@ -42,27 +45,31 @@ export async function getErrorGroups({
// sort buckets by last occurrence of error
const sortByLatestOccurrence = sortField === 'latestOccurrenceAt';

const projection = getErrorGroupsProjection({
environment,
kuery,
serviceName,
start,
end,
});

const order = sortByLatestOccurrence
? {
max_timestamp: sortDirection,
}
const maxTimestampAggKey = 'max_timestamp';
const order: AggregationsTermsAggregationOrder = sortByLatestOccurrence
? { [maxTimestampAggKey]: sortDirection }
: { _count: sortDirection };

const params = mergeProjection(projection, {
const params = {
apm: {
events: [ProcessorEvent.error as const],
},
body: {
size: 0,
query: {
bool: {
filter: [
{ term: { [SERVICE_NAME]: serviceName } },
...rangeQuery(start, end),
...environmentQuery(environment),
...kqlQuery(kuery),
],
},
},
aggs: {
error_groups: {
terms: {
...projection.body.aggs.error_groups.terms,
field: ERROR_GROUP_ID,
size: 500,
order,
},
Expand All @@ -83,19 +90,13 @@ export async function getErrorGroups({
},
},
...(sortByLatestOccurrence
? {
max_timestamp: {
max: {
field: '@timestamp',
},
},
}
? { [maxTimestampAggKey]: { max: { field: '@timestamp' } } }
: {}),
},
},
},
},
});
};

const resp = await apmEventClient.search('get_error_groups', params);

Expand Down
6 changes: 2 additions & 4 deletions x-pack/plugins/apm/server/lib/metrics/by_agent/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Setup } from '../../helpers/setup_request';
import { getCPUChartData } from './shared/cpu';
import { getMemoryChartData } from './shared/memory';

export async function getDefaultMetricsCharts({
export function getDefaultMetricsCharts({
environment,
kuery,
serviceName,
Expand All @@ -24,10 +24,8 @@ export async function getDefaultMetricsCharts({
start: number;
end: number;
}) {
const charts = await Promise.all([
return Promise.all([
getCPUChartData({ environment, kuery, setup, serviceName, start, end }),
getMemoryChartData({ environment, kuery, setup, serviceName, start, end }),
]);

return { charts };
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ import { isFiniteNumber } from '../../../../../../common/utils/is_finite_number'
import { Setup } from '../../../../helpers/setup_request';
import { getMetricsDateHistogramParams } from '../../../../helpers/metrics';
import { ChartBase } from '../../../types';
import { getMetricsProjection } from '../../../../../projections/metrics';
import { mergeProjection } from '../../../../../projections/util/merge_projection';

import {
AGENT_NAME,
LABEL_NAME,
METRIC_JAVA_GC_COUNT,
METRIC_JAVA_GC_TIME,
SERVICE_NAME,
} from '../../../../../../common/elasticsearch_fieldnames';
import { getBucketSize } from '../../../../helpers/get_bucket_size';
import { getVizColorForIndex } from '../../../../../../common/viz_colors';
import { JAVA_AGENT_NAMES } from '../../../../../../common/agent_name';
import {
environmentQuery,
serviceNodeNameQuery,
} from '../../../../../../common/utils/environment_query';
import {
kqlQuery,
rangeQuery,
} from '../../../../../../../observability/server';
import { ProcessorEvent } from '../../../../../../common/processor_event';

export async function fetchAndTransformGcMetrics({
environment,
Expand Down Expand Up @@ -50,26 +59,24 @@ export async function fetchAndTransformGcMetrics({

const { bucketSize } = getBucketSize({ start, end });

const projection = getMetricsProjection({
environment,
kuery,
serviceName,
serviceNodeName,
start,
end,
});

// GC rate and time are reported by the agents as monotonically
// increasing counters, which means that we have to calculate
// the delta in an es query. In the future agent might start
// reporting deltas.
const params = mergeProjection(projection, {
const params = {
apm: {
events: [ProcessorEvent.metric],
},
body: {
size: 0,
query: {
bool: {
filter: [
...projection.body.query.bool.filter,
{ term: { [SERVICE_NAME]: serviceName } },
...serviceNodeNameQuery(serviceNodeName),
...rangeQuery(start, end),
...environmentQuery(environment),
...kqlQuery(kuery),
{ exists: { field: fieldName } },
{ terms: { [AGENT_NAME]: JAVA_AGENT_NAMES } },
],
Expand Down Expand Up @@ -114,7 +121,7 @@ export async function fetchAndTransformGcMetrics({
},
},
},
});
};

const response = await apmEventClient.search(operationName, params);

Expand Down
6 changes: 2 additions & 4 deletions x-pack/plugins/apm/server/lib/metrics/by_agent/java/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function getJavaMetricsCharts({
start: number;
end: number;
}) {
return withApmSpan('get_java_system_metric_charts', async () => {
return withApmSpan('get_java_system_metric_charts', () => {
const options = {
environment,
kuery,
Expand All @@ -43,7 +43,7 @@ export function getJavaMetricsCharts({
end,
};

const charts = await Promise.all([
return Promise.all([
getCPUChartData(options),
getMemoryChartData(options),
getHeapMemoryChart(options),
Expand All @@ -52,7 +52,5 @@ export function getJavaMetricsCharts({
getGcRateChart(options),
getGcTimeChart(options),
]);

return { charts };
});
}
Loading

0 comments on commit 0365722

Please sign in to comment.