-
Couldn't load subscription status.
- Fork 29.7k
Development: Implement request log time details #84906
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,54 @@ | ||
| // Time thresholds in seconds | ||
| const SECONDS_IN_MINUTE = 60 | ||
| const MINUTES_THRESHOLD_SECONDS = 120 // 2 minutes | ||
| const SECONDS_THRESHOLD_HIGH = 40 | ||
| const SECONDS_THRESHOLD_LOW = 2 | ||
| const MILLISECONDS_PER_SECOND = 1000 | ||
|
|
||
| // Time thresholds and conversion factors for nanoseconds | ||
| const NANOSECONDS_PER_SECOND = 1_000_000_000 | ||
| const NANOSECONDS_PER_MILLISECOND = 1_000_000 | ||
| const NANOSECONDS_PER_MICROSECOND = 1_000 | ||
| const NANOSECONDS_IN_MINUTE = 60_000_000_000 // 60 * 1_000_000_000 | ||
| const MINUTES_THRESHOLD_NANOSECONDS = 120_000_000_000 // 2 minutes in nanoseconds | ||
| const SECONDS_THRESHOLD_HIGH_NANOSECONDS = 40_000_000_000 // 40 seconds in nanoseconds | ||
| const SECONDS_THRESHOLD_LOW_NANOSECONDS = 2_000_000_000 // 2 seconds in nanoseconds | ||
| const MILLISECONDS_THRESHOLD_NANOSECONDS = 1_000_000 // 1 millisecond in nanoseconds | ||
|
|
||
| export function durationToString(compilerDuration: number) { | ||
| let durationString | ||
| if (compilerDuration > 120) { | ||
| durationString = `${(compilerDuration / 60).toFixed(1)}min` | ||
| } else if (compilerDuration > 40) { | ||
| durationString = `${compilerDuration.toFixed(0)}s` | ||
| } else if (compilerDuration > 2) { | ||
| durationString = `${compilerDuration.toFixed(1)}s` | ||
| if (compilerDuration > MINUTES_THRESHOLD_SECONDS) { | ||
| return `${(compilerDuration / SECONDS_IN_MINUTE).toFixed(1)}min` | ||
| } else if (compilerDuration > SECONDS_THRESHOLD_HIGH) { | ||
| return `${compilerDuration.toFixed(0)}s` | ||
| } else if (compilerDuration > SECONDS_THRESHOLD_LOW) { | ||
| return `${compilerDuration.toFixed(1)}s` | ||
| } else { | ||
| durationString = `${(compilerDuration * 1000).toFixed(0)}ms` | ||
| return `${(compilerDuration * MILLISECONDS_PER_SECOND).toFixed(1)}ms` | ||
| } | ||
| return durationString | ||
| } | ||
|
|
||
| export function hrtimeToSeconds(hrtime: [number, number]): number { | ||
| // hrtime is a tuple of [seconds, nanoseconds] | ||
| return hrtime[0] + hrtime[1] / 1e9 | ||
| } | ||
|
|
||
| function nanosecondsBigIntToSeconds(nanoseconds: bigint): number { | ||
| return Number(nanoseconds) / 1000000000 | ||
| function durationToStringWithNanoseconds(durationBigInt: bigint): string { | ||
| const duration = Number(durationBigInt) | ||
| if (duration > MINUTES_THRESHOLD_NANOSECONDS) { | ||
| return `${(duration / NANOSECONDS_IN_MINUTE).toFixed(1)}min` | ||
| } else if (duration > SECONDS_THRESHOLD_HIGH_NANOSECONDS) { | ||
| return `${(duration / NANOSECONDS_PER_SECOND).toFixed(0)}s` | ||
| } else if (duration > SECONDS_THRESHOLD_LOW_NANOSECONDS) { | ||
| return `${(duration / NANOSECONDS_PER_SECOND).toFixed(1)}s` | ||
| } else if (duration > MILLISECONDS_THRESHOLD_NANOSECONDS) { | ||
| return `${(duration / NANOSECONDS_PER_MILLISECOND).toFixed(0)}ms` | ||
| } else { | ||
| return `${(duration / NANOSECONDS_PER_MICROSECOND).toFixed(0)}µs` | ||
| } | ||
|
Comment on lines
+32
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could call |
||
| } | ||
|
|
||
| function hrtimeBigIntToSeconds(hrtime: bigint): number { | ||
| return nanosecondsBigIntToSeconds(hrtime) | ||
| export function hrtimeToSeconds(hrtime: [number, number]): number { | ||
| // hrtime is a tuple of [seconds, nanoseconds] | ||
| return hrtime[0] + hrtime[1] / NANOSECONDS_PER_SECOND | ||
| } | ||
|
|
||
| export function hrtimeBigIntDurationToString(hrtime: bigint) { | ||
| return durationToString(hrtimeBigIntToSeconds(hrtime)) | ||
| return durationToStringWithNanoseconds(hrtime) | ||
| } | ||
|
|
||
| export function hrtimeDurationToString(hrtime: [number, number]): string { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { | |
| red, | ||
| white, | ||
| yellow, | ||
| dim, | ||
| } from '../../lib/picocolors' | ||
| import { stripNextRscUnionQuery } from '../../lib/url' | ||
| import type { FetchMetric } from '../base-http' | ||
|
|
@@ -43,14 +44,18 @@ export function logRequests( | |
| response: NodeNextResponse, | ||
| loggingConfig: LoggingConfig, | ||
| requestStartTime: bigint, | ||
| requestEndTime: bigint | ||
| requestEndTime: bigint, | ||
| devRequestTimingMiddlewareStart: bigint | undefined, | ||
| devRequestTimingMiddlewareEnd: bigint | undefined | ||
| ): void { | ||
| if (!ignoreLoggingIncomingRequests(request, loggingConfig)) { | ||
| logIncomingRequests( | ||
| request, | ||
| requestStartTime, | ||
| requestEndTime, | ||
| response.statusCode | ||
| response.statusCode, | ||
| devRequestTimingMiddlewareStart, | ||
| devRequestTimingMiddlewareEnd | ||
| ) | ||
| } | ||
|
|
||
|
|
@@ -65,9 +70,15 @@ function logIncomingRequests( | |
| request: NodeNextRequest, | ||
| requestStartTime: bigint, | ||
| requestEndTime: bigint, | ||
| statusCode: number | ||
| statusCode: number, | ||
| devRequestTimingMiddlewareStart: bigint | undefined, | ||
| devRequestTimingMiddlewareEnd: bigint | undefined | ||
| ): void { | ||
| const isRSC = getRequestMeta(request, 'isRSCRequest') | ||
| const devRequestTimingInternalsEnd = getRequestMeta( | ||
| request, | ||
| 'devRequestTimingInternalsEnd' | ||
| ) | ||
|
Comment on lines
+74
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit inconsistent that |
||
| const url = isRSC ? stripNextRscUnionQuery(request.url) : request.url | ||
|
|
||
| const statusCodeColor = | ||
|
|
@@ -83,8 +94,31 @@ function logIncomingRequests( | |
|
|
||
| const coloredStatus = statusCodeColor(statusCode.toString()) | ||
|
|
||
| const totalRequestTime = requestEndTime - requestStartTime | ||
|
|
||
| const times: Array<[label: string, time: bigint]> = [] | ||
|
|
||
| let middlewareTime: bigint | undefined | ||
| if (devRequestTimingMiddlewareStart && devRequestTimingMiddlewareEnd) { | ||
| middlewareTime = | ||
| devRequestTimingMiddlewareEnd - devRequestTimingMiddlewareStart | ||
| times.push(['proxy.ts', middlewareTime]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We discussed this with @feedthejim but it's a bit confusing until the middleware -> proxy rename is a bit further along. |
||
| } | ||
|
|
||
| if (devRequestTimingInternalsEnd) { | ||
| let frameworkTime = devRequestTimingInternalsEnd - requestStartTime | ||
|
|
||
| /* Middleware runs during the internals so we have to subtract it from the framework time */ | ||
| if (middlewareTime) { | ||
| frameworkTime -= middlewareTime | ||
| } | ||
| // Insert as the first item to be rendered in the list | ||
| times.unshift(['compile', frameworkTime]) | ||
| times.push(['render', requestEndTime - devRequestTimingInternalsEnd]) | ||
| } | ||
|
|
||
| return writeLine( | ||
| `${request.method} ${url} ${coloredStatus} in ${hrtimeBigIntDurationToString(requestEndTime - requestStartTime)}` | ||
| `${request.method} ${url} ${coloredStatus} in ${hrtimeBigIntDurationToString(totalRequestTime)}${times.length > 0 ? dim(` (${times.map(([label, time]) => `${label}: ${hrtimeBigIntDurationToString(time)}`).join(', ')})`) : ''}` | ||
| ) | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs another MILLISECONDS_THRESHOLD_LOW and HIGH and use toFixed(0) and toFixed(1).
We don't want to see
1784.4msthat's a bit too exact.MILLISECONDS_THRESHOLD_HIGH = 0.040MILLISECONDS_THRESHOLD_LOW = 0.002