Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@louisinger
Copy link
Collaborator

@louisinger louisinger commented Nov 20, 2025

if ARKD_ENABLE_PPROF=true, pprof will run profiling and server debug endpoints.

make pprof PROFILE=heap

open the report in UI.

@Kukks @sekulicd @altafan please review

Summary by CodeRabbit

  • New Features
    • Added optional profiling via pprof, configurable at runtime.
    • Added a convenient Makefile target to launch/access the pprof web UI on localhost.
    • Dev environment now includes a setting to enable pprof by default.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 20, 2025

Walkthrough

Adds optional pprof profiling: new EnablePprof config flag, wired through the config system and into the gRPC service, an updated server constructor to conditionally register /debug/pprof endpoints, and a Makefile pprof target plus dev env variable to invoke pprof.

Changes

Cohort / File(s) Summary
Configuration
internal/config/config.go, internal/interface/grpc/config.go
Added EnablePprof boolean to Config structs, introduced ENABLE_PPROF key and default (false), and wired viper loading to populate the field.
Service / Runtime
internal/interface/grpc/service.go, cmd/arkd/main.go
Propagated EnablePprof through to grpc service config; updated newServer signature to accept withPprof and conditionally register HTTP pprof handlers (/debug/pprof/*) when enabled; main populates svcConfig.EnablePprof from application config.
Tooling & Environment
Makefile, envs/arkd.dev.env
Added pprof to .PHONY and new pprof target that echoes and runs go tool pprof -http=:8080 http://localhost:7071/debug/pprof/$(PROFILE); added ARKD_ENABLE_PPROF=true to dev env file.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Dev as Developer (Makefile)
  participant Env as Env (arkd.dev.env)
  participant App as cmd/arkd (main)
  participant Config as internal/config (viper)
  participant GRPC as grpc service
  participant HTTP as HTTP mux (/debug/pprof)

  Note over Env,Config: Environment provides ARKD_ENABLE_PPROF
  Dev->>App: make pprof (invokes go tool pprof with PROFILE)
  App->>Config: LoadConfig() (viper reads ENABLE_PPROF)
  Config-->>App: Config{EnablePprof: true/false}
  App->>GRPC: start service (withPprof = cfg.EnablePprof)
  alt EnablePprof == true
    GRPC->>HTTP: register /debug/pprof/* handlers
    Note right of HTTP: pprof endpoints available
  else EnablePprof == false
    Note right of GRPC: pprof endpoints not registered
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review attention:
    • internal/interface/grpc/service.go: verify correct HTTP mux used for pprof registrations and ensure import of net/http/pprof is intentional and side-effects handled.
    • cmd/arkd/main.go: confirm svcConfig.EnablePprof is correctly set and passed to server startup.
    • Makefile: ensure pprof target PROFILE variable and default HTTP/port URL match runtime admin endpoint.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'pprof profiling' clearly summarizes the main change: adding pprof profiling support to the project, which is the primary objective across all modified files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

Copy link
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: 0

🧹 Nitpick comments (2)
Makefile (1)

213-215: Consider adding prerequisites or documentation.

The pprof target assumes the server is already running on localhost:7070 with pprof enabled. Consider adding a comment to clarify this, or add a prerequisite that ensures the server is running.

Example comment:

## pprof: connects to running arkd server and opens pprof UI (requires ARKD_ENABLE_PPROF=true)
pprof:
	@echo "Running pprof..."
	@echo "Note: Ensure arkd is running with ARKD_ENABLE_PPROF=true"
	@go tool pprof -http=:8080 http://localhost:7070/debug/pprof/heap
internal/interface/grpc/service.go (1)

332-345: Consider restricting pprof endpoints to the admin port for better security isolation.

The pprof endpoints expose sensitive runtime information (heap dumps, goroutine stacks, CPU profiles) that could be valuable to attackers. Currently, these endpoints are registered on the main server port rather than the admin port.

When a separate admin port is configured (lines 362-399), consider registering pprof endpoints on the admin mux instead:

-	mux := http.NewServeMux()
-
-	if withPprof {
-		mux.HandleFunc("/debug/pprof/", pprof.Index)
-		mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
-		mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
-		mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
-		mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
-		mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
-		mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
-		mux.Handle("/debug/pprof/allocs", pprof.Handler("allocs"))
-		mux.Handle("/debug/pprof/block", pprof.Handler("block"))
-		mux.Handle("/debug/pprof/mutex", pprof.Handler("mutex"))
-		log.Info("pprof enabled at /debug/pprof/")
-	}
-
 	mux.Handle("/", handler)

And add the same block to the admin mux setup (around line 385):

 		adminHandler := router(adminGrpcServer, adminGrpcGateway)
 		adminMux := http.NewServeMux()
+
+		if withPprof {
+			adminMux.HandleFunc("/debug/pprof/", pprof.Index)
+			adminMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
+			adminMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
+			adminMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
+			adminMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
+			adminMux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
+			adminMux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
+			adminMux.Handle("/debug/pprof/allocs", pprof.Handler("allocs"))
+			adminMux.Handle("/debug/pprof/block", pprof.Handler("block"))
+			adminMux.Handle("/debug/pprof/mutex", pprof.Handler("mutex"))
+			log.Infof("pprof enabled at admin port /debug/pprof/")
+		}
+
 		adminMux.Handle("/", adminHandler)

This would ensure pprof is only accessible via the admin port (typically not publicly exposed) when configured. The default of EnablePprof=false provides good protection, but defense-in-depth suggests restricting sensitive endpoints to the admin interface.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 38b7bf6 and 2453998.

📒 Files selected for processing (6)
  • Makefile (1 hunks)
  • cmd/arkd/main.go (1 hunks)
  • envs/arkd.dev.env (1 hunks)
  • internal/config/config.go (5 hunks)
  • internal/interface/grpc/config.go (1 hunks)
  • internal/interface/grpc/service.go (4 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-08T11:50:25.008Z
Learnt from: altafan
Repo: arkade-os/arkd PR: 733
File: cmd/arkd/commands.go:601-606
Timestamp: 2025-10-08T11:50:25.008Z
Learning: In the market hour configuration (cmd/arkd/commands.go and related files), when roundMinParticipantsCount or roundMaxParticipantsCount is set to 0, the system uses default values from environment variables ARKD_ROUN_MIN/MAX_PARTICIPANTS_COUNT. Therefore, it's acceptable that these values cannot be explicitly cleared back to 0 after being set to a non-zero value.

Applied to files:

  • envs/arkd.dev.env
🧬 Code graph analysis (3)
internal/interface/grpc/service.go (2)
internal/config/config.go (2)
  • EnablePprof (204-204)
  • Config (59-136)
internal/interface/grpc/config.go (1)
  • Config (13-23)
internal/interface/grpc/config.go (1)
internal/config/config.go (1)
  • EnablePprof (204-204)
cmd/arkd/main.go (1)
internal/config/config.go (1)
  • EnablePprof (204-204)
🔇 Additional comments (11)
envs/arkd.dev.env (1)

18-18: LGTM!

The pprof flag is appropriately enabled in the development environment configuration.

internal/config/config.go (5)

122-122: LGTM!

The EnablePprof field is properly added to the Config struct with the correct type.


204-204: LGTM!

The configuration key constant follows the established naming convention and properly maps to the ARKD_ENABLE_PPROF environment variable.


240-240: LGTM!

The default value of false ensures pprof is disabled by default, which is the correct production-safe choice.


281-281: LGTM!

The default value is properly registered with Viper, following the established pattern.


389-389: LGTM!

The EnablePprof field is correctly populated using viper.GetBool(), consistent with other boolean configuration fields.

internal/interface/grpc/config.go (1)

22-22: LGTM!

The EnablePprof field is correctly added to the gRPC service configuration.

cmd/arkd/main.go (1)

46-46: LGTM!

The EnablePprof configuration is properly propagated from the application config to the gRPC service config.

internal/interface/grpc/service.go (3)

8-8: LGTM!

The net/http/pprof import is correctly added to enable profiling endpoints.


140-140: LGTM!

The EnablePprof configuration flag is correctly passed to the newServer function.


195-195: LGTM!

The newServer signature is correctly updated to accept the withPprof parameter.

@echo "Run 'make docker-stop' to shut down the environment when finished."
@echo "Run 'make docker-stop' to shut down the environment when finished."

pprof:
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is only heap pprof, parametrize it so user can call make pprof heap , make pprof goroutine etc

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done but with named parameter make pprof PROFILE=heap

@louisinger louisinger requested a review from sekulicd November 20, 2025 15:09
Copy link
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 (1)
internal/interface/grpc/service.go (1)

140-140: Add validation to warn if pprof is enabled without admin port configured.

The code correctly ensures pprof handlers are only registered when an admin port exists (line 348 if s.config.hasAdminPort()). However, if EnablePprof=true but no admin port is configured, pprof silently doesn't activate with no user warning. Consider adding a validation check or warning log in Config.Validate() (internal/interface/grpc/config.go:25) to alert users of this misconfiguration.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2453998 and 61e623e.

📒 Files selected for processing (2)
  • Makefile (2 hunks)
  • internal/interface/grpc/service.go (5 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
internal/interface/grpc/service.go (2)
internal/config/config.go (2)
  • EnablePprof (204-204)
  • Config (59-136)
internal/interface/grpc/config.go (1)
  • Config (13-23)
🪛 checkmake (0.2.2)
Makefile

[warning] 1-1: Missing required phony target "all"

(minphony)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: unit tests
  • GitHub Check: Build and Scan
  • GitHub Check: integration tests
🔇 Additional comments (4)
Makefile (1)

1-1: .PHONY declaration looks good.

The pprof target is correctly included in the phony targets list with proper alphabetical ordering.

internal/interface/grpc/service.go (3)

8-8: LGTM!

The net/http/pprof import is correctly added to support the pprof profiling endpoints registered later in the file.


195-195: LGTM!

The function signature correctly extends newServer to accept the withPprof parameter, following the same pattern as the existing withAppSvc parameter for conditional feature enablement.


373-385: LGTM! Pprof correctly registered on admin mux.

The pprof endpoints are correctly registered on the admin port when both withPprof is true and an admin port is configured. This addresses the previous review comment to "register with admin mux."

The implementation:

  • Uses standard pprof handler registration patterns
  • Exposes appropriate profiling endpoints (heap, goroutine, allocs, block, mutex, etc.)
  • Logs enablement for observability
  • Appropriately restricts pprof to the admin port rather than the public port

Security note: Pprof endpoints expose sensitive runtime information. Ensure that access to the admin port is properly restricted through network policies, firewalls, or additional authentication mechanisms beyond the TLS configuration.

Comment on lines +213 to +216
## pprof: run pprof tool (e.g. make pprof PROFILE=heap)
pprof:
@echo "Running pprof..."
@go tool pprof -http=:8080 http://localhost:7071/debug/pprof/$(PROFILE)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Missing default PROFILE value; target will fail if not provided.

The target uses $(PROFILE) without a default, so bare make pprof will fail or produce an invalid URL. Additionally, the target silently assumes arkd is running on localhost:7071 with the pprof endpoint enabled.

Provide a sensible default (e.g., heap) and improve the help text to make dependencies explicit:

-## pprof: run pprof tool (e.g. make pprof PROFILE=heap)
+## pprof: run pprof tool (default: heap; e.g. make pprof PROFILE=goroutine)
+## Note: Requires arkd running on localhost:7071 with ARKD_ENABLE_PPROF=true
 pprof:
 	@echo "Running pprof..."
-	@go tool pprof -http=:8080 http://localhost:7071/debug/pprof/$(PROFILE)
+	@go tool pprof -http=:8080 http://localhost:7071/debug/pprof/$(PROFILE:-heap)

Alternative UX (subcommand-style): If sekulicd's earlier feedback favored make pprof heap over make pprof PROFILE=heap, consider this refactor:

pprof:
	@echo "Running pprof (profile: $(or $(PROFILE),heap))..."
	@go tool pprof -http=:8080 http://localhost:7071/debug/pprof/$(or $(PROFILE),heap)

I need to verify the claims in the review comment by examining the actual Makefile and checking the behavior described.


🏁 Script executed:

#!/bin/bash
# Examine the Makefile around lines 213-216 and search for PROFILE references
echo "=== Makefile lines 210-220 ==="
sed -n '210,220p' Makefile

echo -e "\n=== All PROFILE references in Makefile ==="
grep -n "PROFILE" Makefile || echo "No PROFILE references found"

echo -e "\n=== Variable definitions in Makefile ==="
grep -n "^[A-Z_]*\s*\?=" Makefile | head -20 || echo "No default variable definitions found"

echo -e "\n=== Check if PROFILE is used as target parameter elsewhere ==="
grep -n "\.PHONY.*pprof\|pprof:" Makefile

Length of output: 1062


Missing default PROFILE value causes failure; first suggested fix uses incorrect Make syntax.

The core issue is valid: $(PROFILE) has no default, so bare make pprof fails. However, the first suggested fix uses shell parameter expansion syntax $(PROFILE:-heap), which is invalid in Make. Use correct Make syntax instead.

Recommended fix:

-## pprof: run pprof tool (e.g. make pprof PROFILE=heap)
+## pprof: run pprof tool (default: heap; e.g. make pprof PROFILE=goroutine)
 pprof:
 	@echo "Running pprof..."
-	@go tool pprof -http=:8080 http://localhost:7071/debug/pprof/$(PROFILE)
+	@go tool pprof -http=:8080 http://localhost:7071/debug/pprof/$(or $(PROFILE),heap)

Alternatively, define PROFILE ?= heap at the top of the Makefile.

🤖 Prompt for AI Agents
In Makefile around lines 213 to 216, the pprof target fails when PROFILE is not
set because the suggested fix used shell parameter expansion ($(PROFILE:-heap))
which is invalid in Make; fix by providing a Make-level default instead — add a
line like PROFILE ?= heap near the top of the Makefile (or immediately above the
pprof target) so $(PROFILE) expands to "heap" when not provided, and ensure the
pprof recipe keeps using the Make variable $(PROFILE) rather than shell
parameter syntax.

@altafan
Copy link
Collaborator

altafan commented Nov 21, 2025

not working for me, need to double check with you @louisinger

@altafan altafan merged commit 28cd0ac into arkade-os:master Nov 21, 2025
5 checks passed
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.

3 participants