Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add trace to logs #405

Merged
merged 6 commits into from
Oct 5, 2020
Merged

feat: add trace to logs #405

merged 6 commits into from
Oct 5, 2020

Conversation

tshuli
Copy link
Contributor

@tshuli tshuli commented Oct 2, 2020

Problem

Implements tracing for (https://github.com/datagovsg/formsg-private/issues/25)

Solution

Add trace to logs using cloudflare cf-ray header. If cf-ray does not exist, trace using x-request-id header

Tests

On staging

  • At the login page, key in an invalid email address abc@abc.com. Check that the value of cf-ray header is logged under trace in the server logs error message.
  • Access a public form. Check that the value of cf-ray header is logged under trace in the server logs for the get request.

On localhost

  • At the login page, key in an invalid email address abc@abc.com. Check that the value of x-request-id header is logged under trace in the server logs error message.
  • In L25-L27 of logging.ts, replace
isDev
  ? format.combine(format.colorize(), errorPrinter(), customFormat)
  : format.json({ replacer: jsonErrorReplacer }),

with

format.json({ replacer: jsonErrorReplacer }),

Access a public form. Check that the value of x-request-id header is logged under trace in the server logs for the get request.

New dependencies

  • express-request-id^1.4.1

@tshuli tshuli marked this pull request as ready for review October 3, 2020 13:25
@tshuli tshuli changed the title wip: feat: add trace to logs feat: add trace to logs Oct 3, 2020
Copy link
Contributor

@karrui karrui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@@ -1,5 +1,14 @@
import { Request } from 'express'

export const getRequestIp = (req: Request) => {
interface IRequestWithId extends Request {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can we extend the namespace globally (following suggestions in this SO post, some may be outdated) so it's consistent throughout the application?

Can also be done in a different PR, since I think we need to extend request to also have req.session.user

Copy link
Contributor Author

@tshuli tshuli Oct 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! Will create issue for this and work on it

@@ -577,6 +577,7 @@ exports.getExampleFormsUsingAggregateCollection = function (req, res) {
meta: {
action: 'getExampleFormsUsingAggregateCollection',
ip: getRequestIp(req),
trace: getTrace(req),
url: req.url,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scope for this PR, but can you help create an issue for an utility function that returns the relevant log info from a req? Seems like the ip, trace, url, header combo is repeated throughout the application :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants