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

chore(sitelaunch): better error message #1358

Merged
merged 1 commit into from
May 3, 2024

Conversation

kishore03109
Copy link
Contributor

@kishore03109 kishore03109 commented Apr 30, 2024

Problem

The existing statements does not print the error (since errors are not serializable) as mentioned here

In addition, we have not been using the child loggers as effectively as possible to provide the context needed to debug efficiently when errors arise. A obvious example is our constant noise of Github API call made, but we have no context surrounding that particular call being made.

To fix this issue, this pr introduces a small introduction of using child loggers, which has already been declared in our codebase by @seaerchin. A child logger is used to create a new logger instance that inherits all of its parent's metadata (e.g. logger name) and attach new metadata for all logs written by that child logger. This way we avoid having to keep repeating our context in the multiple calls that are needed.

Help needed, but not a blocker to this particular PR.

Moving forward, my stance is that we would want to inject the logger directly onto the child functions in different modules.

eg in this trace in the happy path, we can see that the trace for all the calls are not instantiated with the meta that I would like it to have.

Screenshot 2024-05-02 at 11 12 45 AM

The reason for this is because the other logs are done in seperate modules, and if we were to pass the child logger over, this means that for every call, we would need to pass in the logger with bindings as needed. But, I see how this could lead to verbose code.

To me, it seems like there is a balance to be strived for. I personally think using child loggers just the module is not very helpful. What I found more useful is the ability to view the context around the call useful. Ie, with what set of inputs did this module behave in the manner that we see here. I think this can help with debugging, rather than just the module name. But I am not too sure where to draw the line between passing the context form the parent service vs depending on the child service to log the relevant meta as needed during debugging. Happy to hear your thoughts on this. Note that regardless of the direction that we chose, this pr should still go in as it is only scoped to one module + it is an incremental improvement to introducing the usage of child loggers in our codebase.

Solution

use pino to serialize the errors instead + use child loggers with relevant context so that subsequent loggers do not need to keep repeating the context over and over again.

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @kishore03109 and the rest of your teammates on Graphite Graphite

@kishore03109 kishore03109 marked this pull request as ready for review April 30, 2024 02:00
@kishore03109 kishore03109 requested a review from a team April 30, 2024 02:00
@timotheeg
Copy link
Contributor

Thanks @kishore03109 🙏

My recommendation when updating logging in a given file (as part of "clean up your neighbourhood" activities) is:

  • update ALL the log call sites in the file (not just error() calls, info()/warn() calls as well)
  • remove message interpolation in favour of data fields at all call sites
  • create a child logger for the file / module to specify the module

@kishore03109 kishore03109 force-pushed the 04-30-chore_sitelaunch_better_error_message branch from fd7328e to 5d89291 Compare April 30, 2024 05:13
@kishore03109
Copy link
Contributor Author

Thanks @kishore03109 🙏
My recommendation when updating logging in a given file (as part of "clean up your neighbourhood" activities) is:

  • update ALL the log call sites in the file (not just error() calls, info()/warn() calls as well)
  • remove message interpolation in favour of data fields at all call sites
  • create a child logger for the file / module to specify the module

@timotheeg, apologies, have done changes to address points one and two.

i am unclear of the third point, wondering if this pattern is used anywhere in the current codebase that i can reference the pattern from!

@timotheeg
Copy link
Contributor

i am unclear of the third point, wondering if this pattern is used anywhere in the current codebase that i can reference the pattern from!

Ah yes, the intention to use child loggers was there ever since the new logger was merged, but I don’t think any module was updated to make use of it yet (@seaerchin intended to do it) Apologies for dumping that on you as a “little surprise” 😅

So it’s a very minor stuff, but it’s something every dev should be aware of, so we can multiply the effects of doing incremental cleanup whenever we touch code.

The idea is that log entries have broad contextual info (e.g. which module is doing the logging), and very specific info for the call sites (code flows, error condition, etc.)

Rather than pollute the call sites with the broad context everywhere, child loggers are a way to encapsulate the context in a new logger instance. Like so:

import logger as baseLogger from "@root/logger/logger"

const logger = baseLogger.child({ module: 'SiteService' })

// later in the file
logger.info({message: 'foo', /* etc... */})

This means that in the file, your log entries will automatically have some additional information added to them, but your call sites will be as lean as possible.

It’s the same principle as DD adding the service name and environment transparently to log entries, but in this case, the base loggers and child loggers are explicitly controlled by us.

You are able to dump put more info into the child logger when you instantiate it btw (if you so wish), and/or you can have child’s child loggers (if that is useful for whatever reason).

Note that having the “module” as broad context doesn’t necessarily have a lot of value... APM is our killer tool to know the true context of a log call, BUT APM traces expire and we might need to revisit log entries in the future (e.g. police summon). So, having the log entries self-contained with context is useful. The context is also helpful for cases where we have duplicated log message in different places of the code base (e.g. “Record updated”), which is terrible because we can’t know where a given log line is from, and so the extra context help disambiguating.

@kishore03109 kishore03109 force-pushed the 04-30-chore_sitelaunch_better_error_message branch 3 times, most recently from 2c05f01 to acb43fa Compare May 2, 2024 03:20
Copy link
Contributor

@timotheeg timotheeg left a comment

Choose a reason for hiding this comment

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

Hey Kishore, I added a comment. I might be misunderstanding, but I see a potential red flags. Please check :)

support/routes/v2/formsg/formsgSiteLaunch.ts Outdated Show resolved Hide resolved
@kishore03109 kishore03109 force-pushed the 04-30-chore_sitelaunch_better_error_message branch from acb43fa to ccaf5b0 Compare May 2, 2024 09:22
@kishore03109 kishore03109 requested a review from timotheeg May 2, 2024 09:27
Copy link
Contributor

@timotheeg timotheeg left a comment

Choose a reason for hiding this comment

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

LGTM

@kishore03109 kishore03109 merged commit c4aa6a5 into develop May 3, 2024
12 checks passed
@mergify mergify bot deleted the 04-30-chore_sitelaunch_better_error_message branch May 3, 2024 02:20
@dcshzj dcshzj mentioned this pull request May 13, 2024
10 tasks
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