Skip to content

Commit

Permalink
feat: add trace to logs (#405)
Browse files Browse the repository at this point in the history
* chore: install express-request-id

* chore: update types

* chore: use addRequestId() middleware

* feat: add trace to logs

* chore: add more traces

* chore: include trace in loggingMiddleware
  • Loading branch information
tshuli authored Oct 5, 2020
1 parent 373761d commit 306800f
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 16 deletions.
29 changes: 26 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
"@sentry/browser": "^5.24.2",
"@sentry/integrations": "^5.24.2",
"@stablelib/base64": "^1.0.0",
"@types/express-request-id": "^1.4.1",
"@types/express-serve-static-core": "^4.17.13",
"JSONStream": "^1.3.5",
"angular": "~1.8.0",
"angular-animate": "^1.8.0",
Expand Down Expand Up @@ -112,6 +114,7 @@
"ejs": "^3.1.5",
"express": "^4.16.4",
"express-device": "~0.4.2",
"express-request-id": "^1.4.1",
"express-session": "^1.15.6",
"express-winston": "^4.0.5",
"fetch-readablestream": "^0.2.0",
Expand Down
8 changes: 7 additions & 1 deletion src/app/controllers/admin-console.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Form = getFormModel(mongoose)
const _ = require('lodash')

const logger = require('../../config/logger').createLoggerWithLabel(module)
const { getRequestIp } = require('../utils/request')
const { getRequestIp, getTrace } = require('../utils/request')

// Examples search-specific constants
const PAGE_SIZE = 16 // maximum number of results to return
Expand Down Expand Up @@ -577,6 +577,7 @@ exports.getExampleFormsUsingAggregateCollection = function (req, res) {
meta: {
action: 'getExampleFormsUsingAggregateCollection',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down Expand Up @@ -609,6 +610,7 @@ exports.getExampleFormsUsingSubmissionsCollection = function (req, res) {
meta: {
action: 'getExampleFormsUsingSubmissionsCollection',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down Expand Up @@ -699,6 +701,7 @@ exports.getSingleExampleFormUsingSubmissionCollection = function (req, res) {
meta: {
action: 'getSingleExampleFormUsingSubmissionCollection',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down Expand Up @@ -727,6 +730,7 @@ exports.getSingleExampleFormUsingAggregateCollection = function (req, res) {
meta: {
action: 'getSingleExampleFormUsingAggregateCollection',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down Expand Up @@ -808,6 +812,7 @@ exports.getLoginStats = function (req, res) {
meta: {
action: 'getLoginStats',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand All @@ -827,6 +832,7 @@ exports.getLoginStats = function (req, res) {
}`,
meta: {
action: 'getLoginStats',
trace: getTrace(req),
},
})

Expand Down
12 changes: 11 additions & 1 deletion src/app/controllers/admin-forms.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { StatusCodes } = require('http-status-codes')

const logger = require('../../config/logger').createLoggerWithLabel(module)
const errorHandler = require('./errors.server.controller')
const { getRequestIp } = require('../utils/request')
const { getRequestIp, getTrace } = require('../utils/request')
const { FormLogoState } = require('../../types')

const {
Expand Down Expand Up @@ -69,6 +69,7 @@ function makeModule(connection) {
meta: {
action: 'respondOnMongoError',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down Expand Up @@ -290,6 +291,7 @@ function makeModule(connection) {
meta: {
action: 'makeModule.update',
ip: getRequestIp(req),
trace: getTrace(req),
formId: form._id,
},
})
Expand All @@ -307,6 +309,7 @@ function makeModule(connection) {
meta: {
action: 'makeModule.update',
ip: getRequestIp(req),
trace: getTrace(req),
formId: form._id,
},
error,
Expand Down Expand Up @@ -521,6 +524,7 @@ function makeModule(connection) {
meta: {
action: 'makeModule.countFeedback',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down Expand Up @@ -550,6 +554,7 @@ function makeModule(connection) {
meta: {
action: 'makeModule.streamFeedback',
ip: getRequestIp(req),
trace: getTrace(req),
},
error: err,
})
Expand All @@ -564,6 +569,7 @@ function makeModule(connection) {
meta: {
action: 'makeModule.streamFeedback',
ip: getRequestIp(req),
trace: getTrace(req),
},
error: err,
})
Expand All @@ -578,6 +584,7 @@ function makeModule(connection) {
meta: {
action: 'makeModule.streamFeedback',
ip: getRequestIp(req),
trace: getTrace(req),
},
error: err,
})
Expand Down Expand Up @@ -689,6 +696,7 @@ function makeModule(connection) {
meta: {
action: 'makeModule.streamFeedback',
ip: getRequestIp(req),
trace: getTrace(req),
},
error: err,
})
Expand Down Expand Up @@ -736,6 +744,7 @@ function makeModule(connection) {
meta: {
action: 'makeModule.streamFeedback',
ip: getRequestIp(req),
trace: getTrace(req),
},
error: err,
})
Expand Down Expand Up @@ -764,6 +773,7 @@ function makeModule(connection) {
meta: {
action: 'makeModule.transferOwner',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down
3 changes: 2 additions & 1 deletion src/app/controllers/authentication.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
const { StatusCodes } = require('http-status-codes')
const PERMISSIONS = require('../utils/permission-levels').default
const { getRequestIp } = require('../utils/request')
const { getRequestIp, getTrace } = require('../utils/request')
const logger = require('../../config/logger').createLoggerWithLabel(module)

/**
Expand Down Expand Up @@ -50,6 +50,7 @@ const logUnauthorizedAccess = (req, action, requiredPermission) => {
meta: {
action: action,
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down
6 changes: 5 additions & 1 deletion src/app/controllers/core.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const _ = require('lodash')
const { StatusCodes } = require('http-status-codes')

const config = require('../../config/config')
const { getRequestIp } = require('../utils/request')
const { getRequestIp, getTrace } = require('../utils/request')
const logger = require('../../config/logger').createLoggerWithLabel(module)

const getFormStatisticsTotalModel = require('../models/form_statistics_total.server.model')
Expand Down Expand Up @@ -53,6 +53,7 @@ exports.formCountUsingAggregateCollection = (req, res) => {
meta: {
action: 'formCountUsingAggregateCollection',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down Expand Up @@ -97,6 +98,7 @@ exports.formCountUsingSubmissionsCollection = (req, res) => {
meta: {
action: 'formCountUsingSubmissionsCollection',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down Expand Up @@ -124,6 +126,7 @@ exports.userCount = (req, res) => {
meta: {
action: 'userCount',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand All @@ -149,6 +152,7 @@ exports.submissionCount = (req, res) => {
meta: {
action: 'submissionCount',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down
14 changes: 13 additions & 1 deletion src/app/controllers/email-submissions.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const mongoose = require('mongoose')
const { getEmailSubmissionModel } = require('../models/submission.server.model')
const emailSubmission = getEmailSubmissionModel(mongoose)
const { StatusCodes } = require('http-status-codes')
const { getRequestIp } = require('../utils/request')
const { getRequestIp, getTrace } = require('../utils/request')
const { ConflictError } = require('../modules/submission/submission.errors')
const { MB } = require('../constants/filesize')
const {
Expand Down Expand Up @@ -57,6 +57,7 @@ exports.receiveEmailSubmissionUsingBusBoy = function (req, res, next) {
meta: {
action: 'receiveEmailSubmissionUsingBusBoy',
ip: getRequestIp(req),
trace: getTrace(req),
formId: _.get(req, 'form._id'),
},
error: err,
Expand Down Expand Up @@ -108,6 +109,7 @@ exports.receiveEmailSubmissionUsingBusBoy = function (req, res, next) {
meta: {
action: 'receiveEmailSubmissionUsingBusBoy',
ip: getRequestIp(req),
trace: getTrace(req),
formId: req.form._id,
},
error: err,
Expand All @@ -125,6 +127,7 @@ exports.receiveEmailSubmissionUsingBusBoy = function (req, res, next) {
meta: {
action: 'receiveEmailSubmissionUsingBusBoy',
ip: getRequestIp(req),
trace: getTrace(req),
formId: req.form._id,
},
})
Expand Down Expand Up @@ -155,6 +158,7 @@ exports.receiveEmailSubmissionUsingBusBoy = function (req, res, next) {
action: 'receiveEmailSubmissionUsingBusBoy',
formId: req.form._id,
ip: getRequestIp(req),
trace: getTrace(req),
uin: hashedUinFin,
submission: hashedSubmission,
},
Expand All @@ -168,6 +172,7 @@ exports.receiveEmailSubmissionUsingBusBoy = function (req, res, next) {
meta: {
action: 'receiveEmailSubmissionUsingBusBoy',
ip: getRequestIp(req),
trace: getTrace(req),
formId: req.form._id,
},
})
Expand All @@ -192,6 +197,7 @@ exports.receiveEmailSubmissionUsingBusBoy = function (req, res, next) {
meta: {
action: 'receiveEmailSubmissionUsingBusBoy',
ip: getRequestIp(req),
trace: getTrace(req),
formId: req.form._id,
uin: hashedUinFin,
submission: hashedSubmission,
Expand All @@ -210,6 +216,7 @@ exports.receiveEmailSubmissionUsingBusBoy = function (req, res, next) {
meta: {
action: 'receiveEmailSubmissionUsingBusBoy',
ip: getRequestIp(req),
trace: getTrace(req),
formId: req.form._id,
},
error: err,
Expand Down Expand Up @@ -246,6 +253,7 @@ exports.validateEmailSubmission = function (req, res, next) {
meta: {
action: 'validateEmailSubmission',
ip: getRequestIp(req),
trace: getTrace(req),
formId: req.form._id,
},
error: err,
Expand Down Expand Up @@ -502,6 +510,7 @@ function onSubmissionEmailFailure(err, req, res, submission) {
meta: {
action: 'onSubmissionEmailFailure',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
headers: req.headers,
},
Expand Down Expand Up @@ -601,6 +610,7 @@ exports.saveMetadataToDb = function (req, res, next) {
submissionId: submission.id,
formId: form._id,
ip: getRequestIp(req),
trace: getTrace(req),
responseHash: submission.responseHash,
},
})
Expand Down Expand Up @@ -643,6 +653,7 @@ exports.sendAdminEmail = async function (req, res, next) {
submissionId: submission.id,
formId: form._id,
ip: getRequestIp(req),
trace: getTrace(req),
submissionHash: submission.responseHash,
},
})
Expand All @@ -665,6 +676,7 @@ exports.sendAdminEmail = async function (req, res, next) {
submissionId: submission.id,
formId: form._id,
ip: getRequestIp(req),
trace: getTrace(req),
submissionHash: submission.responseHash,
},
error: err,
Expand Down
Loading

0 comments on commit 306800f

Please sign in to comment.