-
-
Notifications
You must be signed in to change notification settings - Fork 361
feat(core): Attach TurboModule breakdown to active spans on spanEnd #6478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5d90835
feat(core): Attach TurboModule breakdown to active spans on spanEnd
alwx 8feb4c5
fix(core): Address review comments on TurboModule span attribution
alwx 352a6f2
fix(core): Don't credit a later span for a call that started before it
alwx a6da727
fix(core): Clear stale span attributes and cap pending call windows
alwx 2496c70
fix(core): Decouple slow-call breadcrumbs and skip sync in pending map
alwx 076488c
fix(core): Snapshot windows for every kind at call start
alwx 8fb1f05
fix(core): Merge late span attribution via processEvent, escape dot keys
alwx 49ba31f
chore(core): Trim comment density in TurboModule integration
alwx 643678a
chore(core): Trim comment density and fix safeKeyPart collision
alwx 0278518
chore(core): Restore doc comments trimmed by previous commit
alwx 6b8c23a
Merge branch 'main' into alwx/feature/6165
alwx d55bd0e
Merge branch 'main' into alwx/feature/6165
alwx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
579 changes: 315 additions & 264 deletions
579
packages/core/src/js/integrations/turboModuleContext.ts
Large diffs are not rendered by default.
Oops, something went wrong.
184 changes: 184 additions & 0 deletions
184
packages/core/src/js/integrations/turboModuleContextFlush.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import type { Client, TransactionEvent } from '@sentry/core'; | ||
|
|
||
| import { debug } from '@sentry/core'; | ||
|
|
||
| import { createSpanJSON } from '../tracing/utils'; | ||
| import { | ||
| drainTurboModuleAggregate, | ||
| HISTOGRAM_BUCKET_LABELS, | ||
| hasTurboModuleAggregateData, | ||
| type TurboModuleAggregate, | ||
| } from '../turbomodule'; | ||
|
|
||
| export const TURBO_MODULES_AGGREGATE_OP = 'turbo_modules.aggregate'; | ||
| export const TURBO_MODULES_AGGREGATE_ORIGIN = 'auto.tracing.turbo_modules'; | ||
|
|
||
| const MAX_AGGREGATE_ATTRIBUTE_ROWS = 64; | ||
|
|
||
| /** | ||
| * Drains the aggregate into the transaction event as a synthetic child span | ||
| * plus headline measurements. Runs before `beforeSendTransaction`, so a | ||
| * user-dropped transaction loses its interval — bounded and self-healing. | ||
| */ | ||
| export function attachAggregateToTransactionEvent(event: TransactionEvent): void { | ||
| const trace = event.contexts?.trace; | ||
| if (!trace?.trace_id || !trace.span_id) { | ||
| return; | ||
| } | ||
| const startTs = event.start_timestamp; | ||
| const endTs = event.timestamp; | ||
| if (typeof startTs !== 'number' || typeof endTs !== 'number') { | ||
| return; | ||
| } | ||
|
|
||
| const snapshot = drainTurboModuleAggregate(); | ||
| if (snapshot.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const totals = summarise(snapshot); | ||
| const topByTotalMs = [...snapshot].sort((a, b) => b.totalDurationMs - a.totalDurationMs); | ||
|
|
||
| const aggregateSpan = createSpanJSON({ | ||
| op: TURBO_MODULES_AGGREGATE_OP, | ||
| description: 'TurboModule call aggregate', | ||
| start_timestamp: startTs, | ||
| timestamp: endTs, | ||
| trace_id: trace.trace_id, | ||
| parent_span_id: trace.span_id, | ||
| origin: TURBO_MODULES_AGGREGATE_ORIGIN, | ||
| data: { | ||
| 'turbo_modules.total_call_count': totals.callCount, | ||
| 'turbo_modules.total_error_count': totals.errorCount, | ||
| 'turbo_modules.total_duration_ms': roundMs(totals.totalDurationMs), | ||
| 'turbo_modules.unique_methods': snapshot.length, | ||
| ...serialiseRows(topByTotalMs.slice(0, MAX_AGGREGATE_ATTRIBUTE_ROWS)), | ||
| }, | ||
| }); | ||
|
|
||
| event.spans = event.spans ?? []; | ||
| event.spans.push(aggregateSpan); | ||
|
|
||
| event.measurements = event.measurements ?? {}; | ||
| event.measurements['turbo_modules.call_count'] = { value: totals.callCount, unit: 'none' }; | ||
| event.measurements['turbo_modules.error_count'] = { value: totals.errorCount, unit: 'none' }; | ||
| event.measurements['turbo_modules.total_ms'] = { value: roundMs(totals.totalDurationMs), unit: 'millisecond' }; | ||
|
|
||
| const top = topByTotalMs[0]; | ||
| if (top) { | ||
| event.measurements['turbo_modules.top_module_ms'] = { | ||
| value: roundMs(top.totalDurationMs), | ||
| unit: 'millisecond', | ||
| }; | ||
| } | ||
|
|
||
| if (snapshot.length > MAX_AGGREGATE_ATTRIBUTE_ROWS) { | ||
| debug.log( | ||
| `[TurboModuleContext] Aggregate has ${snapshot.length} rows, truncated to top ${MAX_AGGREGATE_ATTRIBUTE_ROWS} ` + | ||
| `by total_ms on the aggregate span. Headline measurements still reflect the full totals.`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** Custom Sentry event so long-running sessions without a transaction still emit a signal. */ | ||
| export function flushPeriodicAggregate(client: Client): void { | ||
| if (!hasTurboModuleAggregateData()) { | ||
| return; | ||
| } | ||
| const snapshot = drainTurboModuleAggregate(); | ||
| const totals = summarise(snapshot); | ||
| const topByTotalMs = [...snapshot].sort((a, b) => b.totalDurationMs - a.totalDurationMs); | ||
|
|
||
| client.captureEvent?.({ | ||
| message: 'TurboModule aggregate (periodic)', | ||
| level: 'info', | ||
| tags: { | ||
| 'event.kind': 'turbo_modules.aggregate', | ||
| }, | ||
| extra: { | ||
| total_call_count: totals.callCount, | ||
| total_error_count: totals.errorCount, | ||
| total_duration_ms: roundMs(totals.totalDurationMs), | ||
| unique_methods: snapshot.length, | ||
| modules: topByTotalMs.slice(0, MAX_AGGREGATE_ATTRIBUTE_ROWS).map(serialiseRowAsObject), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function summarise(snapshot: ReadonlyArray<TurboModuleAggregate>): { | ||
| callCount: number; | ||
| errorCount: number; | ||
| totalDurationMs: number; | ||
| } { | ||
| let callCount = 0; | ||
| let errorCount = 0; | ||
| let totalDurationMs = 0; | ||
| for (const row of snapshot) { | ||
| callCount += row.callCount; | ||
| errorCount += row.errorCount; | ||
| totalDurationMs += row.totalDurationMs; | ||
| } | ||
| return { callCount, errorCount, totalDurationMs }; | ||
| } | ||
|
|
||
| function serialiseRows(rows: ReadonlyArray<TurboModuleAggregate>): Record<string, number | string> { | ||
| const out: Record<string, number | string> = {}; | ||
| for (const row of rows) { | ||
| const prefix = `turbo_modules.${safeKeyPart(row.name)}.${safeKeyPart(row.method)}.${row.kind}`; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| out[`${prefix}.count`] = row.callCount; | ||
| out[`${prefix}.error_count`] = row.errorCount; | ||
| out[`${prefix}.total_ms`] = roundMs(row.totalDurationMs); | ||
| out[`${prefix}.max_ms`] = roundMs(row.maxDurationMs); | ||
| for (let i = 0; i < row.buckets.length; i++) { | ||
| const label = HISTOGRAM_BUCKET_LABELS[i]; | ||
| const count = row.buckets[i]; | ||
| if (label !== undefined && count !== undefined) { | ||
| out[`${prefix}.${label}`] = count; | ||
| } | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| function serialiseRowAsObject(row: TurboModuleAggregate): { | ||
| name: string; | ||
| method: string; | ||
| kind: string; | ||
| call_count: number; | ||
| error_count: number; | ||
| total_ms: number; | ||
| max_ms: number; | ||
| histogram: Record<string, number>; | ||
| } { | ||
| const histogram: Record<string, number> = {}; | ||
| for (let i = 0; i < row.buckets.length; i++) { | ||
| const label = HISTOGRAM_BUCKET_LABELS[i]; | ||
| const count = row.buckets[i]; | ||
| if (label !== undefined && count !== undefined) { | ||
| histogram[label] = count; | ||
| } | ||
| } | ||
| return { | ||
| name: row.name, | ||
| method: row.method, | ||
| kind: row.kind, | ||
| call_count: row.callCount, | ||
| error_count: row.errorCount, | ||
| total_ms: roundMs(row.totalDurationMs), | ||
| max_ms: roundMs(row.maxDurationMs), | ||
| histogram, | ||
| }; | ||
| } | ||
|
|
||
| export function roundMs(value: number): number { | ||
| return Math.round(value * 100) / 100; | ||
| } | ||
|
|
||
| /** | ||
| * `.` is the attribute-key delimiter — escape it to `_` in name/method so keys | ||
| * don't collide. `_` is pre-escaped to `__` so `(a.b, c)` and `(a_b, c)` still | ||
| * round-trip to distinct keys. | ||
| */ | ||
| function safeKeyPart(s: string): string { | ||
| return s.replace(/_/g, '__').replace(/\./g, '_'); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.