Annotate WebAPI logs with request subtype

This commit is contained in:
Fedor Indutny 2021-09-28 13:55:50 -07:00 committed by GitHub
parent 68cef84c29
commit 0291830d16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -318,18 +318,17 @@ async function _promiseAjax(
providedUrl: string | null, providedUrl: string | null,
options: PromiseAjaxOptionsType options: PromiseAjaxOptionsType
): Promise<unknown> { ): Promise<unknown> {
const { proxyUrl, socketManager } = options;
const url = providedUrl || `${options.host}/${options.path}`; const url = providedUrl || `${options.host}/${options.path}`;
const logType = socketManager ? '(WS)' : '(REST)';
const redactedURL = options.redactUrl ? options.redactUrl(url) : url;
const unauthLabel = options.unauthenticated ? ' (unauth)' : ''; const unauthLabel = options.unauthenticated ? ' (unauth)' : '';
if (options.redactUrl) { log.info(`${options.type} ${logType} ${redactedURL}${unauthLabel}`);
log.info(`${options.type} ${options.redactUrl(url)}${unauthLabel}`);
} else {
log.info(`${options.type} ${url}${unauthLabel}`);
}
const timeout = typeof options.timeout === 'number' ? options.timeout : 10000; const timeout = typeof options.timeout === 'number' ? options.timeout : 10000;
const { proxyUrl, socketManager } = options;
const agentType = options.unauthenticated ? 'unauth' : 'auth'; const agentType = options.unauthenticated ? 'unauth' : 'auth';
const cacheKey = `${proxyUrl}-${agentType}`; const cacheKey = `${proxyUrl}-${agentType}`;
@ -430,21 +429,13 @@ async function _promiseAjax(
result = await response.textConverted(); result = await response.textConverted();
} }
} catch (e) { } catch (e) {
if (options.redactUrl) { log.error(options.type, logType, redactedURL, 0, 'Error');
log.error(options.type, options.redactUrl(url), 0, 'Error');
} else {
log.error(options.type, url, 0, 'Error');
}
const stack = `${e.stack}\nInitial stack:\n${options.stack}`; const stack = `${e.stack}\nInitial stack:\n${options.stack}`;
throw makeHTTPError('promiseAjax catch', 0, {}, e.toString(), stack); throw makeHTTPError('promiseAjax catch', 0, {}, e.toString(), stack);
} }
if (!isSuccess(response.status)) { if (!isSuccess(response.status)) {
if (options.redactUrl) { log.error(options.type, logType, redactedURL, response.status, 'Error');
log.info(options.type, options.redactUrl(url), response.status, 'Error');
} else {
log.error(options.type, url, response.status, 'Error');
}
throw makeHTTPError( throw makeHTTPError(
'promiseAjax: error response', 'promiseAjax: error response',
@ -461,16 +452,7 @@ async function _promiseAjax(
) { ) {
if (options.validateResponse) { if (options.validateResponse) {
if (!_validateResponse(result, options.validateResponse)) { if (!_validateResponse(result, options.validateResponse)) {
if (options.redactUrl) { log.error(options.type, logType, redactedURL, response.status, 'Error');
log.info(
options.type,
options.redactUrl(url),
response.status,
'Error'
);
} else {
log.error(options.type, url, response.status, 'Error');
}
throw makeHTTPError( throw makeHTTPError(
'promiseAjax: invalid response', 'promiseAjax: invalid response',
response.status, response.status,
@ -482,11 +464,7 @@ async function _promiseAjax(
} }
} }
if (options.redactUrl) { log.info(options.type, logType, redactedURL, response.status, 'Success');
log.info(options.type, options.redactUrl(url), response.status, 'Success');
} else {
log.info(options.type, url, response.status, 'Success');
}
if (options.responseType === 'byteswithdetails') { if (options.responseType === 'byteswithdetails') {
assert(result instanceof Uint8Array, 'Expected Uint8Array result'); assert(result instanceof Uint8Array, 'Expected Uint8Array result');