- Notifications
You must be signed in to change notification settings - Fork34
Fix JSON stringify circular structure when logging#607
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
055bf83
Fix JSON stringify circular structure during serializing
EhabY485b5ae
Use utils.inspect instead of JSON.stringify
EhabY360b5c9
Add tests
EhabY8c56ebe
Fix flakey test
EhabY598ac9d
Reduce computation by size estimating on raw JSON requests
EhabY35bf759
Add changelog entry
EhabY1988056
Final review comments
EhabYFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsCHANGELOG.md
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
80 changes: 78 additions & 2 deletionssrc/api/coderApi.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 |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import { | ||
type AxiosResponseHeaders, | ||
type AxiosInstance, | ||
type AxiosHeaders, | ||
type AxiosResponseTransformer, | ||
} from "axios"; | ||
import { Api } from "coder/site/src/api/api"; | ||
import { | ||
type GetInboxNotificationResponse, | ||
@@ -23,6 +28,7 @@ import { | ||
type RequestConfigWithMeta, | ||
HttpClientLogLevel, | ||
} from "../logging/types"; | ||
import { sizeOf } from "../logging/utils"; | ||
import { WsLogger } from "../logging/wsLogger"; | ||
import { | ||
OneWayWebSocket, | ||
@@ -207,7 +213,24 @@ function addLoggingInterceptors(client: AxiosInstance, logger: Logger) { | ||
(config) => { | ||
const configWithMeta = config as RequestConfigWithMeta; | ||
configWithMeta.metadata = createRequestMeta(); | ||
config.transformRequest = [ | ||
...wrapRequestTransform( | ||
config.transformRequest || client.defaults.transformRequest || [], | ||
configWithMeta, | ||
), | ||
(data) => { | ||
// Log after setting the raw request size | ||
logRequest(logger, configWithMeta, getLogLevel()); | ||
return data; | ||
}, | ||
]; | ||
config.transformResponse = wrapResponseTransform( | ||
config.transformResponse || client.defaults.transformResponse || [], | ||
configWithMeta, | ||
); | ||
return config; | ||
}, | ||
(error: unknown) => { | ||
@@ -228,6 +251,59 @@ function addLoggingInterceptors(client: AxiosInstance, logger: Logger) { | ||
); | ||
} | ||
function wrapRequestTransform( | ||
transformer: AxiosResponseTransformer | AxiosResponseTransformer[], | ||
config: RequestConfigWithMeta, | ||
): AxiosResponseTransformer[] { | ||
return [ | ||
(data: unknown, headers: AxiosHeaders) => { | ||
const transformerArray = Array.isArray(transformer) | ||
? transformer | ||
: [transformer]; | ||
// Transform the request first then get the size (measure what's sent over the wire) | ||
const result = transformerArray.reduce( | ||
EhabY marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
(d, fn) => fn.call(config, d, headers), | ||
data, | ||
); | ||
config.rawRequestSize = getSize(config.headers, result); | ||
return result; | ||
}, | ||
]; | ||
} | ||
function wrapResponseTransform( | ||
transformer: AxiosResponseTransformer | AxiosResponseTransformer[], | ||
config: RequestConfigWithMeta, | ||
): AxiosResponseTransformer[] { | ||
return [ | ||
(data: unknown, headers: AxiosResponseHeaders, status?: number) => { | ||
// Get the size before transforming the response (measure what's sent over the wire) | ||
config.rawResponseSize = getSize(headers, data); | ||
const transformerArray = Array.isArray(transformer) | ||
? transformer | ||
: [transformer]; | ||
return transformerArray.reduce( | ||
(d, fn) => fn.call(config, d, headers, status), | ||
data, | ||
); | ||
}, | ||
]; | ||
} | ||
function getSize(headers: AxiosHeaders, data: unknown): number | undefined { | ||
const contentLength = headers["content-length"]; | ||
if (contentLength !== undefined) { | ||
return parseInt(contentLength, 10); | ||
} | ||
return sizeOf(data); | ||
} | ||
function getLogLevel(): HttpClientLogLevel { | ||
const logLevelStr = vscode.workspace | ||
.getConfiguration() | ||
2 changes: 1 addition & 1 deletionsrc/core/cliManager.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
35 changes: 8 additions & 27 deletionssrc/logging/formatters.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
18 changes: 11 additions & 7 deletionssrc/logging/httpLogger.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
2 changes: 2 additions & 0 deletionssrc/logging/types.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
43 changes: 36 additions & 7 deletionssrc/logging/utils.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 |
---|---|---|
@@ -1,21 +1,37 @@ | ||
import { Buffer } from "node:buffer"; | ||
import crypto from "node:crypto"; | ||
import util from "node:util"; | ||
export function shortId(id: string): string { | ||
return id.slice(0, 8); | ||
} | ||
export function createRequestId(): string { | ||
return crypto.randomUUID().replace(/-/g, ""); | ||
} | ||
/** | ||
* Returns the byte size of the data if it can be determined from the data's intrinsic properties, | ||
* otherwise returns undefined (e.g., for plain objects and arrays that would require serialization). | ||
*/ | ||
export function sizeOf(data: unknown): number | undefined { | ||
if (data === null || data === undefined) { | ||
return 0; | ||
} | ||
if (typeof data === "boolean") { | ||
return 4; | ||
} | ||
if (typeof data === "number") { | ||
return 8; | ||
EhabY marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
if (typeofdata === "string" || typeof data === "bigint") { | ||
returnBuffer.byteLength(data.toString()); | ||
} | ||
if ( | ||
Buffer.isBuffer(data) || | ||
data instanceof ArrayBuffer || | ||
ArrayBuffer.isView(data) | ||
) { | ||
return data.byteLength; | ||
} | ||
if ( | ||
@@ -28,6 +44,19 @@ export function sizeOf(data: unknown): number | undefined { | ||
return undefined; | ||
} | ||
export function safeStringify(data: unknown): string | null { | ||
try { | ||
return util.inspect(data, { | ||
showHidden: false, | ||
depth: Infinity, | ||
maxArrayLength: Infinity, | ||
maxStringLength: Infinity, | ||
breakLength: Infinity, | ||
compact: true, | ||
getters: false, // avoid side-effects | ||
}); | ||
} catch { | ||
// Should rarely happen but just in case | ||
return null; | ||
} | ||
} |
2 changes: 1 addition & 1 deletionsrc/logging/wsLogger.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
12 changes: 12 additions & 0 deletionstest/mocks/testHelpers.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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.