-
Notifications
You must be signed in to change notification settings - Fork 108
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
fix(zetaclient): distinguish between known and connected peers #3208
Conversation
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the 📝 Walkthrough📝 WalkthroughWalkthroughThe pull request introduces enhancements to the telemetry system by modifying the Changes
Possibly related PRs
Suggested labels
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #3208 +/- ##
===========================================
- Coverage 62.43% 62.37% -0.07%
===========================================
Files 428 428
Lines 30404 30434 +30
===========================================
Hits 18982 18982
- Misses 10576 10606 +30
Partials 846 846
|
42c28ca
to
c7bbd4a
Compare
c7bbd4a
to
3d164e6
Compare
3d164e6
to
69ca4a8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caution
Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments.
Actionable comments posted: 4
🧹 Outside diff range and nitpick comments (4)
zetaclient/metrics/telemetry.go (2)
46-46
: Add test coverage for the constructorThe initialization of
knownPeers
needs test coverage to ensure proper initialization of the TelemetryServer.Would you like me to help generate unit tests for the constructor?
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 46-46: zetaclient/metrics/telemetry.go#L46
Added line #L46 was not covered by tests
72-82
: Consider defensive copying in GetKnownPeersWhile the implementation is thread-safe, returning a direct slice reference could lead to potential data races if the caller modifies the slice. Consider returning a copy.
func (t *TelemetryServer) GetKnownPeers() []peer.AddrInfo { t.mu.Lock() defer t.mu.Unlock() - return t.knownPeers + result := make([]peer.AddrInfo, len(t.knownPeers)) + copy(result, t.knownPeers) + return result }🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 72-75: zetaclient/metrics/telemetry.go#L72-L75
Added lines #L72 - L75 were not covered by tests
[warning] 78-81: zetaclient/metrics/telemetry.go#L78-L81
Added lines #L78 - L81 were not covered by testszetaclient/tss/healthcheck.go (2)
107-118
: Add Unit Tests forconnectedPeersCounter
FunctionThe
connectedPeersCounter
function is crucial for tracking connected peers accurately. However, it currently lacks unit tests, as indicated by the static analysis tools. Implementing tests will enhance code reliability and ensure the function behaves as expected.Would you like assistance in creating unit tests for this function?
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 107-117: zetaclient/tss/healthcheck.go#L107-L117
Added lines #L107 - L117 were not covered by tests
99-102
: Add Unit Tests forknownPeersCounter
FunctionSimilarly, the
knownPeersCounter
function is not covered by unit tests. To maintain high code quality and confidence in metric tracking, consider adding unit tests for this function.Would you like assistance in creating unit tests for this function?
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 99-99: zetaclient/tss/healthcheck.go#L99
Added line #L99 was not covered by tests
[warning] 102-102: zetaclient/tss/healthcheck.go#L102
Added line #L102 was not covered by tests
🛑 Comments failed to post (4)
zetaclient/tss/service.go (1)
51-51: 💡 Codebase verification
Missing implementation of SetKnownPeers in interface implementations
Based on the verification results:
- The
Telemetry
interface requiresSetKnownPeers
method- Only
TelemetryServer
inzetaclient/metrics/telemetry.go
implements this method- Other potential implementations in
zetaclient/tss/service.go
andzetaclient/tss/healthcheck.go
lack this required methodPlease implement the
SetKnownPeers
method in all structs that implement theTelemetry
interface to maintain interface compliance.🔗 Analysis chain
LGTM! Verify implementation consistency.
The addition of
SetKnownPeers
method to theTelemetry
interface aligns well with the PR objective of distinguishing between known and connected peers. The method signature is consistent with the existingSetConnectedPeers
method.Let's verify that all implementations of the
Telemetry
interface have been updated:🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Find all implementations of the Telemetry interface and verify they implement SetKnownPeers # Find all Go files that might implement Telemetry interface ast-grep --pattern 'type $_ struct { $$$ } func ($_ $_) SetP2PID($_) { $$$ } func ($_ $_) SetConnectedPeers($_ []peer.AddrInfo) { $$$ }' # Find all implementations of SetKnownPeers ast-grep --pattern 'func ($_ $_) SetKnownPeers(peers []peer.AddrInfo) { $$$ }'Length of output: 527
Script:
#!/bin/bash # Let's find all files that might contain Telemetry interface implementations rg -l "type.*struct.*SetP2PID|SetConnectedPeers" --type go # Let's also check the interface definition to understand all required methods rg "type Telemetry interface" -A 10 --type goLength of output: 744
Script:
#!/bin/bash # Let's check the implementation of SetKnownPeers in all potential implementation files rg "func.*SetKnownPeers" -B 2 -A 5 zetaclient/tss/service.go zetaclient/tss/healthcheck.go # Also check for any mock implementations rg "type.*Mock.*Telemetry" -A 20 --type goLength of output: 137
zetaclient/metrics/telemetry.go (2)
301-306:
⚠️ Potential issueFix incorrect error message in connectedPeersHandler
The error message incorrectly refers to "known peers" in the connected peers handler.
- t.logger.Error().Err(err).Msg("Failed to marshal known peers") + t.logger.Error().Err(err).Msg("Failed to marshal connected peers")📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.t.logger.Error().Err(err).Msg("Failed to marshal connected peers") http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Fprintf(w, "%s", string(data)) }
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 301-305: zetaclient/metrics/telemetry.go#L301-L305
Added lines #L301 - L305 were not covered by tests
308-313: 🛠️ Refactor suggestion
Set Content-Type header for JSON response
The handler returns JSON data but doesn't set the appropriate Content-Type header.
func (t *TelemetryServer) knownPeersHandler(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) peers := t.GetKnownPeers() data, err := json.Marshal(peers) if err != nil { t.logger.Error().Err(err).Msg("Failed to marshal known peers") http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Fprintf(w, "%s", string(data)) }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.func (t *TelemetryServer) knownPeersHandler(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) peers := t.GetKnownPeers() data, err := json.Marshal(peers) if err != nil { t.logger.Error().Err(err).Msg("Failed to marshal known peers")
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 308-313: zetaclient/metrics/telemetry.go#L308-L313
Added lines #L308 - L313 were not covered by testszetaclient/tss/healthcheck.go (1)
99-102:
⚠️ Potential issueIncorrect Metric Updated in
knownPeersCounter
In the
knownPeersCounter
function, the metricNumConnectedPeersMetric
is being updated instead ofNumKnownPeersMetric
. This misalignment could lead to inaccurate tracking of known peers versus connected peers.Please apply the following fix to ensure the correct metric is updated:
knownPeersCounter := func(_ context.Context, _ *ticker.Ticker) error { peers := server.GetKnownPeers() - p.NumConnectedPeersMetric.Set(float64(len(peers))) + p.NumKnownPeersMetric.Set(float64(len(peers))) p.Telemetry.SetKnownPeers(peers) return nil }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.knownPeersCounter := func(_ context.Context, _ *ticker.Ticker) error { peers := server.GetKnownPeers() p.NumKnownPeersMetric.Set(float64(len(peers))) p.Telemetry.SetKnownPeers(peers)
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 99-99: zetaclient/tss/healthcheck.go#L99
Added line #L99 was not covered by tests
[warning] 102-102: zetaclient/tss/healthcheck.go#L102
Added line #L102 was not covered by tests
"Known peers" should not be exposed as "connected peers". They are two completely separate things which should not be mixed to avoid confusion.
Add new
/knownpeers
endpoint which will expose this info. Expose connected peers in/connectedpeers
.Summary by CodeRabbit
New Features
/knownpeers
to retrieve and manage known peers.Bug Fixes
Documentation