Skip to content

Commit 7176cb3

Browse files
committed
ARSN-531: record server access log fields in s3routes
1 parent d92702e commit 7176cb3

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

lib/s3routes/routesUtils.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ const jsutil = require('../jsutil');
1414

1515
const ALLOW_INVALID_META_HEADERS = !!process.env.ALLOW_INVALID_META_HEADERS;
1616

17+
function storeServerAccessLogFields(res: http.ServerResponse, endTurnAroundTime: bigint, errorCode?: string) {
18+
// @ts-expect-error
19+
if (res.serverAccessLog) {
20+
// @ts-expect-error
21+
res.serverAccessLog.errorCode = errorCode;
22+
// @ts-expect-error
23+
res.serverAccessLog.endTurnAroundTime = endTurnAroundTime;
24+
}
25+
}
26+
1727
export type CallApiMethod = (
1828
methodName: string,
1929
request: http.IncomingMessage,
@@ -97,6 +107,7 @@ export function okHeaderResponse(
97107
setCommonResponseHeaders(headers, response, log);
98108
log.debug('response http code', { httpCode });
99109
response.writeHead(httpCode);
110+
storeServerAccessLogFields(response, process.hrtime.bigint());
100111
return response.end(() => {
101112
log.end().info('responded to request', {
102113
httpCode: response.statusCode,
@@ -129,6 +140,7 @@ export const XMLResponseBackend = {
129140
setCommonResponseHeaders(additionalHeaders, response, log);
130141
response.writeHead(200, { 'Content-type': 'application/xml' });
131142
log.trace('xml response', { xml });
143+
storeServerAccessLogFields(response, process.hrtime.bigint());
132144
return response.end(xml, 'utf8', () => {
133145
log.end().info('responded with XML', {
134146
httpCode: response.statusCode,
@@ -148,6 +160,7 @@ export const XMLResponseBackend = {
148160
// early return to avoid extra headers and XML data
149161
if (error.code === 304) {
150162
response.writeHead(error.code);
163+
storeServerAccessLogFields(response, process.hrtime.bigint(), error.message);
151164
return response.end('', 'utf8', () => {
152165
log.end().info('responded with empty body', {
153166
httpCode: response.statusCode,
@@ -191,6 +204,7 @@ export const XMLResponseBackend = {
191204
'Content-Type': 'application/xml',
192205
'Content-Length': bytesSent,
193206
});
207+
storeServerAccessLogFields(response, process.hrtime.bigint(), error.message);
194208
return response.end(xmlStr, 'utf8', () => {
195209
log.end().info('responded with error XML', {
196210
httpCode: response.statusCode,
@@ -221,6 +235,7 @@ export const JSONResponseBackend = {
221235
setCommonResponseHeaders(additionalHeaders, response, log);
222236
response.writeHead(200, { 'Content-type': 'application/json' });
223237
log.trace('sending success json response', { json });
238+
storeServerAccessLogFields(response, process.hrtime.bigint());
224239
return response.end(json, 'utf8', () => {
225240
log.end().info('responded with JSON', {
226241
httpCode: response.statusCode,
@@ -267,6 +282,7 @@ export const JSONResponseBackend = {
267282
'Content-Type': 'application/json',
268283
'Content-Length': bytesSent,
269284
});
285+
storeServerAccessLogFields(response, process.hrtime.bigint(), error.message);
270286
return response.end(data, 'utf8', () => {
271287
log.end().info('responded with error JSON', {
272288
httpCode: response.statusCode,
@@ -388,6 +404,7 @@ export function retrieveData(
388404
log: RequestLogger,
389405
) {
390406
if (locations.length === 0) {
407+
storeServerAccessLogFields(response, process.hrtime.bigint());
391408
return response.end();
392409
}
393410
if (locations[0].azureStreamingOptions) {
@@ -480,6 +497,8 @@ export function retrieveData(
480497
// in the callback of data.get()? The 'close' event is not
481498
// called if 'end' is.
482499
currentStream = readable;
500+
501+
storeServerAccessLogFields(response, process.hrtime.bigint());
483502
return readable.pipe(response, { end: false });
484503
}
485504
), err => {
@@ -641,6 +660,8 @@ export function responseContentHeaders(
641660
log.debug('response http code', { httpCode: 200 });
642661
response.writeHead(200);
643662
}
663+
664+
storeServerAccessLogFields(response, process.hrtime.bigint());
644665
return response.end(() => {
645666
log.end().info('responded with content headers', {
646667
httpCode: response.statusCode,
@@ -686,7 +707,7 @@ export function responseStreamData(
686707
dataLocations)) {
687708
log.error(
688709
'logic error: total length of fetched data ' +
689-
'locations does not match returned content-length',
710+
'locations does not match returned content-length',
690711
{ contentLength, dataLocations });
691712
return XMLResponseBackend.errorResponse(errors.InternalError,
692713
response, log,
@@ -700,6 +721,8 @@ export function responseStreamData(
700721
range, log);
701722
}
702723
if (dataLocations === null || _computeContentLengthFromLocation(dataLocations) === 0) {
724+
725+
storeServerAccessLogFields(response, process.hrtime.bigint());
703726
return response.end(() => {
704727
log.end().info('responded with only metadata', {
705728
httpCode: response.statusCode,
@@ -799,6 +822,7 @@ export function errorHtmlResponse(
799822
'</html>',
800823
);
801824

825+
storeServerAccessLogFields(response, process.hrtime.bigint(), error.message);
802826
return response.end(html.join(''), 'utf8', () => {
803827
log.end().info('responded with error html', {
804828
httpCode: response.statusCode,
@@ -824,6 +848,7 @@ export function errorHeaderResponse(
824848
response.setHeader('x-amz-error-code', error.message);
825849
response.setHeader('x-amz-error-message', error.description);
826850
response.writeHead(error.code);
851+
storeServerAccessLogFields(response, process.hrtime.bigint(), error.message);
827852
return response.end(() => {
828853
log.end().info('responded with error headers', {
829854
httpCode: response.statusCode,
@@ -915,6 +940,7 @@ export function redirectRequest(
915940
response.writeHead(redirectCode, {
916941
Location: redirectLocation,
917942
});
943+
storeServerAccessLogFields(response, process.hrtime.bigint());
918944
response.end();
919945
return undefined;
920946
}
@@ -1140,7 +1166,7 @@ export function normalizeRequest(
11401166
// the x-amz-decoded-content-length
11411167
const contentLength = request.headers['x-amz-decoded-content-length'] ?
11421168
request.headers['x-amz-decoded-content-length'] :
1143-
request.headers['content-length'];
1169+
request.headers['content-length']; // here
11441170
// @ts-expect-error
11451171
request.parsedContentLength =
11461172
Number.parseInt(contentLength?.toString() ?? '', 10);

0 commit comments

Comments
 (0)