-
Notifications
You must be signed in to change notification settings - Fork 272
feat: add instrumentation-client support #679
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
Open
hyoban
wants to merge
9
commits into
cloudflare:main
Choose a base branch
from
hyoban:3-25-instrumentation-client-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f88347d
feat: add instrumentation-client support
hyoban 063bb89
trigger ci
hyoban 5c5e7e9
fix deps
hyoban 5f20d8e
tweaks
hyoban b6a2cac
tweaks
hyoban ed9a1e0
Merge branch 'cloudflare:main' into 3-25-instrumentation-client-support
hyoban faf073a
tweaks
hyoban 215900e
tweaks
hyoban 0430f06
Merge branch 'cloudflare:main' into 3-25-instrumentation-client-support
hyoban 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export {}; |
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
27 changes: 27 additions & 0 deletions
27
packages/vinext/src/client/instrumentation-client-state.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,27 @@ | ||
| import type { ClientInstrumentationHooks } from "./instrumentation-client.js"; | ||
|
|
||
| let clientInstrumentationHooks: ClientInstrumentationHooks | null = null; | ||
|
|
||
| export function normalizeClientInstrumentationHooks( | ||
| hooks: ClientInstrumentationHooks, | ||
| ): ClientInstrumentationHooks | null { | ||
| return Object.values(hooks).some((value) => typeof value === "function") ? hooks : null; | ||
| } | ||
|
|
||
| export function setClientInstrumentationHooks( | ||
| hooks: ClientInstrumentationHooks | null, | ||
| ): ClientInstrumentationHooks | null { | ||
| clientInstrumentationHooks = hooks; | ||
| return clientInstrumentationHooks; | ||
| } | ||
|
|
||
| export function getClientInstrumentationHooks(): ClientInstrumentationHooks | null { | ||
| return clientInstrumentationHooks; | ||
| } | ||
|
|
||
| export function notifyAppRouterTransitionStart( | ||
| href: string, | ||
| navigationType: "push" | "replace" | "traverse", | ||
| ): void { | ||
| clientInstrumentationHooks?.onRouterTransitionStart?.(href, navigationType); | ||
| } |
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,13 @@ | ||
| import * as instrumentationClientHooks from "private-next-instrumentation-client"; | ||
| import { | ||
| normalizeClientInstrumentationHooks, | ||
| setClientInstrumentationHooks, | ||
| } from "./instrumentation-client-state.js"; | ||
|
|
||
| export interface ClientInstrumentationHooks { | ||
| onRouterTransitionStart?: (href: string, navigationType: "push" | "replace" | "traverse") => void; | ||
| } | ||
|
|
||
| export const clientInstrumentationHooks = setClientInstrumentationHooks( | ||
| normalizeClientInstrumentationHooks(instrumentationClientHooks as ClientInstrumentationHooks), | ||
| ); |
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,47 @@ | ||
| import type { Plugin } from "vite"; | ||
| import { normalizePath, parseAst } from "vite"; | ||
| import MagicString from "magic-string"; | ||
|
|
||
| export function createInstrumentationClientTransformPlugin( | ||
| getInstrumentationClientPath: () => string | null, | ||
| ): Plugin { | ||
| return { | ||
| name: "vinext:instrumentation-client", | ||
| apply: "serve", | ||
| transform(code, id) { | ||
| const instrumentationClientPath = getInstrumentationClientPath(); | ||
| if (!instrumentationClientPath) return null; | ||
|
|
||
| const normalizedId = normalizePath(id.split("?", 1)[0]); | ||
| if (normalizedId !== normalizePath(instrumentationClientPath)) return null; | ||
| if (code.includes("__vinextInstrumentationClientDuration")) return null; | ||
|
|
||
| const ast = parseAst(code); | ||
| let insertPos = 0; | ||
hyoban marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // When the module has no imports, inject the timer at the top so the | ||
| // measurement still wraps the full module body execution. | ||
| for (const node of ast.body) { | ||
| if (node.type === "ImportDeclaration") { | ||
| insertPos = node.end; | ||
| } | ||
| } | ||
|
|
||
| const s = new MagicString(code); | ||
| s.appendLeft(insertPos, "\nconst __vinextInstrumentationClientStart = performance.now();\n"); | ||
| s.append( | ||
| "\nconst __vinextInstrumentationClientEnd = performance.now();\n" + | ||
| "const __vinextInstrumentationClientDuration = __vinextInstrumentationClientEnd - __vinextInstrumentationClientStart;\n" + | ||
| "// Match Next.js: only report slow client instrumentation during dev.\n" + | ||
| "// Production should execute the hook without additional timing overhead.\n" + | ||
| "if (__vinextInstrumentationClientDuration > 16) {\n" + | ||
hyoban marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| " console.log(`[Client Instrumentation Hook] Slow execution detected: ${__vinextInstrumentationClientDuration.toFixed(0)}ms (Note: Code download overhead is not included in this measurement)`);\n" + | ||
| "}\n", | ||
| ); | ||
|
|
||
| return { | ||
| code: s.toString(), | ||
| map: s.generateMap({ hires: true }), | ||
| }; | ||
| }, | ||
| }; | ||
| } | ||
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,6 @@ | ||
| declare module "private-next-instrumentation-client" { | ||
| export function onRouterTransitionStart( | ||
| href: string, | ||
| navigationType: "push" | "replace" | "traverse", | ||
| ): void; | ||
| } |
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
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.