mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-04-14 19:56:23 +00:00
log slow requests to loggly (#15364)
This commit is contained in:
parent
8c71ca12b8
commit
29eb8ca10b
3 changed files with 26 additions and 1 deletions
|
|
@ -93,5 +93,6 @@
|
||||||
"TRUSTED_DOMAINS": "localhost,https://habitica.com",
|
"TRUSTED_DOMAINS": "localhost,https://habitica.com",
|
||||||
"TIME_TRAVEL_ENABLED": "false",
|
"TIME_TRAVEL_ENABLED": "false",
|
||||||
"DEBUG_ENABLED": "false",
|
"DEBUG_ENABLED": "false",
|
||||||
"CONTENT_SWITCHOVER_TIME_OFFSET": 8
|
"CONTENT_SWITCHOVER_TIME_OFFSET": 8,
|
||||||
|
"SLOW_REQUEST_THRESHOLD": 1000
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,12 +33,14 @@ import {
|
||||||
} from './language';
|
} from './language';
|
||||||
import {
|
import {
|
||||||
logRequestData,
|
logRequestData,
|
||||||
|
logSlowRequests,
|
||||||
} from './requestLogHandler';
|
} from './requestLogHandler';
|
||||||
|
|
||||||
const IS_PROD = nconf.get('IS_PROD');
|
const IS_PROD = nconf.get('IS_PROD');
|
||||||
const DISABLE_LOGGING = nconf.get('DISABLE_REQUEST_LOGGING') === 'true';
|
const DISABLE_LOGGING = nconf.get('DISABLE_REQUEST_LOGGING') === 'true';
|
||||||
const ENABLE_HTTP_AUTH = nconf.get('SITE_HTTP_AUTH_ENABLED') === 'true';
|
const ENABLE_HTTP_AUTH = nconf.get('SITE_HTTP_AUTH_ENABLED') === 'true';
|
||||||
const LOG_REQUESTS_EXCESSIVE_MODE = nconf.get('LOG_REQUESTS_EXCESSIVE_MODE') === 'true';
|
const LOG_REQUESTS_EXCESSIVE_MODE = nconf.get('LOG_REQUESTS_EXCESSIVE_MODE') === 'true';
|
||||||
|
const SLOW_REQUEST_THRESHOLD = nconf.get('SLOW_REQUEST_THRESHOLD');
|
||||||
// const PUBLIC_DIR = path.join(__dirname, '/../../client');
|
// const PUBLIC_DIR = path.join(__dirname, '/../../client');
|
||||||
|
|
||||||
const SESSION_SECRET = nconf.get('SESSION_SECRET');
|
const SESSION_SECRET = nconf.get('SESSION_SECRET');
|
||||||
|
|
@ -51,6 +53,10 @@ export default function attachMiddlewares (app, server) {
|
||||||
app.use(logRequestData);
|
app.use(logRequestData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (SLOW_REQUEST_THRESHOLD > 0) {
|
||||||
|
app.use(logSlowRequests);
|
||||||
|
}
|
||||||
|
|
||||||
if (ENABLE_CLUSTER) {
|
if (ENABLE_CLUSTER) {
|
||||||
app.use(domainMiddleware(server, mongoose));
|
app.use(domainMiddleware(server, mongoose));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
|
import nconf from 'nconf';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
import omit from 'lodash/omit';
|
import omit from 'lodash/omit';
|
||||||
import logger from '../libs/logger';
|
import logger from '../libs/logger';
|
||||||
|
|
||||||
|
const SLOW_REQUEST_THRESHOLD = nconf.get('SLOW_REQUEST_THRESHOLD');
|
||||||
|
|
||||||
function buildBaseLogData (req) {
|
function buildBaseLogData (req) {
|
||||||
return {
|
return {
|
||||||
requestId: req.requestIdentifier,
|
requestId: req.requestIdentifier,
|
||||||
|
|
@ -35,3 +38,18 @@ export const logRequestData = (req, res, next) => {
|
||||||
});
|
});
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const logSlowRequests = (req, res, next) => {
|
||||||
|
req.requestStartTime = Date.now();
|
||||||
|
req.on('close', () => {
|
||||||
|
const requestTime = Date.now() - req.requestStartTime;
|
||||||
|
if (requestTime > SLOW_REQUEST_THRESHOLD) {
|
||||||
|
const data = buildBaseLogData(req);
|
||||||
|
data.duration = requestTime;
|
||||||
|
data.endTime = Date.now();
|
||||||
|
data.statusCode = res.statusCode;
|
||||||
|
logger.error(Error('Slow request'), data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
next();
|
||||||
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue