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

Streaming: replace npmlog with pino & pino-http #27828

Merged
merged 3 commits into from
Jan 18, 2024

Conversation

ThisIsMissEm
Copy link
Contributor

@ThisIsMissEm ThisIsMissEm commented Nov 12, 2023

This implements #27387.

Advice for reviewers: I'd recommend going commit by commit in the files changed tab, as each commit tries to focus in on specific changes (though the change from verifyClient to server.on("upgrade") is bundled with the logger change)

In order to properly initialize the logging, I had to rework the WebSocket Server attachment to the HTTP server to use the upgrade event, which allows for making a decision around authentication before even attempting to upgrade the connection, which is preferrable to verifyClient whose usage is now discouraged.

By using the upgrade event and ws.handleUpgrade method allows us to pass through a logger to the wss.on("connection") event/handler, which can be augmented with logging details specific to websockets.

The log redaction and sanitizer is a little complicated, but essentially prevents access tokens from accidentally ending up in server logs.

The request.id is now managed by the logger, and we don't need request.remoteAddress as the request logger correctly captures the information about the request for logging, so we're able to remove both of those middlewares. Additionally, I replaced the custom CORS middleware with the standard and widely used cors package, which is well tested.

Bundled in is the start of some code cleanup, moving things out to separate files where appropriate (since these things where making it much more difficult to read the code whilst I changed the connection upgrade handling).

In development, if you want, you can pipe to pino-pretty for nicer looking logs, which is available via a development dependency:

$ yarn workspace @mastodon/streaming start | pino-pretty
Screenshot 2023-11-12 at 9 17 57 pm

@ThisIsMissEm
Copy link
Contributor Author

@renchap 9b24627 is what I was talking about over on #24702, it didn't directly break anything when linting ran, but did cause issues; This will also act as a starting point for eventually being able to migrate to ESM, without affecting the reset of the codebase.

Copy link
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

@ThisIsMissEm ThisIsMissEm marked this pull request as ready for review November 14, 2023 21:06
streaming/index.js Outdated Show resolved Hide resolved
Copy link
Contributor

This pull request has resolved merge conflicts and is ready for review.

@mjankowski
Copy link
Contributor

This is definitely easier to process one commit at a time than all together, but even so, it seems like at least:

  • The eslint/tsconfig changes
  • Extracting the metrics to a separate file
  • Move isTruthy to utils
  • Swap out custom impl for cors package

...could all be stand-alone PRs (they all seem like improvements), and may make it easier to review/merge the narrower stated purpose here of the logger change?

@ThisIsMissEm
Copy link
Contributor Author

@mjankowski I've tried in the past to split changes down, but due to the project merge cadence I had to abandon those PRs which were all stacked and a nightmare to maintain / rebase.

I'm trying to avoid that here.

Copy link
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

@ThisIsMissEm
Copy link
Contributor Author

Extracted the CORS change to #28523 & rebased this PR to separate the server initialization changes from the logging changes as best as possible.

Copy link
Contributor

This pull request has resolved merge conflicts and is ready for review.

streaming/logging.js Outdated Show resolved Hide resolved
Copy link
Contributor

github-actions bot commented Jan 2, 2024

This pull request has merge conflicts that must be resolved before it can be merged.

@renchap
Copy link
Sponsor Member

renchap commented Jan 4, 2024

Now that the CORS refactor is merged, could you extract your websocket refactor commit into it's own PR?

Then I think we will be able to review & merge this :)

Thanks a lot for this work!

@ThisIsMissEm
Copy link
Contributor Author

ThisIsMissEm commented Jan 4, 2024

Frustrated reply when sick

Now that the CORS refactor is merged, could you extract your websocket refactor commit into it's own PR?

Then I think we will be able to review & merge this :)

Thanks a lot for this work!

With all due respect, this is the fourth time I've done the work in this branch & tried to get it merged. I've had to abandon 3 other attempts. My time, like yours is extremely limited. I have tried to make the changes reviewable commit by commit:
https://github.com//pull/27828/commits/6533a523d37e5cf5afb7ec012409f141f2064e33
https://github.com//pull/27828/commits/93351e1c6c865c281687dfd190720246de58b8bb

I do not have time or energy to continuously rebase this work, nor do I have the energy as I'm currently reasonably unwell. You either want this change and for the streaming server to improve, or you don't. I know your resources are thin, but mine are likely thinner. I've been trying to get this work merged for almost a year at this point.

Edit: I've hidden this reply because it's not normally how I like to conduct myself; I was incredibly tired, sick and overwhelmed with a bunch of things.

@ThisIsMissEm
Copy link
Contributor Author

There is a follow-on related to this pull request here: #28632

@ThisIsMissEm ThisIsMissEm force-pushed the feat/replace-npmlog-with-pino branch 3 times, most recently from 6e821f0 to b8c5c09 Compare January 16, 2024 00:30
@ThisIsMissEm
Copy link
Contributor Author

ThisIsMissEm commented Jan 16, 2024

@ClearlyClaire @mjankowski okay, I think this should be good to go; I've moved the postgresql and redis type issues into a separate PR (still to finish, because I want to move those out of index.js).

streaming/index.js Outdated Show resolved Hide resolved
@ClearlyClaire
Copy link
Contributor

Apart from the minor naming issues, this looks fine to me, but I'd like another set of eyes on this.

Also, this is a (fairly minor) breaking change that needs to be documented.

Copy link
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

We needed to rework certain method signatures to allow passing the logger in, rather than relying on req.log which may not be defined for websocket connections. The Request ID is now handled by the logger, so that custom middleware has been removed, and the RemoteAddress middleware is no longer necessary with structured logging where each message carries the request ID.
By setting the properties to null, instead of just triggering a `delete` on the entire session object, we ran into typechecking issues.
Copy link

codecov bot commented Jan 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (bd53859) 84.85% compared to head (33d0bb8) 84.86%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #27828   +/-   ##
=======================================
  Coverage   84.85%   84.86%           
=======================================
  Files        1038     1038           
  Lines       28162    28162           
  Branches     4531     4531           
=======================================
+ Hits        23898    23899    +1     
  Misses       3103     3103           
+ Partials     1161     1160    -1     

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

Copy link
Contributor

This pull request has resolved merge conflicts and is ready for review.

@ThisIsMissEm
Copy link
Contributor Author

Also, this is a (fairly minor) breaking change that needs to be documented.

@ClearlyClaire currently the documentation site has absolutely zero mention of logging, and only a little bit on monitoring Mastodon. I did add some documentation about streaming's metrics to the "scaling up" docs, but I don't see a good place for the notice that this is a breaking change in how the streaming server produces logs.

By the looks the changelog should be updated in the release branch.

@ClearlyClaire
Copy link
Contributor

Also, this is a (fairly minor) breaking change that needs to be documented.

@ClearlyClaire currently the documentation site has absolutely zero mention of logging, and only a little bit on monitoring Mastodon. I did add some documentation about streaming's metrics to the "scaling up" docs, but I don't see a good place for the notice that this is a breaking change in how the streaming server produces logs.

By the looks the changelog should be updated in the release branch.

Yes, I meant that as a note to add something in the release notes when the time comes, not as something to do before the PR gets merged.

Copy link
Sponsor Member

@renchap renchap left a comment

Choose a reason for hiding this comment

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

Looks good to me. Quite a hard change to test entirely, but it makes the code cleaner and hopefully provides more useful logs.

@renchap renchap added this pull request to the merge queue Jan 18, 2024
Merged via the queue into mastodon:main with commit 1335083 Jan 18, 2024
31 checks passed
@ThisIsMissEm ThisIsMissEm deleted the feat/replace-npmlog-with-pino branch January 18, 2024 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
streaming Streaming server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants