[9.0] Add trace.id to EBT events (#222366) (#223517)

# Backport

This will backport the following commits from `main` to `9.0`:
- [Add trace.id to EBT events
(#222366)](https://github.com/elastic/kibana/pull/222366)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Maryam
Saeidi","email":"maryam.saeidi@elastic.co"},"sourceCommit":{"committedDate":"2025-06-11T20:11:41Z","message":"Add
trace.id to EBT events (#222366)\n\nCloses
https://github.com/elastic/observability-dev/issues/4445\n\nEBT
implementation: https://github.com/elastic/ebt/pull/57\n\n##
Summary\n\nIn this PR, we are adding `getTraceContext` to the
`createAnalytics` in\norder to include `trace.id` in the EBT
events.\n\n\n![image](https://github.com/user-attachments/assets/6aa19400-0e38-477a-b5de-74defea7652a)\n\n###
⚠️ Note\n\nSome events might not have a `trace.id` due to not having an
active\ntransaction at the moment of reporting the event. For those, we
can\nimplement more sophisticated logic to keep track of transactions
and use\nthe `trace.id` of the last one for the event (in a follow-up
ticket, if\nnecessary).\n\n### How to test\n- Add
`telemetry.localShipper: true` to the kibana config and create a\ndata
view for `ebt-kibana-browser` index\n- Check the `trace.id` in the
events that are passed\n\n---------\n\nCo-authored-by: Alejandro
Fernández Haro
<alejandro.haro@elastic.co>","sha":"cfc7f0ed0e84e486b9757756334f61153bae2037","branchLabelMapping":{"^v9.1.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:version","v9.1.0","v8.19.0","author:obs-ux-management","v9.0.3"],"title":"Add
trace.id to EBT
events","number":222366,"url":"https://github.com/elastic/kibana/pull/222366","mergeCommit":{"message":"Add
trace.id to EBT events (#222366)\n\nCloses
https://github.com/elastic/observability-dev/issues/4445\n\nEBT
implementation: https://github.com/elastic/ebt/pull/57\n\n##
Summary\n\nIn this PR, we are adding `getTraceContext` to the
`createAnalytics` in\norder to include `trace.id` in the EBT
events.\n\n\n![image](https://github.com/user-attachments/assets/6aa19400-0e38-477a-b5de-74defea7652a)\n\n###
⚠️ Note\n\nSome events might not have a `trace.id` due to not having an
active\ntransaction at the moment of reporting the event. For those, we
can\nimplement more sophisticated logic to keep track of transactions
and use\nthe `trace.id` of the last one for the event (in a follow-up
ticket, if\nnecessary).\n\n### How to test\n- Add
`telemetry.localShipper: true` to the kibana config and create a\ndata
view for `ebt-kibana-browser` index\n- Check the `trace.id` in the
events that are passed\n\n---------\n\nCo-authored-by: Alejandro
Fernández Haro
<alejandro.haro@elastic.co>","sha":"cfc7f0ed0e84e486b9757756334f61153bae2037"}},"sourceBranch":"main","suggestedTargetBranches":["9.0"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/222366","number":222366,"mergeCommit":{"message":"Add
trace.id to EBT events (#222366)\n\nCloses
https://github.com/elastic/observability-dev/issues/4445\n\nEBT
implementation: https://github.com/elastic/ebt/pull/57\n\n##
Summary\n\nIn this PR, we are adding `getTraceContext` to the
`createAnalytics` in\norder to include `trace.id` in the EBT
events.\n\n\n![image](https://github.com/user-attachments/assets/6aa19400-0e38-477a-b5de-74defea7652a)\n\n###
⚠️ Note\n\nSome events might not have a `trace.id` due to not having an
active\ntransaction at the moment of reporting the event. For those, we
can\nimplement more sophisticated logic to keep track of transactions
and use\nthe `trace.id` of the last one for the event (in a follow-up
ticket, if\nnecessary).\n\n### How to test\n- Add
`telemetry.localShipper: true` to the kibana config and create a\ndata
view for `ebt-kibana-browser` index\n- Check the `trace.id` in the
events that are passed\n\n---------\n\nCo-authored-by: Alejandro
Fernández Haro
<alejandro.haro@elastic.co>","sha":"cfc7f0ed0e84e486b9757756334f61153bae2037"}},{"branch":"8.19","label":"v8.19.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/223464","number":223464,"state":"MERGED","mergeCommit":{"sha":"71b79af9b7c44566118e65b2e6d9341c5c39898d","message":"[8.19]
Add trace.id to EBT events (#222366) (#223464)\n\n# Backport\n\nThis
will backport the following commits from `main` to `8.19`:\n- [Add
trace.id to EBT
events\n(#222366)](https://github.com/elastic/kibana/pull/222366)\n\n\n\n###
Questions ?\nPlease refer to the [Backport
tool\ndocumentation](https://github.com/sorenlouv/backport)\n\n\n\nCo-authored-by:
Maryam Saeidi <maryam.saeidi@elastic.co>\nCo-authored-by: Alejandro
Fernández Haro
<alejandro.haro@elastic.co>"}},{"branch":"9.0","label":"v9.0.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Maryam Saeidi <maryam.saeidi@elastic.co>
Co-authored-by: Alejandro Fernández Haro <alejandro.haro@elastic.co>
This commit is contained in:
Kibana Machine 2025-06-12 13:46:11 +02:00 committed by GitHub
parent 01bb8fc018
commit 5419984424
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 44 additions and 6 deletions

View file

@ -8,6 +8,7 @@
*/
import { of, Subscription } from 'rxjs';
import { apm, type Transaction } from '@elastic/apm-rum';
import type { AnalyticsClient } from '@elastic/ebt/client';
import { createAnalytics } from '@elastic/ebt/client';
import { registerPerformanceMetricEventType } from '@kbn/ebt-tools';
@ -25,6 +26,10 @@ export interface AnalyticsServiceSetupDeps {
injectedMetadata: InternalInjectedMetadataSetup;
}
interface TransactionWithUndocumentedProps extends Transaction {
traceId: string;
}
export class AnalyticsService {
private readonly analyticsClient: AnalyticsClient;
private readonly subscriptionsHandler = new Subscription();
@ -33,6 +38,9 @@ export class AnalyticsService {
this.analyticsClient = createAnalytics({
isDev: core.env.mode.dev,
logger: core.logger.get('analytics'),
getTraceContext: () => ({
id: (apm.getCurrentTransaction() as TransactionWithUndocumentedProps | undefined)?.traceId,
}),
});
this.registerBuildInfoAnalyticsContext(core);

View file

@ -8,6 +8,7 @@
*/
import { of } from 'rxjs';
import apm from 'elastic-apm-node';
import type { AnalyticsClient } from '@elastic/ebt/client';
import { createAnalytics } from '@elastic/ebt/client';
import { registerPerformanceMetricEventType } from '@kbn/ebt-tools';
@ -25,6 +26,9 @@ export class AnalyticsService {
this.analyticsClient = createAnalytics({
isDev: core.env.mode.dev,
logger: core.logger.get('analytics'),
getTraceContext: () => ({
id: apm.currentTraceIds?.['trace.id'],
}),
});
this.registerBuildInfoAnalyticsContext(core);

View file

@ -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 {

View file

@ -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<Action>();
@ -125,6 +134,7 @@ export class AnalyticsPluginAPlugin implements Plugin {
analytics.reportEvent('test-plugin-lifecycle', {
plugin: 'analyticsPluginA',
step: 'start',
traceId: apm.currentTraceIds?.['trace.id'],
});
}

View file

@ -153,15 +153,15 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
timestamp: reportTestPluginLifecycleEventsAction!.meta[setupEvent].timestamp,
event_type: 'test-plugin-lifecycle',
context: initialContext,
properties: { plugin: 'analyticsPluginA', step: 'setup' },
trace: null,
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' },
trace: null,
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.
},
]);
});

View file

@ -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.
},
]);
});