← Back to Pull requests

feat: record navigations and first draw for swiftui/uikit #1321

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 26, 2026 11:31 UTC
Author

Screen and navigation instrumentation for UIKit and SwiftUI

Author williazz · Reviewers robert-northmind, nachoBonafonte · Status open, awaiting approval · Updated 2026-12-03

This description is the design doc. It compares main at f299ff7c with this PR's head, 06f34357. The API reference that used to sit here moved to one comment; the same text is in Screen/README.md. Watch the recording of the scenarios on a Revyl simulator.

Background

An app moves between screens through container calls: a navigation controller push, a sheet, a tab switch. UIKit and SwiftUI then run the same lifecycle for every screen they show, and the SDK's processors stamp attributes on each span and log record before the exporter sends it.

!Today: the push runs, the SDK hears none of it

1. App code calls a container, which loads the new screen (viewDidLoad, or a SwiftUI body).
2. UIKit or SwiftUI calls viewWillAppear, Core Animation draws the first frame, and viewDidAppear follows.
3. The session, user and vitals processors stamp each span as it starts. SessionSpanProcessor.onStart adds session.id; nothing adds a screen.
4. AppLaunch reads one first frame per process, for app.start only (FirstFrameReader.swift).

The semantic conventions already name the attribute: case screenName = "app.screen.name". Nothing in the SDK sets it.

How peers do it

| SDK | Hook | Current screen | Navigation record | Draw time per screen |
| --- | --- | --- | --- | --- |
| OpenTelemetry Android | Activity and fragment lifecycle callbacks (VisibleScreenTrackerImpl.kt) | app.screen.name on every span (ScreenAttributesSpanProcessor.kt) | app.navigation.complete defined in events.yaml; emission not checked | Lifecycle spans, Created to Resumed (README) |
| Sentry Cocoa | Swizzles loadView and viewDidLoad (helper) | not checked | not checked | ui.load.initial_display (SentrySpanOperation.m) |
| Datadog iOS | UIKit predicate; SwiftUI .trackRUMView() on onAppear (modifier) | One app-level view stack (RUM_FEATURE.md) | One RUM view per screen | not checked |
| Sentry Flutter | RouteObserver push, replace, pop (observer) | not checked | navigation breadcrumbs | not checked |

This PR takes its names and event shape from OpenTelemetry Android and its draw-time idea from Sentry's initial display. Like Datadog, it keeps one stack for the app, not one per window.

Problem statement

The lifecycle runs on every navigation, but no SDK component listens between it and the processors. Every span and log record leaves without a screen, the path through the app is lost, and a slow screen looks the same as a fast one.

!Bottleneck: no listener between the lifecycle and the processors

Sheets make it harder: when a sheet is dismissed, neither UIKit nor SwiftUI tells the presenter it reappeared, so a naive tracker keeps the sheet as the current screen.

A solution must:

1. Stamp the current screen on every span and log record.
2. Record each navigation with the previous screen, its type and its position in the session.
3. Record time to first frame per screen instance, comparable with app.start.
4. Cover UIKit, SwiftUI, apps that mix them, and screens no front end sees.
5. Let each signal turn off without running.
6. Add to the SDK without changing anything that exists.

Out of scope

  • Interaction events. Taps, scrolls and long presses are a separate instrumentation.

  • Time to full display. Sentry's ui.load.full_display needs the app to say when data arrived; this PR stops at the first frame.

  • Per-window screens. All windows share one current screen. iPad multi-window apps get the most recent one.

  • Demo scenarios. The scenario app used for the recording stays out of this PR.

  • Stable semconv. The new names ship at development stability, as app.start did in #1275.
  • Proposal

    Add a ScreenInstrumentation module, ported from AwsOtelSwift's UIKit and SwiftUI instrumentation. Both front ends report into one tracker, which names the current screen, numbers each navigation and times each first draw.

    !Proposed: front ends feed one tracker, which feeds the processors

    1. Front ends report each screen. ScreenInstrumentation.install(configuration:) swizzles UIKit's lifecycle unless uiKit is nil. SwiftUI views opt in with .otelScreen, which swizzles nothing.
    2. One stack serves both. ScreenTracker pushes a screen when it appears and removes it when it disappears; the newest visible entry is the current screen, so dismissing a sheet makes its presenter current again.
    3. Time to first draw. Each instance records one app.screen.time_to_first_draw span from load to the first frame, read with the display-link code app.start uses (recordTimeToFirstDraw).
    4. Navigation event. A change of current screen emits app.navigation.complete with the screen reached, the screen left, app.navigation.type and app.navigation.sequence (NavigationSequence.next). Setting the screen to itself emits nothing.
    5. Processors stamp the screen. ScreenSpanProcessor and ScreenLogRecordProcessor add app.screen.name to every span and log record.

    What changes

    | Part | Before | After |
    | --- | --- | --- |
    | app.screen.name | Defined, never set | On every span and log record |
    | Navigation | Not recorded | app.navigation.complete with previous screen, type and sequence |
    | Screen draw time | Only app.start, once per process | app.screen.time_to_first_draw per screen instance |
    | Manual screens | None | ScreenTrackerProvider.getInstance().setCurrent(screen:), reported as manual |
    | Opt-outs | None | uiKit: nil, recordsNavigation: false, or skip install |

    How UIKit decides a screen

    Not every view controller is a screen. Containers such as navigation and tab controllers only hold screens, so tracksScreen skips them, along with system controllers. An app overrides the decision per class with ScreenTrackingCustomization. UIKit reads the navigation type from the container call that caused the change: push, pop, pop_to, replace, reset, modal, dismiss or switch. The values extend the W3C Navigation API's navigationType.

    How one stack serves both front ends

    UIKit and SwiftUI call the same tracker, so an app that hosts SwiftUI inside UIKit, or the reverse, sees one path. appear and disappear edit the stack, and settle makes the newest visible screen current and reports a change only when it moved. SwiftUI has no container calls to read, so it infers push, pop and pop_to from @State lifetimes and the rest from opt-in modifiers. Sequence numbers are taken on the main queue (NavigationSequence.swift), so a gap in them shows a lost event.

    How time to first draw is measured

    UIKit starts the span at viewDidLoad, or at viewWillAppear for a screen built before it became current, such as a preloaded tab. SwiftUI starts it when the parent builds the ScreenTraceView. Both end at the first frame the display link reports. A screen the user returns to is already drawn and records nothing; a screen sent to the background before its first frame records none either.

    An example event

    {
      "event.name": "app.navigation.complete",
      "app.screen.name": "AuthorView",
      "app.navigation.destination.name": "AuthorView",
      "app.screen.previous.name": "DetailView",
      "app.screen.type": "uikit",
      "app.navigation.type": "push",
      "app.navigation.sequence": 3,
      "session.id": "…"
    }
    

    Measure it

    The goal is that every span and log record carries a screen, and that a navigation path has no gaps. Check it in a backend by counting spans without app.screen.name after the first screen appears, and by looking for missing app.navigation.sequence numbers within a session. No baseline exists, since today every span lacks a screen. Runtime overhead of the swizzles and processors is not measured.

    Ship it

    One PR, opt-in: nothing runs until an app calls install or adds .otelScreen, so it works beside the previous release. Tests on this head:

    | Run | Result |
    | --- | --- |
    | swift test --enable-all-traits --filter ScreenTests, macOS | 99 pass; the UIKit suite compiles out on macOS |
    | xcodebuild test -only-testing:ScreenTests, iPhone simulator | 153 pass |
    | screen-navigation launch on Revyl simulators | Pass on iOS 26.2, 26.5 and 27.0; 5 of 6 runs on iOS 18.5 |

    docs/limits.md holds the full limits and test notes.

    Risks

  • Swizzling conflicts with another SDK that swizzles the same methods. Mitigation: uiKit: nil turns UIKit tracking off and leaves SwiftUI and manual screens working.

  • Wrong screen classification, such as a container counted as a screen. The 53 handler tests cover the rules, and ScreenTrackingCustomization lets an app override them.

  • Lost navigation on iOS 18.5. One run in six lost the SwiftUI pop_to from [List, Detail, Author] to [List]; the cause is not known. It shows up as a sequence gap, not a wrong screen.

  • Partial SwiftUI types. Without the opt-in modifiers, SwiftUI reports a sheet as a push and its dismissal as a pop, and a tab revisit carries no type.

  • Early work, old name. Work started before a screen appears carries the previous screen's name, and a view controller that skips super is invisible.
  • Open decisions

  • Robert's question in the review thread about the other small tracker change still stands; this rewrite does not answer it.

  • Whether the development names should wait for the semconv SIG before merge, or ship as app.start did.
  • Alternate solutions

    Do nothing

    Apps keep naming screens by hand in span attributes, with no shared name or event, and backends cannot compare screens across apps. Rejected: the convention already exists and Android fills it.

    Separate UIKit and SwiftUI modules

    Revision 1 shipped the UIKit and SwiftUI front ends separately. A late SwiftUI reveal could replace a newer UIKit screen, and whether a dismissed sheet revealed its presenter depended on which front end reported it. Rejected after Robert's review: 70ab8f91 consolidated them into one module and one stack.

    Stamp only app.screen.name

    Android core stamps the screen and leaves navigation to instrumentation. That meets requirement 1 and misses 2 and 3: the path and the draw time are the reason to track screens at all.

    Swizzle SwiftUI too

    SwiftUI views are value types, not Objective-C classes, so there is no view method to swizzle. Swizzling UIHostingController would name the host, not the view the user sees. Rejected in favor of the opt-in .otelScreen modifier, which Datadog's .trackRUMView() also uses.

    Glossary

  • Container call: an app call that changes what a container shows, such as pushViewController or present.

  • Current screen: the newest visible entry on the screen stack.

  • First draw: the first frame Core Animation displays for a screen instance, read with a display link.

  • Front end: the UIKit or SwiftUI adapter that reports screens to the tracker.

  • Manual screen: a screen the app sets itself through ScreenTracker.

  • Navigation: a change of the current screen.

  • Screen stack: the tracker's ordered list of visible screens, shared by both front ends.

  • Sequence: app.navigation.sequence, a navigation's position in the session.

  • Swizzling: replacing an Objective-C method at runtime so the SDK runs before the original.
  • written by billy's 🤖

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: nachoBonafonte Review duration: 12738m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #1321"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%