Skip to content

Commit

Permalink
[APM] Return default error message when log and exception are not ava…
Browse files Browse the repository at this point in the history
…ilable (elastic#106890)
  • Loading branch information
cauemarcondes authored and vadimkibana committed Aug 8, 2021
1 parent 5284c68 commit 0baaebb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
34 changes: 34 additions & 0 deletions x-pack/plugins/apm/server/lib/helpers/get_error_name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { APMError } from '../../../typings/es_schemas/ui/apm_error';
import { NOT_AVAILABLE_LABEL } from '../../../common/i18n';
import { getErrorName } from './get_error_name';

describe('getErrorName', () => {
it('returns log message', () => {
expect(
getErrorName({
error: {
log: { message: 'bar' },
exception: [{ message: 'foo' }],
},
} as APMError)
).toEqual('bar');
});
it('returns exception message', () => {
expect(
getErrorName({
error: {
exception: [{ message: 'foo' }],
},
} as APMError)
).toEqual('foo');
});
it('returns default message', () => {
expect(getErrorName({} as APMError)).toEqual(NOT_AVAILABLE_LABEL);
});
});
8 changes: 6 additions & 2 deletions x-pack/plugins/apm/server/lib/helpers/get_error_name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
* 2.0.
*/

import { NOT_AVAILABLE_LABEL } from '../../../common/i18n';
import { Maybe } from '../../../typings/common';
import { APMError } from '../../../typings/es_schemas/ui/apm_error';

export function getErrorName({ error }: APMError) {
return error.log?.message || error.exception?.[0]?.message;
export function getErrorName({ error }: { error: Maybe<APMError['error']> }) {
return (
error?.log?.message || error?.exception?.[0]?.message || NOT_AVAILABLE_LABEL
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
* 2.0.
*/

import { kqlQuery, rangeQuery } from '../../../../../observability/server';
import {
ERROR_EXC_MESSAGE,
ERROR_GROUP_ID,
ERROR_LOG_MESSAGE,
SERVICE_NAME,
TRANSACTION_TYPE,
} from '../../../../common/elasticsearch_fieldnames';
import { NOT_AVAILABLE_LABEL } from '../../../../common/i18n';
import { ProcessorEvent } from '../../../../common/processor_event';
import { rangeQuery, kqlQuery } from '../../../../../observability/server';
import { environmentQuery } from '../../../../common/utils/environment_query';
import { getErrorName } from '../../helpers/get_error_name';
import { Setup, SetupTimeRange } from '../../helpers/setup_request';
Expand Down Expand Up @@ -82,8 +81,7 @@ export async function getServiceErrorGroupMainStatistics({
const errorGroups =
response.aggregations?.error_groups.buckets.map((bucket) => ({
group_id: bucket.key as string,
name:
getErrorName(bucket.sample.hits.hits[0]._source) ?? NOT_AVAILABLE_LABEL,
name: getErrorName(bucket.sample.hits.hits[0]._source),
last_seen: new Date(
bucket.sample.hits.hits[0]?._source['@timestamp']
).getTime(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
* 2.0.
*/

import { ValuesType } from 'utility-types';
import { orderBy } from 'lodash';
import { NOT_AVAILABLE_LABEL } from '../../../../common/i18n';
import { ValuesType } from 'utility-types';
import { kqlQuery, rangeQuery } from '../../../../../observability/server';
import { PromiseReturnType } from '../../../../../observability/typings/common';
import { rangeQuery, kqlQuery } from '../../../../../observability/server';
import { environmentQuery } from '../../../../common/utils/environment_query';
import { ProcessorEvent } from '../../../../common/processor_event';
import {
ERROR_EXC_MESSAGE,
ERROR_GROUP_ID,
ERROR_LOG_MESSAGE,
SERVICE_NAME,
TRANSACTION_TYPE,
} from '../../../../common/elasticsearch_fieldnames';
import { Setup, SetupTimeRange } from '../../helpers/setup_request';
import { ProcessorEvent } from '../../../../common/processor_event';
import { environmentQuery } from '../../../../common/utils/environment_query';
import { withApmSpan } from '../../../utils/with_apm_span';
import { getBucketSize } from '../../helpers/get_bucket_size';
import { getErrorName } from '../../helpers/get_error_name';
import { withApmSpan } from '../../../utils/with_apm_span';
import { Setup, SetupTimeRange } from '../../helpers/setup_request';

export type ServiceErrorGroupItem = ValuesType<
PromiseReturnType<typeof getServiceErrorGroups>
Expand Down Expand Up @@ -108,9 +107,7 @@ export async function getServiceErrorGroups({
const errorGroups =
response.aggregations?.error_groups.buckets.map((bucket) => ({
group_id: bucket.key as string,
name:
getErrorName(bucket.sample.hits.hits[0]._source) ??
NOT_AVAILABLE_LABEL,
name: getErrorName(bucket.sample.hits.hits[0]._source),
last_seen: new Date(
bucket.sample.hits.hits[0]?._source['@timestamp']
).getTime(),
Expand Down

0 comments on commit 0baaebb

Please sign in to comment.