From ebc8f05ccb69983479d218c6a3b42953c530e2b3 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Thu, 21 Sep 2023 14:20:08 +0200 Subject: [PATCH 01/28] Creating get mobile launches by location --- .../mobile/get_mobile_launches_by_location.ts | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts new file mode 100644 index 00000000000000..89c105117911bf --- /dev/null +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts @@ -0,0 +1,109 @@ +/* + * 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 {ProcessorEvent} from '@kbn/observability-plugin/common'; +import { + kqlQuery, + rangeQuery, + termQuery, +} from '@kbn/observability-plugin/server'; +import {SERVICE_NAME} from '../../../common/es_fields/apm'; +import {APMEventClient} from '../../lib/helpers/create_es_client/create_apm_event_client'; +import {getOffsetInMs} from '../../../common/utils/get_offset_in_ms'; +import {getBucketSize} from '../../../common/utils/get_bucket_size'; +import {environmentQuery} from '../../../common/utils/environment_query'; + +interface Props { + kuery: string; + apmEventClient: APMEventClient; + serviceName: string; + environment: string; + start: number; + end: number; + locationField?: string; + offset?: string; +} + +export async function getLaunchesByLocation({ + kuery, + apmEventClient, + serviceName, + environment, + start, + end, + locationField, + offset, + }: Props) { + const {startWithOffset, endWithOffset} = getOffsetInMs({ + start, + end, + offset, + }); + + const {intervalString} = getBucketSize({ + start: startWithOffset, + end: endWithOffset, + minBucketSize: 60, + }); + + const aggs = { + launches: { + filter: {term: {["labels.lifecycle_state"]: 'created'}}, + aggs: { + launchesByLocation: { + terms: { + field: locationField, + }, + }, + }, + }, + }; + const response = await apmEventClient.search('get_mobile_location_launches', { + apm: { + events: [ProcessorEvent.transaction], + }, + body: { + track_total_hits: false, + size: 0, + query: { + bool: { + filter: [ + ...termQuery(SERVICE_NAME, serviceName), + ...rangeQuery(startWithOffset, endWithOffset), + ...environmentQuery(environment), + ...kqlQuery(kuery), + ], + }, + }, + aggs: { + timeseries: { + date_histogram: { + field: '@timestamp', + fixed_interval: intervalString, + min_doc_count: 0, + }, + aggs, + }, + ...aggs, + }, + }, + }); + return { + location: response.aggregations?.launches?.launchesByLocation?.buckets[0] + ?.key as string, + value: + response.aggregations?.launches?.launchesByLocation?.buckets[0] + ?.doc_count ?? 0, + timeseries: + response.aggregations?.timeseries?.buckets.map((bucket) => ({ + x: bucket.key, + y: + response.aggregations?.launches?.launchesByLocation?.buckets[0] + ?.doc_count ?? 0, + })) ?? [], + }; +} From e8fb0fe987d2639729e197a4ffb410728561b462 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Thu, 21 Sep 2023 14:26:59 +0200 Subject: [PATCH 02/28] Adding most launches to location stats --- .../mobile/get_mobile_launches_by_location.ts | 36 +++++++++---------- .../mobile/get_mobile_location_stats.ts | 6 +++- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts index 89c105117911bf..5f324f9b0f698c 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts @@ -5,17 +5,17 @@ * 2.0. */ -import {ProcessorEvent} from '@kbn/observability-plugin/common'; +import { ProcessorEvent } from '@kbn/observability-plugin/common'; import { kqlQuery, rangeQuery, termQuery, } from '@kbn/observability-plugin/server'; -import {SERVICE_NAME} from '../../../common/es_fields/apm'; -import {APMEventClient} from '../../lib/helpers/create_es_client/create_apm_event_client'; -import {getOffsetInMs} from '../../../common/utils/get_offset_in_ms'; -import {getBucketSize} from '../../../common/utils/get_bucket_size'; -import {environmentQuery} from '../../../common/utils/environment_query'; +import { SERVICE_NAME } from '../../../common/es_fields/apm'; +import { APMEventClient } from '../../lib/helpers/create_es_client/create_apm_event_client'; +import { getOffsetInMs } from '../../../common/utils/get_offset_in_ms'; +import { getBucketSize } from '../../../common/utils/get_bucket_size'; +import { environmentQuery } from '../../../common/utils/environment_query'; interface Props { kuery: string; @@ -29,22 +29,22 @@ interface Props { } export async function getLaunchesByLocation({ - kuery, - apmEventClient, - serviceName, - environment, - start, - end, - locationField, - offset, - }: Props) { - const {startWithOffset, endWithOffset} = getOffsetInMs({ + kuery, + apmEventClient, + serviceName, + environment, + start, + end, + locationField, + offset, +}: Props) { + const { startWithOffset, endWithOffset } = getOffsetInMs({ start, end, offset, }); - const {intervalString} = getBucketSize({ + const { intervalString } = getBucketSize({ start: startWithOffset, end: endWithOffset, minBucketSize: 60, @@ -52,7 +52,7 @@ export async function getLaunchesByLocation({ const aggs = { launches: { - filter: {term: {["labels.lifecycle_state"]: 'created'}}, + filter: { term: { ['labels.lifecycle_state']: 'created' } }, aggs: { launchesByLocation: { terms: { diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts index ccc777e3cfe31f..1be15656555920 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts @@ -10,6 +10,7 @@ import { APMEventClient } from '../../lib/helpers/create_es_client/create_apm_ev import { getSessionsByLocation } from './get_mobile_sessions_by_location'; import { getHttpRequestsByLocation } from './get_mobile_http_requests_by_location'; import { getCrashesByLocation } from './get_mobile_crashes_by_location'; +import { getLaunchesByLocation } from './get_mobile_launches_by_location'; import { Maybe } from '../../../typings/common'; export type Timeseries = Array<{ x: number; y: number }>; @@ -69,16 +70,18 @@ async function getMobileLocationStats({ offset, }; - const [mostSessions, mostRequests, mostCrashes] = await Promise.all([ + const [mostSessions, mostRequests, mostCrashes, mostLaunches] = await Promise.all([ getSessionsByLocation({ ...commonProps }), getHttpRequestsByLocation({ ...commonProps }), getCrashesByLocation({ ...commonProps }), + getLaunchesByLocation({ ...commonProps }), ]); return { mostSessions, mostRequests, mostCrashes, + mostLaunches }; } @@ -117,6 +120,7 @@ export async function getMobileLocationStatsPeriods({ mostSessions: { value: null, timeseries: [] }, mostRequests: { value: null, timeseries: [] }, mostCrashes: { value: null, timeseries: [] }, + mostLaunches: { value: null, timeseries: [] }, }; const [currentPeriod, previousPeriod] = await Promise.all([ From cbcdff153668c141622c66c350d49199e95fe7c2 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Wed, 11 Oct 2023 13:59:15 +0200 Subject: [PATCH 03/28] Connecting most launches data to dashboard --- .../mobile/service_overview/stats/location_stats.tsx | 11 ++++++----- .../server/routes/mobile/get_mobile_location_stats.ts | 5 +++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/mobile/service_overview/stats/location_stats.tsx b/x-pack/plugins/apm/public/components/app/mobile/service_overview/stats/location_stats.tsx index 22e95e54817ddd..24bbd7709d8d7b 100644 --- a/x-pack/plugins/apm/public/components/app/mobile/service_overview/stats/location_stats.tsx +++ b/x-pack/plugins/apm/public/components/app/mobile/service_overview/stats/location_stats.tsx @@ -163,17 +163,18 @@ export function MobileLocationStats({ trendShape: MetricTrendShape.Area, }, { - color: euiTheme.eui.euiColorDisabled, + color: euiTheme.eui.euiColorLightestShade, title: i18n.translate('xpack.apm.mobile.location.metrics.launches', { defaultMessage: 'Most launches', }), - subtitle: i18n.translate('xpack.apm.mobile.coming.soon', { - defaultMessage: 'Coming Soon', + extra: getComparisonValueFormatter({ + currentPeriodValue: currentPeriod?.mostLaunches.value, + previousPeriodValue: previousPeriod?.mostLaunches.value, }), icon: getIcon('launch'), - value: NOT_AVAILABLE_LABEL, + value: currentPeriod?.mostLaunches.location ?? NOT_AVAILABLE_LABEL, valueFormatter: (value) => `${value}`, - trend: [], + trend: currentPeriod?.mostLaunches.timeseries, trendShape: MetricTrendShape.Area, }, ]; diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts index 1be15656555920..eb590d20376569 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts @@ -31,6 +31,11 @@ interface LocationStats { value: Maybe; timeseries: Timeseries; }; + mostLaunches: { + location?: string; + value: Maybe; + timeseries: Timeseries; + }; } export interface MobileLocationStats { From e67c0d0d8d8d8690ef9342b57b98eb69dfb7f71a Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Wed, 11 Oct 2023 16:39:28 +0200 Subject: [PATCH 04/28] Created helper function to get log events --- .../apm/typings/es_schemas/raw/event_raw.ts | 27 +++++++++++++++++++ .../apm/typings/es_schemas/ui/event.ts | 13 +++++++++ 2 files changed, 40 insertions(+) create mode 100644 x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts create mode 100644 x-pack/plugins/apm/typings/es_schemas/ui/event.ts diff --git a/x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts b/x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts new file mode 100644 index 00000000000000..1607ca77253eef --- /dev/null +++ b/x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts @@ -0,0 +1,27 @@ +/* + * 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 {APMBaseDoc} from './apm_base_doc'; +import {TimestampUs} from './fields/timestamp_us'; + +export interface Processor { + name: 'log'; + event: 'log'; +} + +export interface LogRaw extends APMBaseDoc { + processor: Processor; + timestamp: TimestampUs; + transaction?: { + id: string; + sampled?: boolean; + type: string; + }; + log: { + message?: string; + }; +} diff --git a/x-pack/plugins/apm/typings/es_schemas/ui/event.ts b/x-pack/plugins/apm/typings/es_schemas/ui/event.ts new file mode 100644 index 00000000000000..cfee36de51429c --- /dev/null +++ b/x-pack/plugins/apm/typings/es_schemas/ui/event.ts @@ -0,0 +1,13 @@ +/* + * 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 { SpanRaw } from '../raw/span_raw'; +import { Agent } from './fields/agent'; + +export interface Span extends SpanRaw { + agent: Agent; +} From 2cb6a0d311633b086bcf8ab867f402d73a4f0fb4 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Wed, 11 Oct 2023 16:40:02 +0200 Subject: [PATCH 05/28] Created helper function to get log events --- .../get_request_base.ts | 2 +- .../create_apm_event_client/index.ts | 48 ++++++++++++- .../mobile/get_mobile_launches_by_location.ts | 68 ++++++++++--------- .../mobile/get_mobile_location_stats.ts | 15 ++-- .../apm/typings/es_schemas/raw/event_raw.ts | 14 ++-- .../apm/typings/es_schemas/ui/event.ts | 4 +- .../plugins/apm_data_access/server/index.ts | 1 + .../observability/common/processor_event.ts | 1 + 8 files changed, 105 insertions(+), 48 deletions(-) diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/get_request_base.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/get_request_base.ts index 1c2c3ad296101b..bfc9c2e20eaca0 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/get_request_base.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/get_request_base.ts @@ -10,7 +10,6 @@ import type { APMIndices } from '@kbn/apm-data-access-plugin/server'; import { ProcessorEvent } from '@kbn/observability-plugin/common'; import { uniq } from 'lodash'; import { ApmDataSource } from '../../../../../common/data_source'; -import {} from '../../../../../common/document_type'; import { PROCESSOR_EVENT } from '../../../../../common/es_fields/apm'; import { getConfigForDocumentType, @@ -22,6 +21,7 @@ const processorEventIndexMap = { [ProcessorEvent.span]: 'span', [ProcessorEvent.metric]: 'metric', [ProcessorEvent.error]: 'error', + [ProcessorEvent.event]: 'event', } as const; export function processorEventsToIndex( diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts index a9a5e02b9eae14..533c979abbc723 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts @@ -11,11 +11,12 @@ import type { FieldCapsResponse, MsearchMultisearchBody, MsearchMultisearchHeader, - TermsEnumResponse, TermsEnumRequest, + TermsEnumResponse, } from '@elastic/elasticsearch/lib/api/types'; import { ElasticsearchClient, KibanaRequest } from '@kbn/core/server'; import type { ESSearchRequest, InferSearchResponseOf } from '@kbn/es-types'; +import { ESFilter } from '@kbn/es-types'; import { ProcessorEvent } from '@kbn/observability-plugin/common'; import { unwrapEsResponse } from '@kbn/observability-plugin/server'; import { compact, omit } from 'lodash'; @@ -26,6 +27,7 @@ import { APMError } from '../../../../../typings/es_schemas/ui/apm_error'; import { Metric } from '../../../../../typings/es_schemas/ui/metric'; import { Span } from '../../../../../typings/es_schemas/ui/span'; import { Transaction } from '../../../../../typings/es_schemas/ui/transaction'; +import { Event } from '../../../../../typings/es_schemas/ui/event'; import { withApmSpan } from '../../../../utils/with_apm_span'; import { callAsyncWithDebug, @@ -61,6 +63,7 @@ type TypeOfProcessorEvent = { transaction: Transaction; span: Span; metric: Metric; + event: Event; }[T]; type TypedSearchResponse = @@ -196,6 +199,49 @@ export class APMEventClient { }); } + async logEventSearch( + operationName: string, + params: TParams + ): Promise> { + const index = processorEventsToIndex([ProcessorEvent.error], this.indices); + + const filters: ESFilter[] = [ + { + terms: { + ['event.kind']: ['event'], + }, + }, + ]; + + const searchParams = { + ...omit(params, 'apm', 'body'), + index, + body: { + ...params.body, + query: { + bool: { + filter: filters, + must: compact([params.body.query]), + }, + }, + }, + ...(this.includeFrozen ? { ignore_throttled: false } : {}), + ignore_unavailable: true, + preference: 'any', + expand_wildcards: ['open' as const, 'hidden' as const], + }; + + return this.callAsyncWithDebug({ + cb: (opts) => + this.esClient.search(searchParams, opts) as unknown as Promise<{ + body: TypedSearchResponse; + }>, + operationName, + params: searchParams, + requestType: 'search', + }); + } + async msearch( operationName: string, ...allParams: TParams[] diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts index 5f324f9b0f698c..fe23ebd867497a 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts @@ -54,7 +54,7 @@ export async function getLaunchesByLocation({ launches: { filter: { term: { ['labels.lifecycle_state']: 'created' } }, aggs: { - launchesByLocation: { + byLocation: { terms: { field: locationField, }, @@ -62,48 +62,52 @@ export async function getLaunchesByLocation({ }, }, }; - const response = await apmEventClient.search('get_mobile_location_launches', { - apm: { - events: [ProcessorEvent.transaction], - }, - body: { - track_total_hits: false, - size: 0, - query: { - bool: { - filter: [ - ...termQuery(SERVICE_NAME, serviceName), - ...rangeQuery(startWithOffset, endWithOffset), - ...environmentQuery(environment), - ...kqlQuery(kuery), - ], - }, + + const response = await apmEventClient.logEventSearch( + 'get_mobile_location_launches', + { + apm: { + events: [ProcessorEvent.event], }, - aggs: { - timeseries: { - date_histogram: { - field: '@timestamp', - fixed_interval: intervalString, - min_doc_count: 0, + body: { + track_total_hits: false, + size: 0, + query: { + bool: { + filter: [ + ...termQuery(SERVICE_NAME, serviceName), + ...rangeQuery(startWithOffset, endWithOffset), + ...environmentQuery(environment), + ...kqlQuery(kuery), + ], }, - aggs, }, - ...aggs, + aggs: { + timeseries: { + date_histogram: { + field: '@timestamp', + fixed_interval: intervalString, + min_doc_count: 0, + }, + aggs, + }, + ...aggs, + }, }, - }, - }); + } + ); + return { - location: response.aggregations?.launches?.launchesByLocation?.buckets[0] + location: response.aggregations?.launches?.byLocation?.buckets[0] ?.key as string, value: - response.aggregations?.launches?.launchesByLocation?.buckets[0] - ?.doc_count ?? 0, + response.aggregations?.launches?.byLocation?.buckets[0]?.doc_count ?? 0, timeseries: response.aggregations?.timeseries?.buckets.map((bucket) => ({ x: bucket.key, y: - response.aggregations?.launches?.launchesByLocation?.buckets[0] - ?.doc_count ?? 0, + response.aggregations?.launches?.byLocation?.buckets[0]?.doc_count ?? + 0, })) ?? [], }; } diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts index eb590d20376569..985c92ebd43eaf 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_location_stats.ts @@ -75,18 +75,19 @@ async function getMobileLocationStats({ offset, }; - const [mostSessions, mostRequests, mostCrashes, mostLaunches] = await Promise.all([ - getSessionsByLocation({ ...commonProps }), - getHttpRequestsByLocation({ ...commonProps }), - getCrashesByLocation({ ...commonProps }), - getLaunchesByLocation({ ...commonProps }), - ]); + const [mostSessions, mostRequests, mostCrashes, mostLaunches] = + await Promise.all([ + getSessionsByLocation({ ...commonProps }), + getHttpRequestsByLocation({ ...commonProps }), + getCrashesByLocation({ ...commonProps }), + getLaunchesByLocation({ ...commonProps }), + ]); return { mostSessions, mostRequests, mostCrashes, - mostLaunches + mostLaunches, }; } diff --git a/x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts b/x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts index 1607ca77253eef..715cd8ca328984 100644 --- a/x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts +++ b/x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts @@ -5,15 +5,15 @@ * 2.0. */ -import {APMBaseDoc} from './apm_base_doc'; -import {TimestampUs} from './fields/timestamp_us'; +import { APMBaseDoc } from './apm_base_doc'; +import { TimestampUs } from './fields/timestamp_us'; export interface Processor { - name: 'log'; - event: 'log'; + name: 'event'; + event: 'event'; } -export interface LogRaw extends APMBaseDoc { +export interface EventRaw extends APMBaseDoc { processor: Processor; timestamp: TimestampUs; transaction?: { @@ -24,4 +24,8 @@ export interface LogRaw extends APMBaseDoc { log: { message?: string; }; + event: { + action: string; + category: string; + }; } diff --git a/x-pack/plugins/apm/typings/es_schemas/ui/event.ts b/x-pack/plugins/apm/typings/es_schemas/ui/event.ts index cfee36de51429c..8d9fccea1c8bf0 100644 --- a/x-pack/plugins/apm/typings/es_schemas/ui/event.ts +++ b/x-pack/plugins/apm/typings/es_schemas/ui/event.ts @@ -5,9 +5,9 @@ * 2.0. */ -import { SpanRaw } from '../raw/span_raw'; +import { EventRaw } from '../raw/event_raw'; import { Agent } from './fields/agent'; -export interface Span extends SpanRaw { +export interface Event extends EventRaw { agent: Agent; } diff --git a/x-pack/plugins/apm_data_access/server/index.ts b/x-pack/plugins/apm_data_access/server/index.ts index 4ef9a479377336..f26f7f166fbc00 100644 --- a/x-pack/plugins/apm_data_access/server/index.ts +++ b/x-pack/plugins/apm_data_access/server/index.ts @@ -15,6 +15,7 @@ const configSchema = schema.object({ span: schema.string({ defaultValue: 'traces-apm*,apm-*' }), error: schema.string({ defaultValue: 'logs-apm*,apm-*' }), metric: schema.string({ defaultValue: 'metrics-apm*,apm-*' }), + event: schema.string({ defaultValue: 'logs-apm*,apm-*' }), onboarding: schema.string({ defaultValue: 'apm-*' }), // Unused: to be deleted sourcemap: schema.string({ defaultValue: 'apm-*' }), // Unused: to be deleted }), diff --git a/x-pack/plugins/observability/common/processor_event.ts b/x-pack/plugins/observability/common/processor_event.ts index 5bbc327e9f1b16..2c0c38cc3d19ea 100644 --- a/x-pack/plugins/observability/common/processor_event.ts +++ b/x-pack/plugins/observability/common/processor_event.ts @@ -10,4 +10,5 @@ export enum ProcessorEvent { error = 'error', metric = 'metric', span = 'span', + event = 'event', } From f2acd013a7f9f5cbb1d2f9e196eb3ffad37bf1c6 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Wed, 11 Oct 2023 16:50:46 +0200 Subject: [PATCH 06/28] Adding comment on reusing errors indices for events --- .../helpers/create_es_client/create_apm_event_client/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts index 533c979abbc723..81557e3dcd0433 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts @@ -203,6 +203,7 @@ export class APMEventClient { operationName: string, params: TParams ): Promise> { + // Reusing indices configured for errors since both events and errors are stored as logs. const index = processorEventsToIndex([ProcessorEvent.error], this.indices); const filters: ESFilter[] = [ From c4da8f42471f0bf33780ba7611474db603a26fd8 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 13 Oct 2023 11:52:03 +0200 Subject: [PATCH 07/28] Adding iOS active state for launches query --- .../server/routes/mobile/get_mobile_launches_by_location.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts index fe23ebd867497a..3cfb8cd4356513 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts @@ -52,7 +52,9 @@ export async function getLaunchesByLocation({ const aggs = { launches: { - filter: { term: { ['labels.lifecycle_state']: 'created' } }, + filter: { + terms: { ['labels.lifecycle_state']: ['created', 'active'] }, + }, aggs: { byLocation: { terms: { From 84fe444f3adb164734f5f1f885aaba15b7a823c5 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:18:52 +0200 Subject: [PATCH 08/28] Adding mobile launch tests --- .../src/lib/apm/event.ts | 30 +++++++++++++++++++ .../src/lib/apm/mobile_device.ts | 7 ++++- .../src/lib/apm/transaction.ts | 29 +++++++++++++++++- .../mobile/mobile_location_stats.spec.ts | 17 +++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts diff --git a/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts b/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts new file mode 100644 index 00000000000000..6096f867e10bb5 --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts @@ -0,0 +1,30 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { ApmFields } from './apm_fields'; +import { Serializable } from '../serializable'; + +export class Event extends Serializable { + constructor(fields: ApmFields) { + super({ + ...fields, + 'event.kind': 'event', + }); + } + + lifecycle(state: string): this { + this.fields['labels.lifecycle_state'] = state; + return this; + } + + override timestamp(timestamp: number) { + const ret = super.timestamp(timestamp); + this.fields['timestamp.us'] = timestamp * 1000; + return ret; + } +} diff --git a/packages/kbn-apm-synthtrace-client/src/lib/apm/mobile_device.ts b/packages/kbn-apm-synthtrace-client/src/lib/apm/mobile_device.ts index 9a904d0d94dbbb..b0ea0aea4663e1 100644 --- a/packages/kbn-apm-synthtrace-client/src/lib/apm/mobile_device.ts +++ b/packages/kbn-apm-synthtrace-client/src/lib/apm/mobile_device.ts @@ -9,7 +9,8 @@ import { Entity } from '../entity'; import { Span } from './span'; import { Transaction } from './transaction'; -import { ApmFields, SpanParams, GeoLocation, ApmApplicationMetricFields } from './apm_fields'; +import { Event } from './event'; +import { ApmApplicationMetricFields, ApmFields, GeoLocation, SpanParams } from './apm_fields'; import { generateLongId } from '../utils/generate_id'; import { Metricset } from './metricset'; import { ApmError } from './apm_error'; @@ -143,6 +144,10 @@ export class MobileDevice extends Entity { return this; } + event(): Event { + return new Event({ ...this.fields }); + } + transaction( ...options: | [{ transactionName: string; frameworkName?: string; frameworkVersion?: string }] diff --git a/packages/kbn-apm-synthtrace-client/src/lib/apm/transaction.ts b/packages/kbn-apm-synthtrace-client/src/lib/apm/transaction.ts index 55883957c6990c..1b15ac17a4a3d1 100644 --- a/packages/kbn-apm-synthtrace-client/src/lib/apm/transaction.ts +++ b/packages/kbn-apm-synthtrace-client/src/lib/apm/transaction.ts @@ -7,6 +7,7 @@ */ import { ApmError } from './apm_error'; +import { Event } from './event'; import { BaseSpan } from './base_span'; import { generateShortId } from '../utils/generate_id'; import { ApmFields } from './apm_fields'; @@ -15,6 +16,7 @@ import { getBreakdownMetrics } from './processors/get_breakdown_metrics'; export class Transaction extends BaseSpan { private _sampled: boolean = true; private readonly _errors: ApmError[] = []; + private readonly _events: Event[] = []; constructor(fields: ApmFields) { super({ @@ -35,6 +37,27 @@ export class Transaction extends BaseSpan { error.fields['transaction.sampled'] = this.fields['transaction.sampled']; }); + this._events.forEach((event) => { + event.fields['trace.id'] = this.fields['trace.id']; + event.fields['transaction.id'] = this.fields['transaction.id']; + event.fields['transaction.type'] = this.fields['transaction.type']; + event.fields['transaction.sampled'] = this.fields['transaction.sampled']; + }); + + return this; + } + + events(...events: Event[]) { + events.forEach((event) => { + event.fields['trace.id'] = this.fields['trace.id']; + event.fields['transaction.id'] = this.fields['transaction.id']; + event.fields['transaction.name'] = this.fields['transaction.name']; + event.fields['transaction.type'] = this.fields['transaction.type']; + event.fields['transaction.sampled'] = this.fields['transaction.sampled']; + }); + + this._events.push(...events); + return this; } @@ -62,6 +85,9 @@ export class Transaction extends BaseSpan { this._errors.forEach((error) => { error.fields['transaction.sampled'] = sampled; }); + this._events.forEach((event) => { + event.fields['transaction.sampled'] = sampled; + }); return this; } @@ -69,6 +95,7 @@ export class Transaction extends BaseSpan { const [transaction, ...spans] = super.serialize(); const errors = this._errors.flatMap((error) => error.serialize()); + const logEvents = this._events.flatMap((event) => event.serialize()); const directChildren = this.getChildren().map((child) => child.fields); @@ -80,6 +107,6 @@ export class Transaction extends BaseSpan { events.push(...spans); } - return events.concat(errors).concat(breakdownMetrics); + return events.concat(errors).concat(breakdownMetrics).concat(logEvents); } } diff --git a/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts b/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts index 94193f2946ece7..c1d96331e81a1c 100644 --- a/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts +++ b/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts @@ -142,6 +142,7 @@ async function generateData({ galaxy10 .transaction('Start View - View Appearing', 'Android Activity') .errors(galaxy10.crash({ message: 'error' }).timestamp(timestamp)) + .events(galaxy10.event().lifecycle('created').timestamp(timestamp)) .timestamp(timestamp) .duration(500) .success() @@ -159,12 +160,14 @@ async function generateData({ galaxy7 .transaction('Start View - View Appearing', 'Android Activity') .errors(galaxy7.crash({ message: 'error' }).timestamp(timestamp)) + .events(galaxy7.event().lifecycle('created').timestamp(timestamp)) .timestamp(timestamp) .duration(20) .success(), huaweiP2 .transaction('Start View - View Appearing', 'huaweiP2 Activity') .errors(huaweiP2.crash({ message: 'error' }).timestamp(timestamp)) + .events(huaweiP2.event().lifecycle('created').timestamp(timestamp)) .timestamp(timestamp) .duration(20) .success(), @@ -222,6 +225,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(response.currentPeriod.mostCrashes.timeseries.every((item) => item.y === 0)).to.eql( true ); + expect(response.currentPeriod.mostLaunches.timeseries.every((item) => item.y === 0)).to.eql( + true + ); }); }); }); @@ -261,6 +267,11 @@ export default function ApiTest({ getService }: FtrProviderContext) { const { location } = response.currentPeriod.mostCrashes; expect(location).to.be('China'); }); + + it('returns location for most launches', () => { + const { location } = response.currentPeriod.mostLaunches; + expect(location).to.be('China'); + }); }); describe('when filters are applied', () => { @@ -274,6 +285,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(response.currentPeriod.mostSessions.value).to.eql(0); expect(response.currentPeriod.mostRequests.value).to.eql(0); expect(response.currentPeriod.mostCrashes.value).to.eql(0); + expect(response.currentPeriod.mostLaunches.value).to.eql(0); expect(response.currentPeriod.mostSessions.timeseries.every((item) => item.y === 0)).to.eql( true @@ -284,6 +296,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(response.currentPeriod.mostCrashes.timeseries.every((item) => item.y === 0)).to.eql( true ); + expect(response.currentPeriod.mostLaunches.timeseries.every((item) => item.y === 0)).to.eql( + true + ); }); it('returns the correct values when single filter is applied', async () => { @@ -296,6 +311,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(response.currentPeriod.mostSessions.value).to.eql(3); expect(response.currentPeriod.mostRequests.value).to.eql(3); expect(response.currentPeriod.mostCrashes.value).to.eql(3); + expect(response.currentPeriod.mostLaunches.value).to.eql(3); }); it('returns the correct values when multiple filters are applied', async () => { @@ -307,6 +323,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(response.currentPeriod.mostSessions.value).to.eql(3); expect(response.currentPeriod.mostRequests.value).to.eql(3); expect(response.currentPeriod.mostCrashes.value).to.eql(3); + expect(response.currentPeriod.mostLaunches.value).to.eql(3); }); }); }); From 700db7a21cafed0ecbd110606ea30c006d6949eb Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:21:45 +0200 Subject: [PATCH 09/28] Updating settins tests to add event index --- .../tests/settings/apm_indices/apm_indices.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts b/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts index 6798d22af1999e..337f880087d9a9 100644 --- a/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts +++ b/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts @@ -47,6 +47,7 @@ export default function apmIndicesTests({ getService }: FtrProviderContext) { transaction: 'traces-apm*,apm-*', span: 'traces-apm*,apm-*', error: 'logs-apm*,apm-*', + event: 'logs-apm*,apm-*', metric: 'metrics-apm*,apm-*', onboarding: 'apm-*', sourcemap: 'apm-*', From 5c8b70b4a98cb5ca519965f90b30e7df62f16ea6 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:31:37 +0200 Subject: [PATCH 10/28] Removing event unused config index --- x-pack/plugins/apm_data_access/server/index.ts | 1 - .../tests/settings/apm_indices/apm_indices.spec.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/x-pack/plugins/apm_data_access/server/index.ts b/x-pack/plugins/apm_data_access/server/index.ts index f26f7f166fbc00..4ef9a479377336 100644 --- a/x-pack/plugins/apm_data_access/server/index.ts +++ b/x-pack/plugins/apm_data_access/server/index.ts @@ -15,7 +15,6 @@ const configSchema = schema.object({ span: schema.string({ defaultValue: 'traces-apm*,apm-*' }), error: schema.string({ defaultValue: 'logs-apm*,apm-*' }), metric: schema.string({ defaultValue: 'metrics-apm*,apm-*' }), - event: schema.string({ defaultValue: 'logs-apm*,apm-*' }), onboarding: schema.string({ defaultValue: 'apm-*' }), // Unused: to be deleted sourcemap: schema.string({ defaultValue: 'apm-*' }), // Unused: to be deleted }), diff --git a/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts b/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts index 337f880087d9a9..6798d22af1999e 100644 --- a/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts +++ b/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts @@ -47,7 +47,6 @@ export default function apmIndicesTests({ getService }: FtrProviderContext) { transaction: 'traces-apm*,apm-*', span: 'traces-apm*,apm-*', error: 'logs-apm*,apm-*', - event: 'logs-apm*,apm-*', metric: 'metrics-apm*,apm-*', onboarding: 'apm-*', sourcemap: 'apm-*', From 75f759e7672e5953de3c52a1481fead90dd5968b Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Tue, 17 Oct 2023 19:12:56 +0200 Subject: [PATCH 11/28] Adding ApmFields --- packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts b/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts index 911271f5841525..8a74e2bca89142 100644 --- a/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts +++ b/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts @@ -38,6 +38,7 @@ export type ApmUserAgentFields = Partial<{ export interface ApmException { message: string; } + export interface Observer { type: string; version: string; @@ -94,6 +95,7 @@ export type ApmFields = Fields<{ 'error.type': string; 'event.ingested': number; 'event.name': string; + 'event.kind': string; 'event.outcome': string; 'event.outcome_numeric': | number @@ -121,6 +123,7 @@ export type ApmFields = Fields<{ 'kubernetes.pod.uid': string; 'labels.name': string; 'labels.telemetry_auto_version': string; + 'labels.lifecycle_state': string; 'metricset.name': string; 'network.carrier.icc': string; 'network.carrier.mcc': string; From 8ed14d6a8879e462a2d477a2701eb37be1fdfa80 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Wed, 18 Oct 2023 12:00:45 +0200 Subject: [PATCH 12/28] Revert "Removing event unused config index" This reverts commit 5c8b70b4a98cb5ca519965f90b30e7df62f16ea6. --- x-pack/plugins/apm_data_access/server/index.ts | 1 + .../tests/settings/apm_indices/apm_indices.spec.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/x-pack/plugins/apm_data_access/server/index.ts b/x-pack/plugins/apm_data_access/server/index.ts index 4ef9a479377336..f26f7f166fbc00 100644 --- a/x-pack/plugins/apm_data_access/server/index.ts +++ b/x-pack/plugins/apm_data_access/server/index.ts @@ -15,6 +15,7 @@ const configSchema = schema.object({ span: schema.string({ defaultValue: 'traces-apm*,apm-*' }), error: schema.string({ defaultValue: 'logs-apm*,apm-*' }), metric: schema.string({ defaultValue: 'metrics-apm*,apm-*' }), + event: schema.string({ defaultValue: 'logs-apm*,apm-*' }), onboarding: schema.string({ defaultValue: 'apm-*' }), // Unused: to be deleted sourcemap: schema.string({ defaultValue: 'apm-*' }), // Unused: to be deleted }), diff --git a/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts b/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts index 6798d22af1999e..337f880087d9a9 100644 --- a/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts +++ b/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts @@ -47,6 +47,7 @@ export default function apmIndicesTests({ getService }: FtrProviderContext) { transaction: 'traces-apm*,apm-*', span: 'traces-apm*,apm-*', error: 'logs-apm*,apm-*', + event: 'logs-apm*,apm-*', metric: 'metrics-apm*,apm-*', onboarding: 'apm-*', sourcemap: 'apm-*', From 9fc934bacdf3903140af9989ba84c5b2c17529e4 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Wed, 18 Oct 2023 12:47:33 +0200 Subject: [PATCH 13/28] Removing processorEvent.event --- .../create_apm_event_client/get_request_base.ts | 1 - .../helpers/create_es_client/create_apm_event_client/index.ts | 2 -- x-pack/plugins/apm_data_access/server/index.ts | 1 - x-pack/plugins/observability/common/processor_event.ts | 1 - .../tests/settings/apm_indices/apm_indices.spec.ts | 1 - 5 files changed, 6 deletions(-) diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/get_request_base.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/get_request_base.ts index bfc9c2e20eaca0..1046a2ad47cfec 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/get_request_base.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/get_request_base.ts @@ -21,7 +21,6 @@ const processorEventIndexMap = { [ProcessorEvent.span]: 'span', [ProcessorEvent.metric]: 'metric', [ProcessorEvent.error]: 'error', - [ProcessorEvent.event]: 'event', } as const; export function processorEventsToIndex( diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts index 81557e3dcd0433..0945b8590e45a5 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts @@ -27,7 +27,6 @@ import { APMError } from '../../../../../typings/es_schemas/ui/apm_error'; import { Metric } from '../../../../../typings/es_schemas/ui/metric'; import { Span } from '../../../../../typings/es_schemas/ui/span'; import { Transaction } from '../../../../../typings/es_schemas/ui/transaction'; -import { Event } from '../../../../../typings/es_schemas/ui/event'; import { withApmSpan } from '../../../../utils/with_apm_span'; import { callAsyncWithDebug, @@ -63,7 +62,6 @@ type TypeOfProcessorEvent = { transaction: Transaction; span: Span; metric: Metric; - event: Event; }[T]; type TypedSearchResponse = diff --git a/x-pack/plugins/apm_data_access/server/index.ts b/x-pack/plugins/apm_data_access/server/index.ts index f26f7f166fbc00..4ef9a479377336 100644 --- a/x-pack/plugins/apm_data_access/server/index.ts +++ b/x-pack/plugins/apm_data_access/server/index.ts @@ -15,7 +15,6 @@ const configSchema = schema.object({ span: schema.string({ defaultValue: 'traces-apm*,apm-*' }), error: schema.string({ defaultValue: 'logs-apm*,apm-*' }), metric: schema.string({ defaultValue: 'metrics-apm*,apm-*' }), - event: schema.string({ defaultValue: 'logs-apm*,apm-*' }), onboarding: schema.string({ defaultValue: 'apm-*' }), // Unused: to be deleted sourcemap: schema.string({ defaultValue: 'apm-*' }), // Unused: to be deleted }), diff --git a/x-pack/plugins/observability/common/processor_event.ts b/x-pack/plugins/observability/common/processor_event.ts index 2c0c38cc3d19ea..5bbc327e9f1b16 100644 --- a/x-pack/plugins/observability/common/processor_event.ts +++ b/x-pack/plugins/observability/common/processor_event.ts @@ -10,5 +10,4 @@ export enum ProcessorEvent { error = 'error', metric = 'metric', span = 'span', - event = 'event', } diff --git a/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts b/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts index 337f880087d9a9..6798d22af1999e 100644 --- a/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts +++ b/x-pack/test/apm_api_integration/tests/settings/apm_indices/apm_indices.spec.ts @@ -47,7 +47,6 @@ export default function apmIndicesTests({ getService }: FtrProviderContext) { transaction: 'traces-apm*,apm-*', span: 'traces-apm*,apm-*', error: 'logs-apm*,apm-*', - event: 'logs-apm*,apm-*', metric: 'metrics-apm*,apm-*', onboarding: 'apm-*', sourcemap: 'apm-*', From 5ac126ef36681e238234b1bd6e53639f30fdba6f Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Wed, 18 Oct 2023 14:33:52 +0200 Subject: [PATCH 14/28] Adding event type response --- .../create_apm_event_client/index.ts | 18 ++++++++++++++---- .../mobile/get_mobile_launches_by_location.ts | 4 ---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts index 0945b8590e45a5..4a06bf6ff3c4ee 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts @@ -47,6 +47,13 @@ export type APMEventESSearchRequest = Omit & { }; }; +export type APMLogEventESSearchRequest = Omit & { + body: { + size: number; + track_total_hits: boolean | number; + }; +}; + type APMEventWrapper = Omit & { apm: { events: ProcessorEvent[] }; }; @@ -64,6 +71,9 @@ type TypeOfProcessorEvent = { metric: Metric; }[T]; +type TypedLogEventSearchResponse = + InferSearchResponseOf; + type TypedSearchResponse = InferSearchResponseOf< TypeOfProcessorEvent< @@ -197,10 +207,10 @@ export class APMEventClient { }); } - async logEventSearch( + async logEventSearch( operationName: string, params: TParams - ): Promise> { + ): Promise> { // Reusing indices configured for errors since both events and errors are stored as logs. const index = processorEventsToIndex([ProcessorEvent.error], this.indices); @@ -213,7 +223,7 @@ export class APMEventClient { ]; const searchParams = { - ...omit(params, 'apm', 'body'), + ...omit(params, 'body'), index, body: { ...params.body, @@ -233,7 +243,7 @@ export class APMEventClient { return this.callAsyncWithDebug({ cb: (opts) => this.esClient.search(searchParams, opts) as unknown as Promise<{ - body: TypedSearchResponse; + body: TypedLogEventSearchResponse; }>, operationName, params: searchParams, diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts index 3cfb8cd4356513..f1bf507a93052a 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts @@ -5,7 +5,6 @@ * 2.0. */ -import { ProcessorEvent } from '@kbn/observability-plugin/common'; import { kqlQuery, rangeQuery, @@ -68,9 +67,6 @@ export async function getLaunchesByLocation({ const response = await apmEventClient.logEventSearch( 'get_mobile_location_launches', { - apm: { - events: [ProcessorEvent.event], - }, body: { track_total_hits: false, size: 0, From 61cbdbf0e4e2008c1d2ab0c9db3ad0e9c7e40b4c Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Wed, 18 Oct 2023 15:34:16 +0200 Subject: [PATCH 15/28] Adding log event type to log event search response --- .../helpers/create_es_client/create_apm_event_client/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts index 4a06bf6ff3c4ee..ab9f07caf5eb07 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts @@ -27,6 +27,7 @@ import { APMError } from '../../../../../typings/es_schemas/ui/apm_error'; import { Metric } from '../../../../../typings/es_schemas/ui/metric'; import { Span } from '../../../../../typings/es_schemas/ui/span'; import { Transaction } from '../../../../../typings/es_schemas/ui/transaction'; +import { Event } from '../../../../../typings/es_schemas/ui/event'; import { withApmSpan } from '../../../../utils/with_apm_span'; import { callAsyncWithDebug, @@ -72,7 +73,7 @@ type TypeOfProcessorEvent = { }[T]; type TypedLogEventSearchResponse = - InferSearchResponseOf; + InferSearchResponseOf; type TypedSearchResponse = InferSearchResponseOf< From 2f249de5b119fe0b5af7e914074bd571f3f767a1 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Wed, 18 Oct 2023 18:41:57 +0200 Subject: [PATCH 16/28] Adding processor.event attr to mock events --- packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts b/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts index 6096f867e10bb5..50353708829a67 100644 --- a/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts +++ b/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts @@ -14,6 +14,12 @@ export class Event extends Serializable { super({ ...fields, 'event.kind': 'event', + + // Real log events don't have the "processor.event" attribute. See: https://github.com/elastic/apm-server/pull/11494 + // This is a hack needed for the tests in "mobile/mobile_location_stats.spec.ts" to pass since the "SynthtraceEsClient.index" function + // inside "packages/kbn-apm-synthtrace/src/lib/shared/base_client.ts" requires this "processor.event" attr to be available, + // otherwise it'd throw this error: "Could not determine operation: _index and _action not defined in document". + 'processor.event': 'error', }); } From 50a546338c7e90fcc8bc211a51491a03bf1bc5d0 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 30 Oct 2023 11:11:55 +0100 Subject: [PATCH 17/28] Removing filter from APMEventClient.logEventSearch --- .../src/lib/apm/apm_fields.ts | 1 - .../kbn-apm-synthtrace-client/src/lib/apm/event.ts | 2 +- .../create_es_client/create_apm_event_client/index.ts | 10 ---------- 3 files changed, 1 insertion(+), 12 deletions(-) diff --git a/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts b/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts index 8a74e2bca89142..ab2f0daf7187bc 100644 --- a/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts +++ b/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts @@ -95,7 +95,6 @@ export type ApmFields = Fields<{ 'error.type': string; 'event.ingested': number; 'event.name': string; - 'event.kind': string; 'event.outcome': string; 'event.outcome_numeric': | number diff --git a/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts b/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts index 50353708829a67..7f416422e69f71 100644 --- a/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts +++ b/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts @@ -13,7 +13,6 @@ export class Event extends Serializable { constructor(fields: ApmFields) { super({ ...fields, - 'event.kind': 'event', // Real log events don't have the "processor.event" attribute. See: https://github.com/elastic/apm-server/pull/11494 // This is a hack needed for the tests in "mobile/mobile_location_stats.spec.ts" to pass since the "SynthtraceEsClient.index" function @@ -24,6 +23,7 @@ export class Event extends Serializable { } lifecycle(state: string): this { + this.fields['event.action'] = 'lifecycle'; this.fields['labels.lifecycle_state'] = state; return this; } diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts index ab9f07caf5eb07..fcac29b1d57d51 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts @@ -16,7 +16,6 @@ import type { } from '@elastic/elasticsearch/lib/api/types'; import { ElasticsearchClient, KibanaRequest } from '@kbn/core/server'; import type { ESSearchRequest, InferSearchResponseOf } from '@kbn/es-types'; -import { ESFilter } from '@kbn/es-types'; import { ProcessorEvent } from '@kbn/observability-plugin/common'; import { unwrapEsResponse } from '@kbn/observability-plugin/server'; import { compact, omit } from 'lodash'; @@ -215,14 +214,6 @@ export class APMEventClient { // Reusing indices configured for errors since both events and errors are stored as logs. const index = processorEventsToIndex([ProcessorEvent.error], this.indices); - const filters: ESFilter[] = [ - { - terms: { - ['event.kind']: ['event'], - }, - }, - ]; - const searchParams = { ...omit(params, 'body'), index, @@ -230,7 +221,6 @@ export class APMEventClient { ...params.body, query: { bool: { - filter: filters, must: compact([params.body.query]), }, }, From 894437fa8381035472072dcdcc93087c533b6897 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 30 Oct 2023 11:14:09 +0100 Subject: [PATCH 18/28] Removing processor from event_raw --- x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts b/x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts index 715cd8ca328984..31a1952cdc03d8 100644 --- a/x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts +++ b/x-pack/plugins/apm/typings/es_schemas/raw/event_raw.ts @@ -8,13 +8,7 @@ import { APMBaseDoc } from './apm_base_doc'; import { TimestampUs } from './fields/timestamp_us'; -export interface Processor { - name: 'event'; - event: 'event'; -} - export interface EventRaw extends APMBaseDoc { - processor: Processor; timestamp: TimestampUs; transaction?: { id: string; From e17aca9f8b37a149f54293cc30d41036f7891428 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 30 Oct 2023 11:24:28 +0100 Subject: [PATCH 19/28] Creating and using constant LABEL_LIFECYCLE_STATE --- x-pack/plugins/apm/common/es_fields/apm.ts | 1 + .../routes/mobile/get_mobile_launches_by_location.ts | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/apm/common/es_fields/apm.ts b/x-pack/plugins/apm/common/es_fields/apm.ts index 8c1c0e3670baf8..a6cef6ce73e11a 100644 --- a/x-pack/plugins/apm/common/es_fields/apm.ts +++ b/x-pack/plugins/apm/common/es_fields/apm.ts @@ -140,6 +140,7 @@ export const LABEL_NAME = 'labels.name'; export const LABEL_GC = 'labels.gc'; export const LABEL_TYPE = 'labels.type'; export const LABEL_TELEMETRY_AUTO_VERSION = 'labels.telemetry_auto_version'; +export const LABEL_LIFECYCLE_STATE = 'labels.lifecycle_state'; export const HOST = 'host'; export const HOST_HOSTNAME = 'host.hostname'; // Do not use. Please use `HOST_NAME` instead. diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts index f1bf507a93052a..274293c4835804 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts @@ -10,7 +10,10 @@ import { rangeQuery, termQuery, } from '@kbn/observability-plugin/server'; -import { SERVICE_NAME } from '../../../common/es_fields/apm'; +import { + LABEL_LIFECYCLE_STATE, + SERVICE_NAME, +} from '../../../common/es_fields/apm'; import { APMEventClient } from '../../lib/helpers/create_es_client/create_apm_event_client'; import { getOffsetInMs } from '../../../common/utils/get_offset_in_ms'; import { getBucketSize } from '../../../common/utils/get_bucket_size'; @@ -52,7 +55,7 @@ export async function getLaunchesByLocation({ const aggs = { launches: { filter: { - terms: { ['labels.lifecycle_state']: ['created', 'active'] }, + terms: { [LABEL_LIFECYCLE_STATE]: ['created', 'active'] }, }, aggs: { byLocation: { From faae62477ad3bbfa92d15ec6ec6a1f6007bdf025 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 30 Oct 2023 11:31:33 +0100 Subject: [PATCH 20/28] Properly handling test index routing in tests --- packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts | 6 ------ .../apm_synthtrace_es_client/get_routing_transform.ts | 5 +++++ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts b/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts index 7f416422e69f71..8efd5968a349e8 100644 --- a/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts +++ b/packages/kbn-apm-synthtrace-client/src/lib/apm/event.ts @@ -13,12 +13,6 @@ export class Event extends Serializable { constructor(fields: ApmFields) { super({ ...fields, - - // Real log events don't have the "processor.event" attribute. See: https://github.com/elastic/apm-server/pull/11494 - // This is a hack needed for the tests in "mobile/mobile_location_stats.spec.ts" to pass since the "SynthtraceEsClient.index" function - // inside "packages/kbn-apm-synthtrace/src/lib/shared/base_client.ts" requires this "processor.event" attr to be available, - // otherwise it'd throw this error: "Could not determine operation: _index and _action not defined in document". - 'processor.event': 'error', }); } diff --git a/packages/kbn-apm-synthtrace/src/lib/apm/client/apm_synthtrace_es_client/get_routing_transform.ts b/packages/kbn-apm-synthtrace/src/lib/apm/client/apm_synthtrace_es_client/get_routing_transform.ts index a5428b1a084e90..4ca2498c610b1f 100644 --- a/packages/kbn-apm-synthtrace/src/lib/apm/client/apm_synthtrace_es_client/get_routing_transform.ts +++ b/packages/kbn-apm-synthtrace/src/lib/apm/client/apm_synthtrace_es_client/get_routing_transform.ts @@ -46,6 +46,11 @@ export function getRoutingTransform() { index = `metrics-apm.internal-${namespace}`; } break; + default: + if (document['event.action'] != null) { + index = `logs-apm.app-${namespace}`; + } + break; } if (!index) { From d1bbcfbc6ec860b25de12d0fe510ccdb13b8ac95 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 30 Oct 2023 12:02:36 +0100 Subject: [PATCH 21/28] Adding event.action to apm fields --- packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts b/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts index ab2f0daf7187bc..7c3ab2c2b1e3a6 100644 --- a/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts +++ b/packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts @@ -95,6 +95,7 @@ export type ApmFields = Fields<{ 'error.type': string; 'event.ingested': number; 'event.name': string; + 'event.action': string; 'event.outcome': string; 'event.outcome_numeric': | number From 1697bbbfaf15ed70d98bccea58e4206d58412a01 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 30 Oct 2023 13:45:36 +0100 Subject: [PATCH 22/28] Updating es fields snapshot --- .../common/es_fields/__snapshots__/es_fields.test.ts.snap | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/x-pack/plugins/apm/common/es_fields/__snapshots__/es_fields.test.ts.snap b/x-pack/plugins/apm/common/es_fields/__snapshots__/es_fields.test.ts.snap index 9386fb27f88d22..0fa795b6964557 100644 --- a/x-pack/plugins/apm/common/es_fields/__snapshots__/es_fields.test.ts.snap +++ b/x-pack/plugins/apm/common/es_fields/__snapshots__/es_fields.test.ts.snap @@ -158,6 +158,8 @@ exports[`Error KUBERNETES_REPLICASET_NAME 1`] = `undefined`; exports[`Error LABEL_GC 1`] = `undefined`; +exports[`Error LABEL_LIFECYCLE_STATE 1`] = `undefined`; + exports[`Error LABEL_NAME 1`] = `undefined`; exports[`Error LABEL_TELEMETRY_AUTO_VERSION 1`] = `undefined`; @@ -485,6 +487,8 @@ exports[`Span KUBERNETES_REPLICASET_NAME 1`] = `undefined`; exports[`Span LABEL_GC 1`] = `undefined`; +exports[`Span LABEL_LIFECYCLE_STATE 1`] = `undefined`; + exports[`Span LABEL_NAME 1`] = `undefined`; exports[`Span LABEL_TELEMETRY_AUTO_VERSION 1`] = `undefined`; @@ -822,6 +826,8 @@ exports[`Transaction KUBERNETES_REPLICASET_NAME 1`] = `undefined`; exports[`Transaction LABEL_GC 1`] = `undefined`; +exports[`Transaction LABEL_LIFECYCLE_STATE 1`] = `undefined`; + exports[`Transaction LABEL_NAME 1`] = `undefined`; exports[`Transaction LABEL_TELEMETRY_AUTO_VERSION 1`] = `undefined`; From 4516b20b4cadc830c3850d9c830fe294c39b77ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar?= <56847527+LikeTheSalad@users.noreply.github.com> Date: Thu, 2 Nov 2023 14:27:36 +0100 Subject: [PATCH 23/28] Update x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts Co-authored-by: Katerina --- .../apm/server/routes/mobile/get_mobile_launches_by_location.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts index 274293c4835804..47fb6bb6fd0383 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts @@ -107,7 +107,7 @@ export async function getLaunchesByLocation({ response.aggregations?.timeseries?.buckets.map((bucket) => ({ x: bucket.key, y: - response.aggregations?.launches?.byLocation?.buckets[0]?.doc_count ?? + bucket.launches?.byLocation?.buckets[0]?.doc_count ?? 0 0, })) ?? [], }; From 7b0195f548c9ef59bb3f949a393e528107c5368e Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 3 Nov 2023 12:07:42 +0100 Subject: [PATCH 24/28] Fixing lambda param usage --- .../server/routes/mobile/get_mobile_launches_by_location.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts index 47fb6bb6fd0383..5668d1befa31ae 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_launches_by_location.ts @@ -106,9 +106,7 @@ export async function getLaunchesByLocation({ timeseries: response.aggregations?.timeseries?.buckets.map((bucket) => ({ x: bucket.key, - y: - bucket.launches?.byLocation?.buckets[0]?.doc_count ?? 0 - 0, + y: bucket.launches?.byLocation?.buckets[0]?.doc_count ?? 0, })) ?? [], }; } From 1f794c9e35ef5620cb8d56146e20bc600ee92194 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 3 Nov 2023 13:13:45 +0100 Subject: [PATCH 25/28] Fixing mobile data by location timeseries and adding tests for it --- .../mobile/get_mobile_crashes_by_location.ts | 6 ++-- .../get_mobile_http_requests_by_location.ts | 8 ++--- .../mobile/mobile_location_stats.spec.ts | 31 +++++++++++++++++-- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_crashes_by_location.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_crashes_by_location.ts index 4117e6a220bd4e..855ae8fb35d051 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_crashes_by_location.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_crashes_by_location.ts @@ -11,7 +11,7 @@ import { rangeQuery, termQuery, } from '@kbn/observability-plugin/server'; -import { SERVICE_NAME, ERROR_TYPE } from '../../../common/es_fields/apm'; +import { ERROR_TYPE, SERVICE_NAME } from '../../../common/es_fields/apm'; import { APMEventClient } from '../../lib/helpers/create_es_client/create_apm_event_client'; import { getOffsetInMs } from '../../../common/utils/get_offset_in_ms'; import { getBucketSize } from '../../../common/utils/get_bucket_size'; @@ -101,9 +101,7 @@ export async function getCrashesByLocation({ timeseries: response.aggregations?.timeseries?.buckets.map((bucket) => ({ x: bucket.key, - y: - response.aggregations?.crashes?.crashesByLocation?.buckets[0] - ?.doc_count ?? 0, + y: bucket?.crashes?.crashesByLocation?.buckets[0]?.doc_count ?? 0, })) ?? [], }; } diff --git a/x-pack/plugins/apm/server/routes/mobile/get_mobile_http_requests_by_location.ts b/x-pack/plugins/apm/server/routes/mobile/get_mobile_http_requests_by_location.ts index 568e370a92adf4..6141376491243d 100644 --- a/x-pack/plugins/apm/server/routes/mobile/get_mobile_http_requests_by_location.ts +++ b/x-pack/plugins/apm/server/routes/mobile/get_mobile_http_requests_by_location.ts @@ -6,15 +6,15 @@ */ import { - termQuery, kqlQuery, rangeQuery, + termQuery, } from '@kbn/observability-plugin/server'; import { ProcessorEvent } from '@kbn/observability-plugin/common'; import { SERVICE_NAME, - SPAN_TYPE, SPAN_SUBTYPE, + SPAN_TYPE, } from '../../../common/es_fields/apm'; import { environmentQuery } from '../../../common/utils/environment_query'; import { APMEventClient } from '../../lib/helpers/create_es_client/create_apm_event_client'; @@ -111,9 +111,7 @@ export async function getHttpRequestsByLocation({ timeseries: response.aggregations?.timeseries?.buckets.map((bucket) => ({ x: bucket.key, - y: - response.aggregations?.requests?.requestsByLocation?.buckets[0] - ?.doc_count ?? 0, + y: bucket?.requests?.requestsByLocation?.buckets[0]?.doc_count ?? 0, })) ?? [], }; } diff --git a/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts b/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts index c1d96331e81a1c..1804affe31209f 100644 --- a/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts +++ b/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts @@ -130,11 +130,14 @@ async function generateData({ carrierMCC: '440', }); - return await synthtraceEsClient.index([ + const timestamps = []; + + await synthtraceEsClient.index([ timerange(start, end) .interval('5m') .rate(1) .generator((timestamp) => { + timestamps.push(timestamp); galaxy10.startNewSession(); galaxy7.startNewSession(); huaweiP2.startNewSession(); @@ -174,6 +177,7 @@ async function generateData({ ]; }), ]); + return { timestamps }; } export default function ApiTest({ getService }: FtrProviderContext) { @@ -233,8 +237,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { }); registry.when('Location stats', { config: 'basic', archives: [] }, () => { + let dataGenerated = {}; before(async () => { - await generateData({ + dataGenerated = await generateData({ synthtraceEsClient, start, end, @@ -308,6 +313,28 @@ export default function ApiTest({ getService }: FtrProviderContext) { kuery: `service.version:"1.1"`, }); + const timestamps = dataGenerated.timestamps; + expect( + response.currentPeriod.mostSessions.timeseries.every((item) => + timestamps.includes(item.x) ? item.y === 1 : item.y === 0 + ) + ).to.eql(true); + expect( + response.currentPeriod.mostRequests.timeseries.every((item) => + timestamps.includes(item.x) ? item.y === 1 : item.y === 0 + ) + ).to.eql(true); + expect( + response.currentPeriod.mostCrashes.timeseries.every((item) => + timestamps.includes(item.x) ? item.y === 1 : item.y === 0 + ) + ).to.eql(true); + expect( + response.currentPeriod.mostLaunches.timeseries.every((item) => + timestamps.includes(item.x) ? item.y === 1 : item.y === 0 + ) + ).to.eql(true); + expect(response.currentPeriod.mostSessions.value).to.eql(3); expect(response.currentPeriod.mostRequests.value).to.eql(3); expect(response.currentPeriod.mostCrashes.value).to.eql(3); From 5cafe2f0e89cdc121c3ffee27f55ae1c2f00c30d Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 3 Nov 2023 13:14:38 +0100 Subject: [PATCH 26/28] Adding mobile location timeseries tests --- .../mobile/mobile_location_stats.spec.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts b/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts index 1804affe31209f..eb4602a800384e 100644 --- a/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts +++ b/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts @@ -347,6 +347,28 @@ export default function ApiTest({ getService }: FtrProviderContext) { kuery: `service.version:"1.1" and service.environment: "production"`, }); + const timestamps = dataGenerated.timestamps; + expect( + response.currentPeriod.mostSessions.timeseries.every((item) => + timestamps.includes(item.x) ? item.y === 1 : item.y === 0 + ) + ).to.eql(true); + expect( + response.currentPeriod.mostRequests.timeseries.every((item) => + timestamps.includes(item.x) ? item.y === 1 : item.y === 0 + ) + ).to.eql(true); + expect( + response.currentPeriod.mostCrashes.timeseries.every((item) => + timestamps.includes(item.x) ? item.y === 1 : item.y === 0 + ) + ).to.eql(true); + expect( + response.currentPeriod.mostLaunches.timeseries.every((item) => + timestamps.includes(item.x) ? item.y === 1 : item.y === 0 + ) + ).to.eql(true); + expect(response.currentPeriod.mostSessions.value).to.eql(3); expect(response.currentPeriod.mostRequests.value).to.eql(3); expect(response.currentPeriod.mostCrashes.value).to.eql(3); From fe3df19c9116dc9ec8fc4ff947bc41ac7bba64c9 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 3 Nov 2023 14:40:34 +0100 Subject: [PATCH 27/28] Addressing type errors for tests --- .../tests/mobile/mobile_location_stats.spec.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts b/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts index eb4602a800384e..a9d11d659a3987 100644 --- a/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts +++ b/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts @@ -19,6 +19,10 @@ type MobileLocationStats = // timerange 15min, interval 5m, rate 1 // generate 3 http spans for galaxy10 device +interface GeneratedData { + timestamps: number[]; +} + async function generateData({ start, end, @@ -27,7 +31,7 @@ async function generateData({ start: number; end: number; synthtraceEsClient: ApmSynthtraceEsClient; -}) { +}): Promise { const galaxy10 = apm .mobileApp({ name: 'synth-android', @@ -130,7 +134,7 @@ async function generateData({ carrierMCC: '440', }); - const timestamps = []; + const timestamps: number[] = []; await synthtraceEsClient.index([ timerange(start, end) @@ -177,7 +181,7 @@ async function generateData({ ]; }), ]); - return { timestamps }; + return { timestamps } as GeneratedData; } export default function ApiTest({ getService }: FtrProviderContext) { @@ -237,9 +241,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { }); registry.when('Location stats', { config: 'basic', archives: [] }, () => { - let dataGenerated = {}; + let generatedData: GeneratedData; before(async () => { - dataGenerated = await generateData({ + generatedData = await generateData({ synthtraceEsClient, start, end, @@ -313,7 +317,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { kuery: `service.version:"1.1"`, }); - const timestamps = dataGenerated.timestamps; + const timestamps = generatedData.timestamps; expect( response.currentPeriod.mostSessions.timeseries.every((item) => timestamps.includes(item.x) ? item.y === 1 : item.y === 0 @@ -347,7 +351,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { kuery: `service.version:"1.1" and service.environment: "production"`, }); - const timestamps = dataGenerated.timestamps; + const timestamps = generatedData.timestamps; expect( response.currentPeriod.mostSessions.timeseries.every((item) => timestamps.includes(item.x) ? item.y === 1 : item.y === 0 From 4716f62a2f076a363bf5aa50dd73efc69bf3247b Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 6 Nov 2023 12:26:19 +0100 Subject: [PATCH 28/28] Enhancing api tests --- .../mobile/mobile_location_stats.spec.ts | 63 +++---------------- 1 file changed, 10 insertions(+), 53 deletions(-) diff --git a/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts b/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts index a9d11d659a3987..eddfb143b2c8b6 100644 --- a/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts +++ b/x-pack/test/apm_api_integration/tests/mobile/mobile_location_stats.spec.ts @@ -19,10 +19,6 @@ type MobileLocationStats = // timerange 15min, interval 5m, rate 1 // generate 3 http spans for galaxy10 device -interface GeneratedData { - timestamps: number[]; -} - async function generateData({ start, end, @@ -31,7 +27,7 @@ async function generateData({ start: number; end: number; synthtraceEsClient: ApmSynthtraceEsClient; -}): Promise { +}) { const galaxy10 = apm .mobileApp({ name: 'synth-android', @@ -134,14 +130,11 @@ async function generateData({ carrierMCC: '440', }); - const timestamps: number[] = []; - await synthtraceEsClient.index([ timerange(start, end) .interval('5m') .rate(1) .generator((timestamp) => { - timestamps.push(timestamp); galaxy10.startNewSession(); galaxy7.startNewSession(); huaweiP2.startNewSession(); @@ -181,7 +174,6 @@ async function generateData({ ]; }), ]); - return { timestamps } as GeneratedData; } export default function ApiTest({ getService }: FtrProviderContext) { @@ -241,9 +233,8 @@ export default function ApiTest({ getService }: FtrProviderContext) { }); registry.when('Location stats', { config: 'basic', archives: [] }, () => { - let generatedData: GeneratedData; before(async () => { - generatedData = await generateData({ + await generateData({ synthtraceEsClient, start, end, @@ -317,27 +308,10 @@ export default function ApiTest({ getService }: FtrProviderContext) { kuery: `service.version:"1.1"`, }); - const timestamps = generatedData.timestamps; - expect( - response.currentPeriod.mostSessions.timeseries.every((item) => - timestamps.includes(item.x) ? item.y === 1 : item.y === 0 - ) - ).to.eql(true); - expect( - response.currentPeriod.mostRequests.timeseries.every((item) => - timestamps.includes(item.x) ? item.y === 1 : item.y === 0 - ) - ).to.eql(true); - expect( - response.currentPeriod.mostCrashes.timeseries.every((item) => - timestamps.includes(item.x) ? item.y === 1 : item.y === 0 - ) - ).to.eql(true); - expect( - response.currentPeriod.mostLaunches.timeseries.every((item) => - timestamps.includes(item.x) ? item.y === 1 : item.y === 0 - ) - ).to.eql(true); + expect(response.currentPeriod.mostSessions.timeseries[0].y).to.eql(1); + expect(response.currentPeriod.mostCrashes.timeseries[0].y).to.eql(1); + expect(response.currentPeriod.mostRequests.timeseries[0].y).to.eql(1); + expect(response.currentPeriod.mostLaunches.timeseries[0].y).to.eql(1); expect(response.currentPeriod.mostSessions.value).to.eql(3); expect(response.currentPeriod.mostRequests.value).to.eql(3); @@ -351,27 +325,10 @@ export default function ApiTest({ getService }: FtrProviderContext) { kuery: `service.version:"1.1" and service.environment: "production"`, }); - const timestamps = generatedData.timestamps; - expect( - response.currentPeriod.mostSessions.timeseries.every((item) => - timestamps.includes(item.x) ? item.y === 1 : item.y === 0 - ) - ).to.eql(true); - expect( - response.currentPeriod.mostRequests.timeseries.every((item) => - timestamps.includes(item.x) ? item.y === 1 : item.y === 0 - ) - ).to.eql(true); - expect( - response.currentPeriod.mostCrashes.timeseries.every((item) => - timestamps.includes(item.x) ? item.y === 1 : item.y === 0 - ) - ).to.eql(true); - expect( - response.currentPeriod.mostLaunches.timeseries.every((item) => - timestamps.includes(item.x) ? item.y === 1 : item.y === 0 - ) - ).to.eql(true); + expect(response.currentPeriod.mostSessions.timeseries[0].y).to.eql(1); + expect(response.currentPeriod.mostCrashes.timeseries[0].y).to.eql(1); + expect(response.currentPeriod.mostRequests.timeseries[0].y).to.eql(1); + expect(response.currentPeriod.mostLaunches.timeseries[0].y).to.eql(1); expect(response.currentPeriod.mostSessions.value).to.eql(3); expect(response.currentPeriod.mostRequests.value).to.eql(3);