Skip to content

[signal, management, client, cmd] Create shared pprof package and allow pprof port configuration through NB_PPROF_ADDR#4971

Open
ujaandas wants to merge 8 commits intonetbirdio:mainfrom
ujaandas:pprof-configurable-signal-mgmt
Open

[signal, management, client, cmd] Create shared pprof package and allow pprof port configuration through NB_PPROF_ADDR#4971
ujaandas wants to merge 8 commits intonetbirdio:mainfrom
ujaandas:pprof-configurable-signal-mgmt

Conversation

@ujaandas
Copy link
Copy Markdown

@ujaandas ujaandas commented Dec 18, 2025

Describe your changes

Signal and Management servers previously hard‑coded a pprof listener on localhost:6060, which caused port conflicts when multiple services ran in the same namespace (ie; in the same pod or as a NixOS service). This PR refactors both to use the same pprof.go helper pattern already present in Client/Relay.

Does this change build semantics though? To test it, I ran it with -tags pprof. If it does, it shouldn't be a huge change to just slap what pprof.go does directly into the relevant functions.

Issue ticket number and link

Fixes #4923

Stack

Branch: pprof-configurable-signal-mgmt

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

The behavior is consistent with existing Client/Relay profiling logic, so no new user‑facing configuration is introduced beyond the existing NB_PPROF_ADDR.

Summary by CodeRabbit

  • Refactor

    • Centralized profiling support into a shared utility and adjusted initialization across components.
    • Removed automatic local profiling server startup in some services; profiling is now opt-in.
  • Chores

    • Introduced a build-time toggle to exclude profiling when not needed.
    • Changed the default profiling address from localhost:6969 to localhost:6060.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Dec 18, 2025

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Dec 18, 2025

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 90202600-c077-482c-9a2b-4c07d1fd3fbc

📥 Commits

Reviewing files that changed from the base of the PR and between 684fd30 and 309a410.

📒 Files selected for processing (1)
  • util/pprof/pprof_off.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • util/pprof/pprof_off.go

📝 Walkthrough

Walkthrough

Refactors pprof handling: removes ad-hoc HTTP pprof servers from service entry points, centralizes optional pprof initialization in a new util/pprof package (build-tag gated), adds blank imports to enable the package, and changes the default pprof address to localhost:6060.

Changes

Cohort / File(s) Summary
Removed direct pprof startup
management/main.go, signal/cmd/run.go
Deleted inline pprof imports and goroutines that started HTTP pprof servers from service entry points.
Centralized pprof package
util/pprof/pprof.go, util/pprof/pprof_off.go
Renamed package to pprof, made default address localhost:6060, and added a build-constraint file so pprof is opt-in via build tag.
Added side-effect imports
client/cmd/root.go, management/cmd/root.go, relay/cmd/root.go, signal/cmd/root.go
Added blank-identifier imports of github.com/netbirdio/netbird/util/pprof to trigger package init when built with pprof support.
Removed legacy relay pprof file
relay/cmd/pprof.go
Deleted relay-specific pprof init (previously started server using NB_PPROF_ADDR or default 6969).
Minor formatting
signal/main.go
Import block formatting/reordering only; no functional change.

Sequence Diagram(s)

(Skipped — changes are primarily build-tag, import and packaging adjustments rather than new multi-component runtime control flow.)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers

  • pappz
  • bcmmbaga

Poem

🐰
I hopped through imports, quiet and spry,
Gathered profilers from low and high.
One package now hums where many once vied,
Build tags let me nap, side-effects applied.
Happy hops — lightweight and sly!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: creating a shared pprof package and making pprof port configurable through NB_PPROF_ADDR environment variable.
Description check ✅ Passed The description adequately explains the problem (port conflicts when multiple services run in same namespace), the solution (refactor to use shared pprof package), and addresses documentation requirements with proper justification.
Linked Issues check ✅ Passed The PR successfully addresses issue #4923 by making pprof port configurable via NB_PPROF_ADDR environment variable, preventing port conflicts when running multiple services on the same host.
Out of Scope Changes check ✅ Passed All code changes are in scope: refactoring Signal/Management to use shared util/pprof package, removing hardcoded pprof listeners, and adding shared pprof implementation with environment variable configuration.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can disable sequence diagrams in the walkthrough.

Disable the reviews.sequence_diagrams setting to disable sequence diagrams in the walkthrough.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@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: 1

🧹 Nitpick comments (3)
management/cmd/pprof.go (1)

29-29: Remove trailing \n from log format string.

Logrus automatically appends a newline; the explicit \n is unnecessary.

🔎 Apply this diff:
-	log.Infof("listening pprof on: %s\n", listenAddr)
+	log.Infof("listening pprof on: %s", listenAddr)
signal/cmd/pprof.go (2)

29-29: Remove trailing \n from log format string.

Logrus automatically appends a newline; the explicit \n is unnecessary.

🔎 Apply this diff:
-	log.Infof("listening pprof on: %s\n", listenAddr)
+	log.Infof("listening pprof on: %s", listenAddr)

1-33: Note: Intentional code duplication between signal and management pprof implementations.

This file is structurally identical to management/cmd/pprof.go. While this duplication is intentional (each package maintains its own pprof integration), consider whether a shared helper in a common package would reduce maintenance burden if the pprof pattern needs to evolve in the future.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 537151e and 6e4d355.

📒 Files selected for processing (4)
  • management/cmd/pprof.go (1 hunks)
  • management/main.go (0 hunks)
  • signal/cmd/pprof.go (1 hunks)
  • signal/cmd/run.go (1 hunks)
💤 Files with no reviewable changes (1)
  • management/main.go
🔇 Additional comments (4)
signal/cmd/run.go (1)

11-11: LGTM - Clean removal of hardcoded pprof integration.

The removal of the net/http/pprof import and associated pprof startup logic is correct. Pprof functionality is now properly isolated in the build-tagged signal/cmd/pprof.go file.

management/cmd/pprof.go (2)

1-2: LGTM - Correct build tag syntax.

Using both the modern //go:build directive and legacy // +build ensures compatibility across Go versions.


14-17: Consider error handling for pprof server startup in goroutine from init().

The current pattern of using log.Fatalf in a goroutine started from init() is problematic: log.Fatalf is semantically equivalent to a non-recoverable panic without a stack-trace that prevents any deferred functions from running. This means:

  • If the pprof server fails to bind (e.g., port already in use), the goroutine crashes the entire application after main has started, without allowing cleanup
  • Multiple servers built with -tags pprof on the same host without configuring NB_PPROF_ADDR creates a port conflict scenario

Use log.Errorf instead, allowing graceful degradation, or implement synchronous port availability validation during startup with proper error signaling.

signal/cmd/pprof.go (1)

1-2: LGTM - Correct build tag syntax.

Using both the modern //go:build directive and legacy // +build ensures compatibility across Go versions.

Comment thread signal/cmd/pprof.go
Comment thread util/pprof/pprof.go
go pprof(addr)
}

func pprofAddr() string {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think it will be better to centralize the duplicated functions into a shared package

Copy link
Copy Markdown
Collaborator

@mlsmaycon mlsmaycon left a comment

Choose a reason for hiding this comment

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

Thanks @ujaandas . I left a comment around using a shared package

@vooon
Copy link
Copy Markdown

vooon commented Jan 4, 2026

Oh, i also encountered that...
Fixes #5028

vooon added a commit to vooon/my-openwrt-feed that referenced this pull request Jan 9, 2026
@ujaandas ujaandas changed the title [signal, management] Allow pprof port configuration through NB_PPROF_ADDR [signal, management, client, cmd] Create shared pprof package and allow pprof port configuration through NB_PPROF_ADDR Mar 17, 2026
@ujaandas
Copy link
Copy Markdown
Author

Sorry for the delay! I've edited the PR title to reflect the new changes. I've just made a shared package inside util/pprof and imported it in each of the respective packages. Also to note, previously, the client and relay pprofs used the port 6969, and the management and signal used 6060. By using a shared package, we also standardize the pprof port to 6060.

Copy link
Copy Markdown
Contributor

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@util/pprof/pprof_off.go`:
- Around line 5-8: Update the explanatory block comment in
util/pprof/pprof_off.go by correcting the misspelled word "exlude" to "exclude"
so the comment reads: "Otherwise, Go complains that 'build constraints exclude
all Go files'"; keep the rest of the comment intact.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 646bd985-5ac7-48f7-8972-309188dd96e9

📥 Commits

Reviewing files that changed from the base of the PR and between 6e4d355 and 684fd30.

📒 Files selected for processing (8)
  • client/cmd/root.go
  • management/cmd/root.go
  • relay/cmd/pprof.go
  • relay/cmd/root.go
  • signal/cmd/root.go
  • signal/main.go
  • util/pprof/pprof.go
  • util/pprof/pprof_off.go
💤 Files with no reviewable changes (1)
  • relay/cmd/pprof.go
✅ Files skipped from review due to trivial changes (2)
  • signal/cmd/root.go
  • signal/main.go

Comment thread util/pprof/pprof_off.go
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@sonarqubecloud
Copy link
Copy Markdown

@ujaandas ujaandas requested a review from mlsmaycon March 17, 2026 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Management and Signal servers both run pprof on port 6060

4 participants