Skip to content

Commit

Permalink
refactor: adapt errors to use fields instead of _source
Browse files Browse the repository at this point in the history
  • Loading branch information
rmyz committed Oct 4, 2024
1 parent 71cfaad commit e50cf41
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -66,16 +67,18 @@ 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
] as const),
},
});
const errorSampleIds = resp.hits.hits.map((item) => {
const source = item._source;
return source?.error?.id;
const fields = item.fields;
const errorId = normalizeValue<string>(fields?.['error.id']);

return errorId;
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e50cf41

Please sign in to comment.