Skip to content

Commit 60dfb0f

Browse files
authored
[APM] Refactor mobile & profiling to use accessKnownApmEventFields (#244794)
## Summary Closes #244159 This PR replaces the usage of `unflattenKnownApmEventFields` with `accessKnownApmEventFields` in the mobile and profiling APM routes. ## How to test - Go to Service Inventory, select a mobile service. - No regressions noted with errors/crashes tab. - Same to be found with profiling feature. - CI must pass with no regressions
1 parent ff57912 commit 60dfb0f

File tree

3 files changed

+43
-42
lines changed

3 files changed

+43
-42
lines changed

x-pack/solutions/observability/plugins/apm/server/routes/mobile/crashes/get_crash_groups/get_crash_group_main_statistics.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import type { AggregationsAggregateOrder } from '@elastic/elasticsearch/lib/api/types';
99
import { kqlQuery, rangeQuery, termQuery } from '@kbn/observability-plugin/server';
1010
import { ProcessorEvent } from '@kbn/observability-plugin/common';
11-
import { unflattenKnownApmEventFields } from '@kbn/apm-data-access-plugin/server/utils';
11+
import { accessKnownApmEventFields } from '@kbn/apm-data-access-plugin/server/utils';
1212
import { asMutableArray } from '../../../../../common/utils/as_mutable_array';
1313
import {
1414
ERROR_CULPRIT,
@@ -24,8 +24,8 @@ import {
2424
AT_TIMESTAMP,
2525
} from '../../../../../common/es_fields/apm';
2626
import { environmentQuery } from '../../../../../common/utils/environment_query';
27-
import { getErrorName } from '../../../../lib/helpers/get_error_name';
2827
import type { APMEventClient } from '../../../../lib/helpers/create_es_client/create_apm_event_client';
28+
import { NOT_AVAILABLE_LABEL } from '../../../../../common/i18n';
2929

3030
export type MobileCrashGroupMainStatisticsResponse = Array<{
3131
groupId: string;
@@ -133,27 +133,25 @@ export async function getMobileCrashGroupMainStatistics({
133133
? bucket.sample.hits.hits[0]._source
134134
: undefined;
135135

136-
const event = unflattenKnownApmEventFields(bucket.sample.hits.hits[0].fields, requiredFields);
136+
const fields = bucket.sample.hits.hits[0].fields;
137+
const event = accessKnownApmEventFields(fields).requireFields(requiredFields);
137138

138-
const mergedEvent = {
139-
...event,
140-
error: {
141-
...(event.error ?? {}),
142-
exception:
143-
(errorSource?.error.exception?.length ?? 0) > 0
144-
? errorSource?.error.exception
145-
: event?.error.exception && [event.error.exception],
146-
},
139+
const exception = errorSource?.error.exception?.[0] ?? {
140+
message: event[ERROR_EXC_MESSAGE],
141+
handled: event[ERROR_EXC_HANDLED],
142+
type: event[ERROR_EXC_TYPE],
147143
};
148144

145+
const errorName = event[ERROR_LOG_MESSAGE] || exception.message || NOT_AVAILABLE_LABEL;
146+
149147
return {
150-
groupId: event.error?.grouping_key,
151-
name: getErrorName(mergedEvent),
152-
lastSeen: new Date(mergedEvent[AT_TIMESTAMP]).getTime(),
148+
groupId: event[ERROR_GROUP_ID],
149+
name: errorName,
150+
lastSeen: new Date(event[AT_TIMESTAMP]).getTime(),
153151
occurrences: bucket.doc_count,
154-
culprit: mergedEvent.error.culprit,
155-
handled: mergedEvent.error.exception?.[0].handled,
156-
type: mergedEvent.error.exception?.[0].type,
152+
culprit: event[ERROR_CULPRIT],
153+
handled: exception.handled,
154+
type: exception.type,
157155
};
158156
}) ?? []
159157
);

x-pack/solutions/observability/plugins/apm/server/routes/mobile/errors/get_mobile_error_group_main_statistics.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import type { AggregationsAggregateOrder } from '@elastic/elasticsearch/lib/api/types';
99
import { kqlQuery, rangeQuery, termQuery } from '@kbn/observability-plugin/server';
1010
import { ProcessorEvent } from '@kbn/observability-plugin/common';
11-
import { unflattenKnownApmEventFields } from '@kbn/apm-data-access-plugin/server/utils';
11+
import { accessKnownApmEventFields } from '@kbn/apm-data-access-plugin/server/utils';
1212
import { asMutableArray } from '../../../../common/utils/as_mutable_array';
1313
import {
1414
AT_TIMESTAMP,
@@ -23,8 +23,8 @@ import {
2323
TRANSACTION_TYPE,
2424
} from '../../../../common/es_fields/apm';
2525
import { environmentQuery } from '../../../../common/utils/environment_query';
26-
import { getErrorName } from '../../../lib/helpers/get_error_name';
2726
import type { APMEventClient } from '../../../lib/helpers/create_es_client/create_apm_event_client';
27+
import { NOT_AVAILABLE_LABEL } from '../../../../common/i18n';
2828

2929
export type MobileErrorGroupMainStatisticsResponse = Array<{
3030
groupId: string;
@@ -134,27 +134,26 @@ export async function getMobileErrorGroupMainStatistics({
134134
? bucket.sample.hits.hits[0]._source
135135
: undefined;
136136

137-
const event = unflattenKnownApmEventFields(bucket.sample.hits.hits[0].fields, requiredFields);
137+
const event = accessKnownApmEventFields(bucket.sample.hits.hits[0].fields).requireFields(
138+
requiredFields
139+
);
138140

139-
const mergedEvent = {
140-
...event,
141-
error: {
142-
...(event.error ?? {}),
143-
exception:
144-
(errorSource?.error.exception?.length ?? 0) > 0
145-
? errorSource?.error.exception
146-
: event?.error.exception && [event.error.exception],
147-
},
141+
const exception = errorSource?.error.exception?.[0] ?? {
142+
message: event[ERROR_EXC_MESSAGE],
143+
handled: event[ERROR_EXC_HANDLED],
144+
type: event[ERROR_EXC_TYPE],
148145
};
149146

147+
const errorName = event[ERROR_LOG_MESSAGE] || exception.message || NOT_AVAILABLE_LABEL;
148+
150149
return {
151-
groupId: event.error?.grouping_key,
152-
name: getErrorName(mergedEvent),
153-
lastSeen: new Date(mergedEvent[AT_TIMESTAMP]).getTime(),
150+
groupId: event[ERROR_GROUP_ID],
151+
name: errorName,
152+
lastSeen: new Date(event[AT_TIMESTAMP]).getTime(),
154153
occurrences: bucket.doc_count,
155-
culprit: mergedEvent.error.culprit,
156-
handled: mergedEvent.error.exception?.[0].handled,
157-
type: mergedEvent.error.exception?.[0].type,
154+
culprit: event[ERROR_CULPRIT],
155+
handled: exception.handled,
156+
type: exception.type,
158157
};
159158
}) ?? []
160159
);

x-pack/solutions/observability/plugins/apm/server/routes/profiling/get_stacktraces_ids_field.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
*/
77
import { ProcessorEvent } from '@kbn/observability-plugin/common';
88
import { rangeQuery, termQuery, kqlQuery } from '@kbn/observability-plugin/server';
9-
import { isEmpty } from 'lodash';
10-
import { unflattenKnownApmEventFields } from '@kbn/apm-data-access-plugin/server/utils';
9+
import { accessKnownApmEventFields } from '@kbn/apm-data-access-plugin/server/utils';
1110
import {
1211
ELASTIC_PROFILER_STACK_TRACE_IDS,
1312
SERVICE_NAME,
@@ -17,6 +16,7 @@ import {
1716
} from '../../../common/es_fields/apm';
1817
import { environmentQuery } from '../../../common/utils/environment_query';
1918
import type { APMEventClient } from '../../lib/helpers/create_es_client/create_apm_event_client';
19+
import { maybe } from '../../../common/utils/maybe';
2020

2121
export async function getStacktracesIdsField({
2222
apmEventClient,
@@ -64,11 +64,15 @@ export async function getStacktracesIdsField({
6464
},
6565
});
6666

67-
const field = unflattenKnownApmEventFields(response.hits.hits[0]?.fields, [
68-
ELASTIC_PROFILER_STACK_TRACE_IDS,
69-
]);
67+
const fields = maybe(response.hits.hits[0])?.fields;
7068

71-
if (!isEmpty(field.elastic.profiler_stack_trace_ids)) {
69+
const field =
70+
fields &&
71+
accessKnownApmEventFields(fields).requireFields([ELASTIC_PROFILER_STACK_TRACE_IDS])[
72+
ELASTIC_PROFILER_STACK_TRACE_IDS
73+
];
74+
75+
if (field?.length) {
7276
return ELASTIC_PROFILER_STACK_TRACE_IDS;
7377
}
7478

0 commit comments

Comments
 (0)