From e50cf41619bceec3fed5bd826f6c8d40829afd5d Mon Sep 17 00:00:00 2001 From: Sergi Romeu Date: Fri, 4 Oct 2024 15:56:17 +0200 Subject: [PATCH] refactor: adapt errors to use fields instead of _source --- .../get_error_groups/get_error_group_sample_ids.ts | 9 ++++++--- .../get_error_groups/get_error_sample_details.ts | 10 ++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/observability_solution/apm/server/routes/errors/get_error_groups/get_error_group_sample_ids.ts b/x-pack/plugins/observability_solution/apm/server/routes/errors/get_error_groups/get_error_group_sample_ids.ts index dc35c963b14b6..43b950f3a2293 100644 --- a/x-pack/plugins/observability_solution/apm/server/routes/errors/get_error_groups/get_error_group_sample_ids.ts +++ b/x-pack/plugins/observability_solution/apm/server/routes/errors/get_error_groups/get_error_group_sample_ids.ts @@ -6,6 +6,7 @@ */ import { rangeQuery, kqlQuery } from '@kbn/observability-plugin/server'; +import { normalizeValue } from '../../../utils/es_fields_mappings/es_fields_mappings_helpers'; import { asMutableArray } from '../../../../common/utils/as_mutable_array'; import { ERROR_GROUP_ID, @@ -66,7 +67,7 @@ export async function getErrorGroupSampleIds({ should: [{ term: { [TRANSACTION_SAMPLED]: true } }], // prefer error samples with related transactions }, }, - _source: [ERROR_ID, 'transaction'], + fields: [ERROR_ID], sort: asMutableArray([ { _score: { order: 'desc' } }, // sort by _score first to ensure that errors with transaction.sampled:true ends up on top { '@timestamp': { order: 'desc' } }, // sort by timestamp to get the most recent error @@ -74,8 +75,10 @@ export async function getErrorGroupSampleIds({ }, }); const errorSampleIds = resp.hits.hits.map((item) => { - const source = item._source; - return source?.error?.id; + const fields = item.fields; + const errorId = normalizeValue(fields?.['error.id']); + + return errorId; }); return { diff --git a/x-pack/plugins/observability_solution/apm/server/routes/errors/get_error_groups/get_error_sample_details.ts b/x-pack/plugins/observability_solution/apm/server/routes/errors/get_error_groups/get_error_sample_details.ts index 287fce04478ec..0e515ee6493f8 100644 --- a/x-pack/plugins/observability_solution/apm/server/routes/errors/get_error_groups/get_error_sample_details.ts +++ b/x-pack/plugins/observability_solution/apm/server/routes/errors/get_error_groups/get_error_sample_details.ts @@ -14,6 +14,7 @@ import { APMEventClient } from '../../../lib/helpers/create_es_client/create_apm import { getTransaction } from '../../transactions/get_transaction'; import { Transaction } from '../../../../typings/es_schemas/ui/transaction'; import { APMError } from '../../../../typings/es_schemas/ui/apm_error'; +import { errorSampleDetailsMapping } from '../../../utils/es_fields_mappings/error'; export interface ErrorSampleDetailsResponse { transaction: Transaction | undefined; @@ -60,24 +61,25 @@ export async function getErrorSampleDetails({ ], }, }, + fields: ['*'], }, }; const resp = await apmEventClient.search('get_error_sample_details', params); - const error = resp.hits.hits[0]?._source; + const error = errorSampleDetailsMapping(resp.hits.hits[0]?.fields) as APMError; const transactionId = error?.transaction?.id; const traceId = error?.trace?.id; - let transaction; + let transaction: Transaction | undefined; if (transactionId && traceId) { - transaction = await getTransaction({ + transaction = (await getTransaction({ transactionId, traceId, apmEventClient, start, end, - }); + })) as Transaction; } return {