-
Notifications
You must be signed in to change notification settings - Fork 120
feat(browser): Core Web Vitals as browser.web_vital log events #1150
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
Makisuo
merged 4 commits into
feat/browser-sdk-xhr-pageload
from
feat/browser-sdk-web-vitals
Sep 29, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
59c5f2c
feat(browser): Core Web Vitals as browser.web_vital log events
Makisuo b93e0a1
Merge branch 'feat/browser-sdk-xhr-pageload' into feat/browser-sdk-we…
Makisuo e47e51f
fix(browser): report web vitals whether or not a pageload span exists
Makisuo ab3f6b9
Merge branch 'feat/browser-sdk-xhr-pageload' into feat/browser-sdk-we…
Makisuo 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,46 @@ | ||
| // Core Web Vitals as OTel log-based events, following the `browser.web_vital` | ||
| // event in the browser semantic conventions. Aggregates are derived in the | ||
| // warehouse, so each event keeps its page and the pageload trace it belongs to. | ||
| import { scrubUrl } from "@maple/browser-session" | ||
| import type { SpanContext } from "@opentelemetry/api" | ||
| import { type Metric, onCLS, onFCP, onINP, onLCP, onTTFB } from "web-vitals" | ||
| import { emitLog, Severity } from "../logs" | ||
|
|
||
| // web-vitals has no unsubscribe: register once per page, and gate reporting instead. | ||
| let registered = false | ||
| let reporting = false | ||
| let pageload: (() => SpanContext | undefined) | undefined | ||
|
|
||
| function report(metric: Metric): void { | ||
| if (!reporting) return | ||
| emitLog({ | ||
| eventName: "browser.web_vital", | ||
| severityNumber: Severity.INFO, | ||
| severityText: "INFO", | ||
| attributes: { | ||
| "browser.web_vital.name": metric.name.toLowerCase(), | ||
| "browser.web_vital.value": metric.value, | ||
| "browser.web_vital.delta": metric.delta, | ||
| "browser.web_vital.id": metric.id, | ||
| "browser.web_vital.rating": metric.rating, | ||
| "browser.web_vital.navigation_type": metric.navigationType, | ||
| "url.path": scrubUrl(location.pathname), | ||
| }, | ||
| // Only the trace link needs a pageload span; tracing may be off, or the app not navigating yet. | ||
| spanContext: pageload?.(), | ||
| }) | ||
| } | ||
|
|
||
| /** Report vitals, linked to the document's pageload span when there is one. Returns a stop. */ | ||
| export function startWebVitals(getPageload: () => SpanContext | undefined): () => void { | ||
| reporting = true | ||
| pageload = getPageload | ||
| if (!registered) { | ||
| registered = true | ||
| for (const on of [onCLS, onFCP, onINP, onLCP, onTTFB]) on(report) | ||
| } | ||
| return () => { | ||
| reporting = false | ||
| pageload = undefined | ||
| } | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // TEST-SEAM: This focused test replaces process-global modules that have no instance-level injection seam. | ||
| import type { ReadableLogRecord } from "@opentelemetry/sdk-logs" | ||
| import { describe, expect, it, vi } from "vitest" | ||
|
|
||
| const exportedLogs: ReadableLogRecord[] = [] | ||
| vi.mock("@opentelemetry/exporter-logs-otlp-http", () => ({ | ||
| OTLPLogExporter: class { | ||
| export(items: ReadableLogRecord[], callback: (result: { code: number }) => void): void { | ||
| exportedLogs.push(...items) | ||
| callback({ code: 0 }) | ||
| } | ||
| forceFlush(): Promise<void> { | ||
| return Promise.resolve() | ||
| } | ||
| shutdown(): Promise<void> { | ||
| return Promise.resolve() | ||
| } | ||
| }, | ||
| })) | ||
|
|
||
| const { MapleBrowser } = await import("./index") | ||
|
|
||
| // Its own file: web-vitals reports each metric once per page. | ||
| describe("web vitals without tracing", () => { | ||
| it("still reports vitals, just without a trace link", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn(async () => new Response("{}")), | ||
| ) | ||
| const handle = MapleBrowser.init({ | ||
| ingestKey: "k", | ||
| serviceName: "web", | ||
| endpoint: "https://ingest.test", | ||
| replay: { enabled: false }, | ||
| tracing: { enabled: false }, | ||
| }) | ||
| await import("./deferred") | ||
| await new Promise((resolve) => setTimeout(resolve, 50)) | ||
| await handle.shutdown() | ||
| vi.unstubAllGlobals() | ||
|
|
||
| const ttfb = exportedLogs.find( | ||
| (log) => | ||
| log.eventName === "browser.web_vital" && log.attributes["browser.web_vital.name"] === "ttfb", | ||
| ) | ||
| expect(ttfb).toBeDefined() | ||
| expect(ttfb?.spanContext).toBeUndefined() | ||
| }) | ||
| }) |
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,74 @@ | ||
| // TEST-SEAM: This focused test replaces process-global modules that have no instance-level injection seam. | ||
| import type { ReadableLogRecord } from "@opentelemetry/sdk-logs" | ||
| import { afterEach, describe, expect, it, vi } from "vitest" | ||
|
|
||
| const exportedLogs: ReadableLogRecord[] = [] | ||
| vi.mock("@opentelemetry/exporter-logs-otlp-http", () => ({ | ||
| OTLPLogExporter: class { | ||
| export(items: ReadableLogRecord[], callback: (result: { code: number }) => void): void { | ||
| exportedLogs.push(...items) | ||
| callback({ code: 0 }) | ||
| } | ||
| forceFlush(): Promise<void> { | ||
| return Promise.resolve() | ||
| } | ||
| shutdown(): Promise<void> { | ||
| return Promise.resolve() | ||
| } | ||
| }, | ||
| })) | ||
| vi.mock("@opentelemetry/exporter-trace-otlp-http", () => ({ | ||
| OTLPTraceExporter: class { | ||
| export(_spans: unknown[], callback: (result: { code: number }) => void): void { | ||
| callback({ code: 0 }) | ||
| } | ||
| forceFlush(): Promise<void> { | ||
| return Promise.resolve() | ||
| } | ||
| shutdown(): Promise<void> { | ||
| return Promise.resolve() | ||
| } | ||
| }, | ||
| })) | ||
|
|
||
| const { MapleBrowser } = await import("./index") | ||
|
|
||
| let handle: ReturnType<typeof MapleBrowser.init> | undefined | ||
| afterEach(async () => { | ||
| await handle?.shutdown() | ||
| handle = undefined | ||
| }) | ||
|
|
||
| describe("web vitals", () => { | ||
| it("reports browser.web_vital events linked to the pageload span", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn(async () => new Response("{}")), | ||
| ) | ||
| handle = MapleBrowser.init({ | ||
| ingestKey: "k", | ||
| serviceName: "web", | ||
| endpoint: "https://ingest.test", | ||
| replay: { enabled: false }, | ||
| tracing: { instrumentFetch: false }, | ||
| }) | ||
| MapleBrowser.startNavigation("/a") | ||
| MapleBrowser.endNavigation("/a") | ||
| await import("./deferred") | ||
| // TTFB reports once the page has loaded, which the test page long has. | ||
| await new Promise((resolve) => setTimeout(resolve, 50)) | ||
| await handle.shutdown() | ||
| handle = undefined | ||
|
|
||
| const vitals = exportedLogs.filter((log) => log.eventName === "browser.web_vital") | ||
| const ttfb = vitals.find((log) => log.attributes["browser.web_vital.name"] === "ttfb") | ||
| expect(ttfb).toBeDefined() | ||
| expect(typeof ttfb?.attributes["browser.web_vital.value"]).toBe("number") | ||
| expect(["good", "needs-improvement", "poor"]).toContain(ttfb?.attributes["browser.web_vital.rating"]) | ||
| expect(ttfb?.attributes["browser.web_vital.id"]).toMatch(/^v\d+-/) | ||
| expect(ttfb?.attributes["url.path"]).toBe(location.pathname) | ||
| expect(typeof ttfb?.attributes["session.id"]).toBe("string") | ||
| expect(ttfb?.spanContext?.traceId).toMatch(/^[0-9a-f]{32}$/) | ||
| vi.unstubAllGlobals() | ||
| }) | ||
| }) |
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.