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: introduce a pattern to help standardize log tag names #2371

Merged
merged 7 commits into from
Oct 28, 2024

Conversation

aditiharini
Copy link
Contributor

@aditiharini aditiharini commented Oct 21, 2024

It's hard to filter logs by tag if we name the same tag different things in different places. Introduce a pattern for constructing logs that pushes us toward standardizing log tag names.

Merge Checklist

Choose all relevant options below by adding an x now or at any time before submitting for review


PR-Codex overview

This PR focuses on standardizing log tag names by introducing a Tags class, which is used throughout the codebase to create consistent log entries.

Detailed summary

  • Introduced Tags class in apps/hubble/src/utils/logger.ts for standardized log tags.
  • Updated logging in apps/hubble/src/hubble.ts, apps/hubble/src/network/p2p/gossipNode.ts, and apps/hubble/src/network/sync/syncHealthJob.ts to use Tags.
  • Added TagFields type definition for better structure of log fields.

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Copy link

changeset-bot bot commented Oct 21, 2024

🦋 Changeset detected

Latest commit: 18306f8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@farcaster/hubble Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Oct 21, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hub-monorepo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 28, 2024 6:58pm

Copy link
Member

@sds sds left a comment

Choose a reason for hiding this comment

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

Supportive, but there's a way we can structure to not require as much verbosity when providing tags.

return new Tags({ ...this.fields, ...tags.fields });
}

build() {
Copy link
Member

Choose a reason for hiding this comment

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

I believe if you define toString() you won't need to call build() explicitly. This results in slightly DRYer syntax.

this.fields = obj;
}

addTimestamp(timestamp: number) {
Copy link
Member

Choose a reason for hiding this comment

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

Not clear to me from first blush that this needs to be a Farcaster timestamp. Might be worth specifying?

return new Tags({ ...this.fields, errMsg });
}

addMessageFields(message: Message) {
Copy link
Member

Choose a reason for hiding this comment

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

Not a strong opinion, but it might make sense to implement an adapter for the logging instance that instead of passing a Tags object, it accepts an options object which knows how to handle the different types you want to annotate the tags with. For example:

log.info({ fid: ..., timestamp: ..., message: ..., error: ... });

So you would define the info method like:

info(obj?: LogTags, logMsg?: string) {
  const tags = new Tags(obj); // Define logic for special handling of keys
  return this.log.info(tags.build(), logMsg);
}

You would also allow a subset of keys to be provided, e.g.

type LogTags = {
  message?: Partial<Message>; // Can provide any subset of fields for message
  peerId?: string;
  error?: Error;
}

...
class Tags {
  constructor(obj: LogTags) {
    ...
    // Extract `obj` into top-level keys (since `message` etc. will be deeply nested)
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm yeah this is something I was thinking about, but I wasn't sold on it because I wasn't sure how this would evolve if we started using more functionality inside the pino logger library. Then, we'd have to expand the surface area of the adapter object and i could see that becoming a nuisance. It's also hard to remove this sort of library/object once we start using it because it will be used all over the place (we log a lot).

Copy link
Contributor Author

@aditiharini aditiharini Oct 23, 2024

Choose a reason for hiding this comment

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

I think it might make sense to use the type/constructor with all the fields on it rather than the builder pattern though (which is very verbose).

Copy link
Member

Choose a reason for hiding this comment

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

That seems reasonable to me. Supportive!

Copy link

codecov bot commented Oct 28, 2024

Codecov Report

Attention: Patch coverage is 38.09524% with 13 lines in your changes missing coverage. Please review.

Project coverage is 44.17%. Comparing base (1880070) to head (18306f8).
Report is 412 commits behind head on main.

Files with missing lines Patch % Lines
apps/hubble/src/utils/logger.ts 33.33% 7 Missing and 1 partial ⚠️
apps/hubble/src/network/sync/syncHealthJob.ts 0.00% 3 Missing ⚠️
apps/hubble/src/hubble.ts 0.00% 1 Missing ⚠️
apps/hubble/src/network/p2p/gossipNode.ts 80.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2371       +/-   ##
===========================================
- Coverage   74.05%   44.17%   -29.89%     
===========================================
  Files          99      146       +47     
  Lines        9425    26280    +16855     
  Branches     2097     9473     +7376     
===========================================
+ Hits         6980    11610     +4630     
- Misses       2327    12849    +10522     
- Partials      118     1821     +1703     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@aditiharini aditiharini merged commit 5080bbb into farcasterxyz:main Oct 28, 2024
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t-chore Miscellaneous improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants