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

Adding a before-call-begins fxevent, to help with troubleshooting #1267

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Groxx
Copy link

@Groxx Groxx commented Mar 24, 2025

What

Adds a "BeforeRun" fxevent.Event, mirroring "Run" but it is called before the provided func is called by Dig, rather than after.

This relies on uber-go/dig#431, so if/when that is approved I will update this PR with a go.mod to pull it in.

Motivation

A recent startup-issue debugging session included logs like this:

{"level":"info","ts":1742516783.6106565,"caller":"fxevent/zap.go:51","message":"provided","constructor":"a", ...
...
{"level":"info","ts":1742516783.6106653,"caller":"fxevent/zap.go:51","message":"provided","constructor":"q", ...
...
{"level":"info","ts":1742516783.6106734,"caller":"fxevent/zap.go:51","message":"provided","constructor":"z", ...
...
{"level":"info","ts":1742516784.7998207,"caller":"fxevent/zap.go:51","message":"run","name":"a", ...
{"level":"info","ts":1742516784.7999196,"caller":"fxevent/zap.go:51","message":"run","name":"b", ...
{"level":"info","ts":1742516784.9025834,"caller":"application.go:123", ...
{"level":"info","ts":1742516789.5810778,"caller":"application.go:234", ... 
{"level":"info","ts":1742516790.740516,"caller":"application.go:345", ...
# EOF

which accurately reflected the logs the app produced: during startup, it was getting OOM-killed after the final log, several seconds after the last Fx-"run" event.

Unfortunately, Fx's Run event is logged after a call completes, so the fxevent log immediately before the application-produced logs tells us almost nothing about what is currently running. That seems... kinda not ideal. I've run into this quite a few times over the years, but I finally decided to do something about it.

So I added a "before run" event, so this log will be much easier to diagnose:

{"level":"info","ts":1742516784.7899196,"caller":"fxevent/zap.go:51","message":"before run","name":"b", ...
{"level":"info","ts":1742516784.7999196,"caller":"fxevent/zap.go:51","message":"run","name":"b", ...
{"level":"info","ts":1742516784.8000196,"caller":"fxevent/zap.go:51","message":"before run","name":"c", ...
{"level":"info","ts":1742516784.9025834,"caller":"application.go:123", ...
# ^ clearly logged during "c"

After a quick attempt in Fx it became pretty clear that it would be difficult or almost impossible to do without changing Dig, so this PR relies on a change to Dig: uber-go/dig#431

Summary by CodeRabbit

  • New Features

    • Enhanced the logging system to provide proactive notifications before key lifecycle events, offering clearer insight into system operations.
  • Tests

    • Modified test cases to validate the improved event logging flow for greater accuracy in monitoring system behavior.
  • Chores

    • Updated a core dependency to support the new logging enhancements.

Copy link

coderabbitai bot commented Mar 24, 2025

Walkthrough

The changes introduce a new event type called "BeforeRun" and update the associated logging and test validations. Test files now assert the presence of "BeforeRun" events before "Run" events. The event package defines the new "BeforeRun" type with its corresponding method, while various logger implementations (console, slog, and zap) and module methods are updated to log "BeforeRun" events. There are no modifications to exported or public entities.

Changes

Files Change Summary
app_test.go, module_test.go Updated test cases to include or replace "Run" events with "BeforeRun" events in the expected event emission sequences.
fxevent/event.go Added new event type BeforeRun with fields Name, Kind, and ModuleName, and implemented its event() method.
fxevent/console.go, fxevent/slog.go, fxevent/zap.go Introduced new case statements to handle BeforeRun events, formatting log messages with event details including name, kind, and module information.
module.go Added pre-execution logging callbacks (using dig.With...BeforeCallback) in the provide, supply, decorate, and replace methods to log "BeforeRun" events prior to the main operations.
go.mod Added a replace directive to redirect dependency from go.uber.org/dig to github.com/Groxx/dig at a specific version.

Sequence Diagram(s)

sequenceDiagram
    participant M as "Module Operation"
    participant CB as "BeforeRun Callback"
    participant L as "Logger (Console/Slog/Zap)"

    M->>CB: Invoke operation (provide/supply/decorate/replace)
    CB->>L: Log BeforeRun event (name, kind, module)
    L-->>CB: Acknowledge log
    CB-->>M: Callback complete, proceed with operation
Loading

Poem

I'm a hopping rabbit, quick and spry,
Watching "BeforeRun" events fly on by.
Logs now whisper every start,
A prelude tune, a rhythmic art.
With each hop, our code sings—oh my!
🐇✨ Happy trails in every byte!


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Groxx Groxx changed the title implementing a before-call-begins callback, to help with troubleshooting Adding a before-call-begins callback, to help with troubleshooting Mar 24, 2025
@Groxx Groxx changed the title Adding a before-call-begins callback, to help with troubleshooting Adding a before-call-begins fxevent, to help with troubleshooting Mar 24, 2025
@Groxx Groxx marked this pull request as ready for review March 24, 2025 22:22
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
go.mod (1)

19-22: Temporary Dependency Replacement for Dig

The addition of the replace directive to point go.uber.org/dig to github.com/Groxx/dig at version v1.18.2-0.20250324200048-72ef97acadd5 is correctly implemented as a temporary fix. The TODO comment reminds us to remove this change once the upstream "before-callback" PR is integrated. Make sure to update or remove this directive promptly when the upstream changes are merged to avoid future conflicts.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6fd1116 and e5d05c0.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (1)
  • go.mod (1 hunks)

Comment on lines +19 to +21

// TODO: remove, points to before-callback branch
replace go.uber.org/dig => github.com/Groxx/dig v1.18.2-0.20250324200048-72ef97acadd5
Copy link
Author

Choose a reason for hiding this comment

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

to be removed before merging

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

Successfully merging this pull request may close these issues.

1 participant