Skip to content
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

Improve logging of http requests to aid debugging #3485

Merged
merged 9 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion spec/unit/http-api/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { mocked } from "jest-mock";

import { FetchHttpApi } from "../../../src/http-api/fetch";
import { TypedEventEmitter } from "../../../src/models/typed-event-emitter";
import { ClientPrefix, HttpApiEvent, HttpApiEventHandlerMap, IdentityPrefix, IHttpOpts, Method } from "../../../src";
import { emitPromise } from "../../test-utils/test-utils";
import { QueryDict } from "../../../src/utils";
import { defer, QueryDict } from "../../../src/utils";
import { logger } from "../../../src/logger";

describe("FetchHttpApi", () => {
const baseUrl = "http://baseUrl";
Expand Down Expand Up @@ -290,4 +293,30 @@ describe("FetchHttpApi", () => {
runTests(baseUrlWithTrailingSlash);
});
});

it("should not log query parameters", async () => {
jest.useFakeTimers();
const deferred = defer<Response>();
const fetchFn = jest.fn().mockReturnValue(deferred.promise);
jest.spyOn(logger, "debug").mockImplementation(() => {});
const api = new FetchHttpApi(new TypedEventEmitter<any, any>(), { baseUrl, prefix, fetchFn });
const prom = api.requestOtherUrl(Method.Get, "https://server:1234/some/path#fragment?query=param");
richvdh marked this conversation as resolved.
Show resolved Hide resolved
jest.advanceTimersByTime(1234);
deferred.resolve({ ok: true, text: () => Promise.resolve("RESPONSE") } as Response);
richvdh marked this conversation as resolved.
Show resolved Hide resolved
await prom;
expect(logger.debug).not.toHaveBeenCalledWith("fragment");
expect(logger.debug).not.toHaveBeenCalledWith("query");
expect(logger.debug).not.toHaveBeenCalledWith("param");
expect(logger.debug).toHaveBeenCalledTimes(2);
expect(mocked(logger.debug).mock.calls[0]).toMatchInlineSnapshot(`
[
"FetchHttpApi: --> GET https://server:1234/some/path",
richvdh marked this conversation as resolved.
Show resolved Hide resolved
]
`);
expect(mocked(logger.debug).mock.calls[1]).toMatchInlineSnapshot(`
[
"FetchHttpApi: <-- undefined 1234ms GET https://server:1234/some/path",
richvdh marked this conversation as resolved.
Show resolved Hide resolved
]
`);
});
});
24 changes: 24 additions & 0 deletions src/http-api/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -274,7 +279,10 @@ export class FetchHttpApi<O extends IHttpOpts> {
credentials: "omit", // we send credentials via headers
keepalive: keepAlive,
});

logger.debug(`FetchHttpApi: <-- ${res.status} ${Date.now() - start}ms ${method} ${urlForLogs}`);
} catch (e) {
logger.debug(`FetchHttpApi: <-- ${e} ${Date.now() - start}ms ${method} ${urlForLogs}`);
if ((<Error>e).name === "AbortError") {
throw e;
}
Expand All @@ -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".
Expand Down
Loading