[management] add monitoring for nmap update source#6036
Conversation
📝 WalkthroughWalkthroughIntroduces a Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@management/internals/modules/peers/manager.go`:
- Line 182: The call to m.accountManager.UpdateAccountPeers(ctx, accountID,
types.UpdateReason{Resource: types.UpdateResourcePeer, Operation:
types.UpdateOperationDelete}) must be gated so it only runs when at least one
peer was actually deleted; modify the surrounding peer deletion logic (the loop
or the function that removes peers) to produce a deletion count or boolean
(e.g., removedCount or deletedAny) and call m.accountManager.UpdateAccountPeers
only if removedCount > 0 (or deletedAny == true), otherwise skip the update.
In `@management/server/user.go`:
- Line 678: The call to am.UpdateAccountPeers currently always uses
UpdateOperationUpdate but SaveOrAddUsers can insert new users when
addIfNotExists=true; modify the flow around SaveOrAddUsers to detect whether new
users were created (e.g., via its return value or an inserted-count flag) and
call am.UpdateAccountPeers with types.UpdateReason{Resource:
types.UpdateResourceUser, Operation: types.UpdateOperationCreate} when inserts
occurred, otherwise keep UpdateOperationUpdate; reference SaveOrAddUsers,
addIfNotExists, and UpdateAccountPeers/ types.UpdateOperationCreate to locate
and implement the change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 0e6fdd6b-e393-4e74-bcce-1c3da7102305
📒 Files selected for processing (28)
management/internals/controllers/network_map/controller/controller.gomanagement/internals/controllers/network_map/interface.gomanagement/internals/controllers/network_map/interface_mock.gomanagement/internals/modules/peers/ephemeral/manager/ephemeral_test.gomanagement/internals/modules/peers/manager.gomanagement/internals/modules/reverseproxy/service/manager/l4_port_test.gomanagement/internals/modules/reverseproxy/service/manager/manager.gomanagement/internals/modules/reverseproxy/service/manager/manager_test.gomanagement/internals/modules/zones/manager/manager.gomanagement/internals/modules/zones/records/manager/manager.gomanagement/server/account.gomanagement/server/account/manager.gomanagement/server/account/manager_mock.gomanagement/server/dns.gomanagement/server/group.gomanagement/server/mock_server/account_mock.gomanagement/server/nameserver.gomanagement/server/networks/manager.gomanagement/server/networks/resources/manager.gomanagement/server/networks/routers/manager.gomanagement/server/peer.gomanagement/server/peer_test.gomanagement/server/policy.gomanagement/server/posture_checks.gomanagement/server/route.gomanagement/server/telemetry/accountmanager_metrics.gomanagement/server/types/update_reason.gomanagement/server/user.go
| } | ||
|
|
||
| m.accountManager.UpdateAccountPeers(ctx, accountID) | ||
| m.accountManager.UpdateAccountPeers(ctx, accountID, types.UpdateReason{Resource: types.UpdateResourcePeer, Operation: types.UpdateOperationDelete}) |
There was a problem hiding this comment.
Gate peer update trigger on actual deletions.
Line 182 runs even when all peers were skipped (e.g., not found/connected), which can inflate delete-trigger monitoring and perform unnecessary account peer updates.
Suggested fix
func (m *managerImpl) DeletePeers(ctx context.Context, accountID string, peerIDs []string, userID string, checkConnected bool) error {
+ deletedAny := false
for _, peerID := range peerIDs {
var eventsToStore []func()
+ deletedInTx := false
err = m.store.ExecuteInTransaction(ctx, func(transaction store.Store) error {
...
if err = transaction.DeletePeer(ctx, accountID, peerID); err != nil {
return err
}
+ deletedInTx = true
...
return nil
})
if err != nil {
...
continue
}
+ if !deletedInTx {
+ continue
+ }
+ deletedAny = true
...
}
- m.accountManager.UpdateAccountPeers(ctx, accountID, types.UpdateReason{Resource: types.UpdateResourcePeer, Operation: types.UpdateOperationDelete})
+ if deletedAny {
+ m.accountManager.UpdateAccountPeers(ctx, accountID, types.UpdateReason{
+ Resource: types.UpdateResourcePeer,
+ Operation: types.UpdateOperationDelete,
+ })
+ }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@management/internals/modules/peers/manager.go` at line 182, The call to
m.accountManager.UpdateAccountPeers(ctx, accountID, types.UpdateReason{Resource:
types.UpdateResourcePeer, Operation: types.UpdateOperationDelete}) must be gated
so it only runs when at least one peer was actually deleted; modify the
surrounding peer deletion logic (the loop or the function that removes peers) to
produce a deletion count or boolean (e.g., removedCount or deletedAny) and call
m.accountManager.UpdateAccountPeers only if removedCount > 0 (or deletedAny ==
true), otherwise skip the update.
| return nil, fmt.Errorf("failed to increment network serial: %w", err) | ||
| } | ||
| am.UpdateAccountPeers(ctx, accountID) | ||
| am.UpdateAccountPeers(ctx, accountID, types.UpdateReason{Resource: types.UpdateResourceUser, Operation: types.UpdateOperationUpdate}) |
There was a problem hiding this comment.
Use create reason when SaveOrAddUsers inserts new users.
Line 678 always reports UpdateOperationUpdate, but this path can create users (addIfNotExists=true). That will skew the new trigger metrics for user-create flows.
Suggested fix
- am.UpdateAccountPeers(ctx, accountID, types.UpdateReason{Resource: types.UpdateResourceUser, Operation: types.UpdateOperationUpdate})
+ op := types.UpdateOperationUpdate
+ if hasNewUsers {
+ op = types.UpdateOperationCreate
+ }
+ am.UpdateAccountPeers(ctx, accountID, types.UpdateReason{
+ Resource: types.UpdateResourceUser,
+ Operation: op,
+ })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@management/server/user.go` at line 678, The call to am.UpdateAccountPeers
currently always uses UpdateOperationUpdate but SaveOrAddUsers can insert new
users when addIfNotExists=true; modify the flow around SaveOrAddUsers to detect
whether new users were created (e.g., via its return value or an inserted-count
flag) and call am.UpdateAccountPeers with types.UpdateReason{Resource:
types.UpdateResourceUser, Operation: types.UpdateOperationCreate} when inserts
occurred, otherwise keep UpdateOperationUpdate; reference SaveOrAddUsers,
addIfNotExists, and UpdateAccountPeers/ types.UpdateOperationCreate to locate
and implement the change.
Release artifactsBuilt for PR head
GHCR images (amd64)
This comment is updated by the Release workflow. Artifact links expire according to the workflow retention policy. |



Describe your changes
Issue ticket number and link
Stack
Checklist
Documentation
Select exactly one:
Docs PR URL (required if "docs added" is checked)
Paste the PR link from https://github.com/netbirdio/docs here:
https://github.com/netbirdio/docs/pull/__
Summary by CodeRabbit