mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-05 00:56:23 +00:00
## Summary
Extend the runtime analytics bridge with a numeric performance metric channel. Hosts subscribe via the existing postMessage transport (one bridge, two channels) and aggregate per-session p50 / p95 for scrub latency, sustained fps, dropped frames, decoder count, composition load time, and media sync drift before forwarding to their observability pipeline.
This is the foundation other perf tooling sits on — the player itself emits the events; player-side aggregation and flush land in a follow-up.
## Why
Step `X-1` of the player perf proposal. Today there is no way for an embedding host to learn that scrub latency spiked, that a composition took 3 s to load, or that the media-sync loop is running 200 ms behind real time. The only signals are anecdotal user reports.
A single shared bridge keeps the runtime → host surface area minimal: hosts that already wire up the analytics channel get perf for free, and hosts that don't aren't paying for it.
## What changed
- New `emitPerformanceMetric(name, value, tags?)` helper in `@hyperframes/core` that forwards a `{ type: "performance-metric", name, value, tags }` envelope through the existing analytics postMessage transport.
- Six initial metric names defined in the proposal:
- `scrub_latency_ms` — wall-clock from `seek()` call to first paint at the new frame.
- `playback_fps` — sustained rAF cadence during play.
- `dropped_frames` — count of >25 ms gaps within a play window.
- `decoder_count` — number of concurrently-decoding video elements.
- `composition_load_ms` — navigation-start to player-ready.
- `media_sync_drift_ms` — drift between expected and actual decoder time.
- Each emit also writes a `performance.mark()` with `{ value, tags }` on `detail`, so the same numbers surface in the DevTools Performance panel's User Timing track for local debugging without instrumenting the host.
- Zero PostHog (or any other analytics SDK) dependency in `core` — the host decides where to forward the events.
## Test plan
- [x] Unit tests cover the envelope shape, the `performance.mark` mirror, and the no-op path when no host has wired up the bridge.
- [x] Manual: verified marks appear in the User Timing track when scrubbing the studio preview.
## Stack
Step `X-1` of the player perf proposal. Foundation for the perf gate (P0-1a/b/c) — the perf scenarios in this stack instrument these same channels for CI measurement.
127 lines
4.2 KiB
TypeScript
127 lines
4.2 KiB
TypeScript
/**
|
|
* Runtime analytics & performance telemetry — vendor-agnostic event emission.
|
|
*
|
|
* The runtime emits structured events via postMessage. The host application
|
|
* decides what to do with them: forward to PostHog, Mixpanel, Amplitude,
|
|
* a custom logger, or nothing at all.
|
|
*
|
|
* For session replay: initialize your analytics SDK (e.g. PostHog) only in
|
|
* the parent app with `recordCrossOriginIframes: true`. No SDK needs to run
|
|
* inside this iframe.
|
|
*
|
|
* ## Host app integration
|
|
*
|
|
* ```javascript
|
|
* window.addEventListener("message", (e) => {
|
|
* if (e.data?.source !== "hf-preview") return;
|
|
*
|
|
* if (e.data.type === "analytics") {
|
|
* // discrete lifecycle events: composition_loaded, played, seeked, etc.
|
|
* posthog.capture(e.data.event, e.data.properties);
|
|
* }
|
|
*
|
|
* if (e.data.type === "perf") {
|
|
* // numeric performance metrics: scrub latency, fps, decoder count, etc.
|
|
* // Aggregate per-session (p50/p95) and forward on flush.
|
|
* myMetrics.observe(e.data.name, e.data.value, e.data.tags);
|
|
* }
|
|
* });
|
|
* ```
|
|
*/
|
|
|
|
export type RuntimeAnalyticsEvent =
|
|
| "composition_loaded"
|
|
| "composition_played"
|
|
| "composition_paused"
|
|
| "composition_seeked"
|
|
| "composition_ended"
|
|
| "element_picked";
|
|
|
|
export type RuntimeAnalyticsProperties = Record<string, string | number | boolean | null>;
|
|
|
|
/**
|
|
* Tags attached to a performance metric — small, low-cardinality identifiers
|
|
* (composition id hash, media count bucket, browser version, etc.). Same shape
|
|
* as analytics properties so hosts can forward both through one pipeline.
|
|
*/
|
|
export type RuntimePerformanceTags = Record<string, string | number | boolean | null>;
|
|
|
|
// Stored reference to the postRuntimeMessage function, set during init.
|
|
// Avoids a circular import between analytics ↔ bridge. Shared by both
|
|
// emitAnalyticsEvent and emitPerformanceMetric — one bridge, two channels.
|
|
let _postMessage: ((payload: unknown) => void) | null = null;
|
|
|
|
/**
|
|
* Wire the analytics + performance bridge to the runtime's postMessage transport.
|
|
* Called once during runtime bootstrap from `init.ts`.
|
|
*/
|
|
export function initRuntimeAnalytics(postMessage: (payload: unknown) => void): void {
|
|
_postMessage = postMessage;
|
|
}
|
|
|
|
/**
|
|
* Emit an analytics event through the bridge.
|
|
* The host app receives it via postMessage and forwards to its analytics provider.
|
|
*/
|
|
export function emitAnalyticsEvent(
|
|
event: RuntimeAnalyticsEvent,
|
|
properties?: RuntimeAnalyticsProperties,
|
|
): void {
|
|
if (!_postMessage) return;
|
|
try {
|
|
_postMessage({
|
|
source: "hf-preview",
|
|
type: "analytics",
|
|
event,
|
|
properties: properties ?? {},
|
|
});
|
|
} catch {
|
|
// Never let analytics failures affect the runtime
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Emit a numeric performance metric through the bridge.
|
|
*
|
|
* Used for player-perf telemetry — scrub latency, sustained fps, dropped
|
|
* frames, decoder count, composition load time, media sync drift. The host
|
|
* aggregates per-session values (p50/p95) and forwards to its observability
|
|
* pipeline on flush.
|
|
*
|
|
* Also writes a `performance.mark()` so the metric shows up under the
|
|
* DevTools Performance panel's "User Timing" track for local debugging,
|
|
* with `value` and `tags` available on the entry's `detail` field.
|
|
*
|
|
* @param name Metric name, e.g. "player_scrub_latency", "player_playback_fps"
|
|
* @param value Numeric value (units are metric-specific: ms for latency, fps for rate, etc.)
|
|
* @param tags Optional low-cardinality tags (composition id, media count bucket, etc.)
|
|
*/
|
|
export function emitPerformanceMetric(
|
|
name: string,
|
|
value: number,
|
|
tags?: RuntimePerformanceTags,
|
|
): void {
|
|
// Local DevTools breadcrumb. Wrapped because performance.mark() can throw on
|
|
// strict CSP, when the document is not yet ready, or when `detail` is non-cloneable.
|
|
try {
|
|
if (typeof performance !== "undefined" && typeof performance.mark === "function") {
|
|
performance.mark(name, { detail: { value, tags: tags ?? {} } });
|
|
}
|
|
} catch {
|
|
// performance API unavailable or rejected — keep going
|
|
}
|
|
|
|
if (!_postMessage) return;
|
|
try {
|
|
_postMessage({
|
|
source: "hf-preview",
|
|
type: "perf",
|
|
name,
|
|
value,
|
|
tags: tags ?? {},
|
|
});
|
|
} catch {
|
|
// Never let telemetry failures affect the runtime
|
|
}
|
|
}
|