From 037496d8ef33f7157bf868b2ce0a428a5fd17d2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Wed, 11 Jun 2025 15:38:12 +0200 Subject: [PATCH 1/2] Suggestion for tests --- .../plugins/analytics_plugin_a/public/plugin.ts | 14 ++++++++++++++ .../analytics/tests/analytics_from_the_browser.ts | 9 +++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/platform/test/analytics/plugins/analytics_plugin_a/public/plugin.ts b/src/platform/test/analytics/plugins/analytics_plugin_a/public/plugin.ts index bc5490b891b14..96a1ebe03acb6 100644 --- a/src/platform/test/analytics/plugins/analytics_plugin_a/public/plugin.ts +++ b/src/platform/test/analytics/plugins/analytics_plugin_a/public/plugin.ts @@ -7,6 +7,7 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ +import { apm, Transaction } from '@elastic/apm-rum'; import { BehaviorSubject, filter, firstValueFrom, ReplaySubject } from 'rxjs'; import { takeWhile, tap, toArray } from 'rxjs'; import type { Plugin, CoreSetup, CoreStart, TelemetryCounter, Event } from '@kbn/core/server'; @@ -45,6 +46,13 @@ export class AnalyticsPluginA implements Plugin { description: 'The lifecycle step in which the plugin is', }, }, + traceId: { + type: 'keyword', + _meta: { + description: 'The trace ID of the APM transaction', + optional: true, + }, + }, }, }); @@ -52,6 +60,7 @@ export class AnalyticsPluginA implements Plugin { reportEvent('test-plugin-lifecycle', { plugin: 'analyticsPluginA', step: 'setup', + traceId: this.getTransactionTraceId(), }); let found = false; window.__analyticsPluginA__ = { @@ -96,9 +105,14 @@ export class AnalyticsPluginA implements Plugin { analytics.reportEvent('test-plugin-lifecycle', { plugin: 'analyticsPluginA', step: 'start', + traceId: this.getTransactionTraceId(), }); } public stop() {} + + private getTransactionTraceId(): string | undefined { + return (apm.getCurrentTransaction() as Transaction & { traceId: string })?.traceId; + } } function isTestPluginLifecycleReportEventAction({ action, meta }: Action): boolean { diff --git a/src/platform/test/analytics/tests/analytics_from_the_browser.ts b/src/platform/test/analytics/tests/analytics_from_the_browser.ts index bb1927aaeb935..7c72e5286548a 100644 --- a/src/platform/test/analytics/tests/analytics_from_the_browser.ts +++ b/src/platform/test/analytics/tests/analytics_from_the_browser.ts @@ -148,19 +148,20 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { // This helps us to also test the helpers const events = await ebtUIHelper.getEvents(2, { eventTypes: ['test-plugin-lifecycle'] }); - events.forEach(({ trace }) => expect(trace).to.have.property('id')); - expect(events.map(({ trace, ...rest }) => rest)).to.eql([ + expect(events).to.eql([ { timestamp: reportTestPluginLifecycleEventsAction!.meta[setupEvent].timestamp, event_type: 'test-plugin-lifecycle', context: initialContext, - properties: { plugin: 'analyticsPluginA', step: 'setup' }, + properties: { plugin: 'analyticsPluginA', step: 'setup', traceId: events[0].trace?.id }, + trace: { id: events[0].properties.traceId }, // Cross-checking with the properties.traceId to validate that they are the same }, { timestamp: reportTestPluginLifecycleEventsAction!.meta[startEvent].timestamp, event_type: 'test-plugin-lifecycle', context: reportEventContext, - properties: { plugin: 'analyticsPluginA', step: 'start' }, + properties: { plugin: 'analyticsPluginA', step: 'start', traceId: events[1].trace?.id }, + trace: { id: events[1].properties.traceId }, // Cross-checking with the properties.traceId to validate that they are the same }, ]); }); From e65f52fd2266b96b1bda045a2c36865c5fcdc08a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Wed, 11 Jun 2025 16:11:57 +0200 Subject: [PATCH 2/2] Add server-side tests --- .../plugins/analytics_plugin_a/server/plugin.ts | 10 ++++++++++ .../test/analytics/tests/analytics_from_the_browser.ts | 4 ++-- .../test/analytics/tests/analytics_from_the_server.ts | 6 ++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/platform/test/analytics/plugins/analytics_plugin_a/server/plugin.ts b/src/platform/test/analytics/plugins/analytics_plugin_a/server/plugin.ts index 8309164882ff5..9812c651a5fb5 100644 --- a/src/platform/test/analytics/plugins/analytics_plugin_a/server/plugin.ts +++ b/src/platform/test/analytics/plugins/analytics_plugin_a/server/plugin.ts @@ -7,6 +7,7 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ +import apm from 'elastic-apm-node'; import { BehaviorSubject, filter, firstValueFrom, ReplaySubject } from 'rxjs'; import { takeWhile, tap, toArray } from 'rxjs'; import { schema } from '@kbn/config-schema'; @@ -42,6 +43,13 @@ export class AnalyticsPluginAPlugin implements Plugin { description: 'The lifecycle step in which the plugin is', }, }, + traceId: { + type: 'keyword', + _meta: { + description: 'The trace ID of the APM transaction', + optional: true, + }, + }, }, }); @@ -49,6 +57,7 @@ export class AnalyticsPluginAPlugin implements Plugin { reportEvent('test-plugin-lifecycle', { plugin: 'analyticsPluginA', step: 'setup', + traceId: apm.currentTraceIds?.['trace.id'], }); const actions$ = new ReplaySubject(); @@ -137,6 +146,7 @@ export class AnalyticsPluginAPlugin implements Plugin { analytics.reportEvent('test-plugin-lifecycle', { plugin: 'analyticsPluginA', step: 'start', + traceId: apm.currentTraceIds?.['trace.id'], }); } diff --git a/src/platform/test/analytics/tests/analytics_from_the_browser.ts b/src/platform/test/analytics/tests/analytics_from_the_browser.ts index 7c72e5286548a..74ac77a45a13b 100644 --- a/src/platform/test/analytics/tests/analytics_from_the_browser.ts +++ b/src/platform/test/analytics/tests/analytics_from_the_browser.ts @@ -154,14 +154,14 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { event_type: 'test-plugin-lifecycle', context: initialContext, properties: { plugin: 'analyticsPluginA', step: 'setup', traceId: events[0].trace?.id }, - trace: { id: events[0].properties.traceId }, // Cross-checking with the properties.traceId to validate that they are the same + trace: { id: events[0].properties.traceId }, // Cross-checking with the properties.traceId to validate that the event reporter (plugin) and the client see the same transaction. }, { timestamp: reportTestPluginLifecycleEventsAction!.meta[startEvent].timestamp, event_type: 'test-plugin-lifecycle', context: reportEventContext, properties: { plugin: 'analyticsPluginA', step: 'start', traceId: events[1].trace?.id }, - trace: { id: events[1].properties.traceId }, // Cross-checking with the properties.traceId to validate that they are the same + trace: { id: events[1].properties.traceId }, // Cross-checking with the properties.traceId to validate that the event reporter (plugin) and the client see the same transaction. }, ]); }); diff --git a/src/platform/test/analytics/tests/analytics_from_the_server.ts b/src/platform/test/analytics/tests/analytics_from_the_server.ts index bb1df0735107e..d157ea7554af2 100644 --- a/src/platform/test/analytics/tests/analytics_from_the_server.ts +++ b/src/platform/test/analytics/tests/analytics_from_the_server.ts @@ -151,13 +151,15 @@ export default function ({ getService }: FtrProviderContext) { timestamp: reportTestPluginLifecycleEventsAction!.meta[setupEvent].timestamp, event_type: 'test-plugin-lifecycle', context: initialContext, - properties: { plugin: 'analyticsPluginA', step: 'setup' }, + properties: { plugin: 'analyticsPluginA', step: 'setup', traceId: events[0].trace?.id }, + trace: { id: events[0].properties.traceId }, // Cross-checking with the properties.traceId to validate that the event reporter (plugin) and the client see the same transaction. }, { timestamp: reportTestPluginLifecycleEventsAction!.meta[startEvent].timestamp, event_type: 'test-plugin-lifecycle', context: reportEventContext, - properties: { plugin: 'analyticsPluginA', step: 'start' }, + properties: { plugin: 'analyticsPluginA', step: 'start', traceId: events[1].trace?.id }, + trace: { id: events[1].properties.traceId }, // Cross-checking with the properties.traceId to validate that the event reporter (plugin) and the client see the same transaction. }, ]); });