-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Debug tracing: Include navigation through hydration timing (#54078)
This: - Adds a simple, generic tracer in the client that can be subscribed to in order to report spans - Reports client spans through the HMR socket in the dev server - Receives these spans and includes them in `.next/trace` Closes WEB-1387
- Loading branch information
1 parent
6767454
commit bd5b715
Showing
5 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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,18 @@ | ||
import { sendMessage } from '../dev/error-overlay/websocket' | ||
import { Span } from './tracer' | ||
|
||
export default function reportToSocket(span: Span) { | ||
if (span.state.state !== 'ended') { | ||
throw new Error('Expected span to be ended') | ||
} | ||
|
||
sendMessage( | ||
JSON.stringify({ | ||
event: 'span-end', | ||
startTime: span.startTime, | ||
endTime: span.state.endTime, | ||
spanName: span.name, | ||
attributes: span.attributes, | ||
}) | ||
) | ||
} |
This file contains 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,77 @@ | ||
import mitt, { MittEmitter } from '../../shared/lib/mitt' | ||
|
||
export type SpanOptions = { | ||
startTime?: number | ||
attributes?: Record<string, unknown> | ||
} | ||
|
||
export type SpanState = | ||
| { | ||
state: 'inprogress' | ||
} | ||
| { | ||
state: 'ended' | ||
endTime: number | ||
} | ||
|
||
interface ISpan { | ||
name: string | ||
startTime: number | ||
attributes: Record<string, unknown> | ||
state: SpanState | ||
end(endTime?: number): void | ||
} | ||
|
||
class Span implements ISpan { | ||
name: string | ||
startTime: number | ||
onSpanEnd: (span: Span) => void | ||
state: SpanState | ||
attributes: Record<string, unknown> | ||
|
||
constructor( | ||
name: string, | ||
options: SpanOptions, | ||
onSpanEnd: (span: Span) => void | ||
) { | ||
this.name = name | ||
this.attributes = options.attributes ?? {} | ||
this.startTime = options.startTime ?? Date.now() | ||
this.onSpanEnd = onSpanEnd | ||
this.state = { state: 'inprogress' } | ||
} | ||
|
||
end(endTime?: number) { | ||
if (this.state.state === 'ended') { | ||
throw new Error('Span has already ended') | ||
} | ||
|
||
this.state = { | ||
state: 'ended', | ||
endTime: endTime ?? Date.now(), | ||
} | ||
this.onSpanEnd(this) | ||
} | ||
} | ||
|
||
class Tracer { | ||
_emitter: MittEmitter<string> = mitt() | ||
|
||
private handleSpanEnd = (span: Span) => { | ||
this._emitter.emit('spanend', span) | ||
} | ||
|
||
startSpan(name: string, options: SpanOptions) { | ||
return new Span(name, options, this.handleSpanEnd) | ||
} | ||
|
||
onSpanEnd(cb: (span: ISpan) => void): () => void { | ||
this._emitter.on('spanend', cb) | ||
return () => { | ||
this._emitter.off('spanend', cb) | ||
} | ||
} | ||
} | ||
|
||
export { ISpan as Span } | ||
export default new Tracer() |
This file contains 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 comment has been minimized.
Sorry, something went wrong.
hellojserAug 22, 2023
This comment has been minimized.
Sorry, something went wrong.
hellojserAug 22, 2023
safari 13