You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-26 17:03:12 +03:00
Improve logging of http requests to aid debugging (#3485)
* Simple request logging with status and duration * remove url params from logs * superfluous toString() * Add tests * Apply suggestions from code review * update snapshots * update log format * Apply suggestions from code review Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> * update snapshot --------- Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Co-authored-by: Richard van der Hoff <richard@matrix.org>
This commit is contained in:
@@ -25,6 +25,7 @@ import { ConnectionError, MatrixError } from "./errors";
|
||||
import { HttpApiEvent, HttpApiEventHandlerMap, IHttpOpts, IRequestOpts } from "./interface";
|
||||
import { anySignal, parseErrorResponse, timeoutSignal } from "./utils";
|
||||
import { QueryDict } from "../utils";
|
||||
import { logger } from "../logger";
|
||||
|
||||
type Body = Record<string, any> | BodyInit;
|
||||
|
||||
@@ -225,6 +226,9 @@ export class FetchHttpApi<O extends IHttpOpts> {
|
||||
body?: Body,
|
||||
opts: Pick<IRequestOpts, "headers" | "json" | "localTimeoutMs" | "keepAlive" | "abortSignal"> = {},
|
||||
): Promise<ResponseType<T, O>> {
|
||||
const urlForLogs = this.clearUrlParamsForLogs(url);
|
||||
logger.debug(`FetchHttpApi: --> ${method} ${urlForLogs}`);
|
||||
|
||||
const headers = Object.assign({}, opts.headers || {});
|
||||
const json = opts.json ?? true;
|
||||
// We can't use getPrototypeOf here as objects made in other contexts e.g. over postMessage won't have same ref
|
||||
@@ -260,6 +264,7 @@ export class FetchHttpApi<O extends IHttpOpts> {
|
||||
const { signal, cleanup } = anySignal(signals);
|
||||
|
||||
let res: Response;
|
||||
const start = Date.now();
|
||||
try {
|
||||
res = await this.fetch(url, {
|
||||
signal,
|
||||
@@ -274,7 +279,10 @@ export class FetchHttpApi<O extends IHttpOpts> {
|
||||
credentials: "omit", // we send credentials via headers
|
||||
keepalive: keepAlive,
|
||||
});
|
||||
|
||||
logger.debug(`FetchHttpApi: <-- ${method} ${urlForLogs} [${Date.now() - start}ms ${res.status}]`);
|
||||
} catch (e) {
|
||||
logger.debug(`FetchHttpApi: <-- ${method} ${urlForLogs} [${Date.now() - start}ms ${e}]`);
|
||||
if ((<Error>e).name === "AbortError") {
|
||||
throw e;
|
||||
}
|
||||
@@ -293,6 +301,22 @@ export class FetchHttpApi<O extends IHttpOpts> {
|
||||
return res as ResponseType<T, O>;
|
||||
}
|
||||
|
||||
private clearUrlParamsForLogs(url: URL | string): string {
|
||||
try {
|
||||
let asUrl: URL;
|
||||
if (typeof url === "string") {
|
||||
asUrl = new URL(url);
|
||||
} else {
|
||||
asUrl = url;
|
||||
}
|
||||
// get just the path to remove any potential url param that could have
|
||||
// some potential secrets
|
||||
return asUrl.origin + asUrl.pathname;
|
||||
} catch (error) {
|
||||
// defensive coding for malformed url
|
||||
return "??";
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Form and return a homeserver request URL based on the given path params and prefix.
|
||||
* @param path - The HTTP path <b>after</b> the supplied prefix e.g. "/createRoom".
|
||||
|
||||
Reference in New Issue
Block a user