-
Notifications
You must be signed in to change notification settings - Fork 54
pprof profiling #819
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
pprof profiling #819
Conversation
WalkthroughAdds 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 Changes
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 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. Comment |
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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
Makefile (1)
213-215: Consider adding prerequisites or documentation.The
pproftarget assumes the server is already running onlocalhost:7070with 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/heapinternal/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=falseprovides 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
📒 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
EnablePproffield 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_PPROFenvironment variable.
240-240: LGTM!The default value of
falseensures 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
EnablePproffield is correctly populated usingviper.GetBool(), consistent with other boolean configuration fields.internal/interface/grpc/config.go (1)
22-22: LGTM!The
EnablePproffield is correctly added to the gRPC service configuration.cmd/arkd/main.go (1)
46-46: LGTM!The
EnablePprofconfiguration is properly propagated from the application config to the gRPC service config.internal/interface/grpc/service.go (3)
8-8: LGTM!The
net/http/pprofimport is correctly added to enable profiling endpoints.
140-140: LGTM!The
EnablePprofconfiguration flag is correctly passed to thenewServerfunction.
195-195: LGTM!The
newServersignature is correctly updated to accept thewithPprofparameter.
| @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: |
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.
this is only heap pprof, parametrize it so user can call make pprof heap , make pprof goroutine etc
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.
done but with named parameter make pprof PROFILE=heap
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.
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, ifEnablePprof=truebut no admin port is configured, pprof silently doesn't activate with no user warning. Consider adding a validation check or warning log inConfig.Validate()(internal/interface/grpc/config.go:25) to alert users of this misconfiguration.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 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:.PHONYdeclaration looks good.The
pproftarget is correctly included in the phony targets list with proper alphabetical ordering.internal/interface/grpc/service.go (3)
8-8: LGTM!The
net/http/pprofimport is correctly added to support the pprof profiling endpoints registered later in the file.
195-195: LGTM!The function signature correctly extends
newServerto accept thewithPprofparameter, following the same pattern as the existingwithAppSvcparameter 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
withPprofis 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.
| ## 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) |
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.
🧩 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:" MakefileLength 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.
|
not working for me, need to double check with you @louisinger |
if
ARKD_ENABLE_PPROF=true, pprof will run profiling and server debug endpoints.open the report in UI.
@Kukks @sekulicd @altafan please review
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.