Skip to content

Commit

Permalink
feat: Set bytesin log as optional (#2013)
Browse files Browse the repository at this point in the history
#1912

Co-authored-by: frimuchkov <frimuchkov@yandex-team.ru>
  • Loading branch information
frimuchkov and frimuchkov authored Dec 2, 2020
1 parent 1e59b6a commit e2f7bb2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 85 deletions.
2 changes: 2 additions & 0 deletions conf/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ logs:
# token: false
# # support for the new v1 search endpoint, functional by incomplete read more on ticket 1732
# search: false
# # disable writing body size to logs, read more on ticket 1912
# bytesin_off: false

# This affect the web and api (not developed yet)
#i18n:
Expand Down
2 changes: 1 addition & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const defineAPI = function(config: IConfig, storage: IStorageHandler): any {
app.use(cors());

// Router setup
app.use(log);
app.use(log(config));
app.use(errorReportingMiddleware);
app.use(function(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
res.setHeader('X-Powered-By', config.user_agent);
Expand Down
172 changes: 88 additions & 84 deletions src/api/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,102 +174,106 @@ export const LOG_STATUS_MESSAGE = "@{status}, user: @{user}(@{remoteIP}), req: '
export const LOG_VERDACCIO_ERROR = `${LOG_STATUS_MESSAGE}, error: @{!error}`;
export const LOG_VERDACCIO_BYTES = `${LOG_STATUS_MESSAGE}, bytes: @{bytes.in}/@{bytes.out}`;

export function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
// logger
req.log = logger.child({ sub: 'in' });
export function log(config: Config) {
return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
// logger
req.log = logger.child({ sub: 'in' });

const _auth = req.headers.authorization;
if (_.isNil(_auth) === false) {
req.headers.authorization = '<Classified>';
}

const _auth = req.headers.authorization;
if (_.isNil(_auth) === false) {
req.headers.authorization = '<Classified>';
}
const _cookie = req.headers.cookie;
if (_.isNil(_cookie) === false) {
req.headers.cookie = '<Classified>';
}

const _cookie = req.headers.cookie;
if (_.isNil(_cookie) === false) {
req.headers.cookie = '<Classified>';
}
req.url = req.originalUrl;
req.log.info({ req: req, ip: req.ip }, "@{ip} requested '@{req.method} @{req.url}'");
req.originalUrl = req.url;

req.url = req.originalUrl;
req.log.info({ req: req, ip: req.ip }, "@{ip} requested '@{req.method} @{req.url}'");
req.originalUrl = req.url;
if (_.isNil(_auth) === false) {
req.headers.authorization = _auth;
}

if (_.isNil(_auth) === false) {
req.headers.authorization = _auth;
}
if (_.isNil(_cookie) === false) {
req.headers.cookie = _cookie;
}

if (_.isNil(_cookie) === false) {
req.headers.cookie = _cookie;
}
let bytesin = 0;
if (config?.experiments?.bytesin_off !== true) {
req.on('data', function(chunk): void {
bytesin += chunk.length;
});
}

let bytesin = 0;
req.on('data', function(chunk): void {
bytesin += chunk.length;
});

let bytesout = 0;
const _write = res.write;
// FIXME: res.write should return boolean
// @ts-ignore
res.write = function(buf): boolean {
bytesout += buf.length;
/* eslint prefer-rest-params: "off" */
let bytesout = 0;
const _write = res.write;
// FIXME: res.write should return boolean
// @ts-ignore
_write.apply(res, arguments);
};
res.write = function(buf): boolean {
bytesout += buf.length;
/* eslint prefer-rest-params: "off" */
// @ts-ignore
_write.apply(res, arguments);
};

let logHasBeenCalled = false;
const log = function(): void {
if (logHasBeenCalled) {
return;
}
logHasBeenCalled = true;

const forwardedFor = req.headers['x-forwarded-for'];
const remoteAddress = req.connection.remoteAddress;
const remoteIP = forwardedFor ? `${forwardedFor} via ${remoteAddress}` : remoteAddress;
let message;
if (res._verdaccio_error) {
message = LOG_VERDACCIO_ERROR;
} else {
message = LOG_VERDACCIO_BYTES;
}
let logHasBeenCalled = false;
const log = function(): void {
if (logHasBeenCalled) {
return;
}
logHasBeenCalled = true;

const forwardedFor = req.headers['x-forwarded-for'];
const remoteAddress = req.connection.remoteAddress;
const remoteIP = forwardedFor ? `${forwardedFor} via ${remoteAddress}` : remoteAddress;
let message;
if (res._verdaccio_error) {
message = LOG_VERDACCIO_ERROR;
} else {
message = LOG_VERDACCIO_BYTES;
}

req.url = req.originalUrl;
req.log.warn(
{
request: {
method: req.method,
url: req.url,
req.url = req.originalUrl;
req.log.warn(
{
request: {
method: req.method,
url: req.url,
},
level: 35, // http
user: (req.remote_user && req.remote_user.name) || null,
remoteIP,
status: res.statusCode,
error: res._verdaccio_error,
bytes: {
in: bytesin,
out: bytesout,
},
},
level: 35, // http
user: (req.remote_user && req.remote_user.name) || null,
remoteIP,
status: res.statusCode,
error: res._verdaccio_error,
bytes: {
in: bytesin,
out: bytesout,
},
},
message
);
req.originalUrl = req.url;
};
message
);
req.originalUrl = req.url;
};

req.on('close', function(): void {
log();
});
req.on('close', function(): void {
log();
});

const _end = res.end;
res.end = function(buf): void {
if (buf) {
bytesout += buf.length;
}
/* eslint prefer-rest-params: "off" */
// @ts-ignore
_end.apply(res, arguments);
log();
};
next();
const _end = res.end;
res.end = function(buf): void {
if (buf) {
bytesout += buf.length;
}
/* eslint prefer-rest-params: "off" */
// @ts-ignore
_end.apply(res, arguments);
log();
};
next();
}
}

// Middleware
Expand Down

0 comments on commit e2f7bb2

Please sign in to comment.