Repository navigation
feat: implement a pprof server to help profile ncps [backport #1063] - #1066
Conversation
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> (cherry picked from commit 1f9c6d8)
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new feature that enables profiling of the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Related Documentation 1 document(s) may need updating based on files changed in this PR: kalbasit's Space Cache and Storage Configuration FlagsView Suggested Changes@@ -314,6 +314,45 @@
- Otherwise, the NAR is skipped
This allows fsck to focus resources on files that haven't been verified recently, while maintaining confidence that all files are periodically checked. The optimization is especially beneficial for large caches with many chunked NARs, where chunk-level verification can be time-consuming.
+
+## Profiling Configuration
+
+The `ncps serve` command supports optional pprof profiling for performance analysis and troubleshooting.
+
+**Flag:** `--pprof-addr`
+
+**Environment Variable:** `PPROF_ADDR`
+
+**YAML Key:** `pprof.addr`
+
+**Purpose:** Specifies the address to listen on for pprof profiling endpoints (e.g., `:6060`). When set to an empty value (default), pprof is disabled.
+
+**Default value:** `""` (empty, disabled by default)
+
+pprof is a Go profiling tool that helps analyze CPU usage, memory allocation, goroutines, and other runtime metrics for performance troubleshooting. When enabled, pprof endpoints are available at `http://[pprof-addr]/debug/pprof`.
+
+**CLI Example:**
+```bash
+ncps serve --pprof-addr :6060
+```
+
+**YAML Example:**
+```yaml
+pprof:
+ addr: ":6060"
+```
+
+**Accessing pprof endpoints:**
+
+Once the server is running with pprof enabled, you can access the profiling interface at `http://localhost:6060/debug/pprof` (or whatever address you configured). Available endpoints include:
+
+- `/debug/pprof/` - Index page with all available profiles
+- `/debug/pprof/cmdline` - Command line invocation
+- `/debug/pprof/profile` - CPU profile (30-second sample by default)
+- `/debug/pprof/symbol` - Symbol information
+- `/debug/pprof/trace` - Execution trace
+
+**Note:** pprof endpoints should only be exposed in trusted environments, as they can reveal internal application state and consume system resources during profiling operations.
## Summary Table of Storage Flags
| Purpose | CLI Flag | YAML Key | Required |
@@ -326,6 +365,7 @@
| S3 secret key | `--cache-storage-s3-secret-access-key`| `cache.storage.s3.secret-access-key` | Yes** |
| S3 force path style | `--cache-storage-s3-force-path-style` | `cache.storage.s3.force-path-style` | No |
| LRU schedule timezone | `--cache-lru-schedule-timezone` | `cache.lru.schedule.timezone` | No |
+| Profiling address | `--pprof-addr` | `pprof.addr` | No |
* Either local or S3 storage must be configured, not both.
** Required for S3 storage.Note: You must be authenticated to accept/decline updates. |
There was a problem hiding this comment.
Code Review
This pull request introduces a pprof server for profiling, a valuable addition for performance analysis. While the default configuration binding to all network interfaces for pprof is acceptable as per repository rules, the implementation exposes sensitive profiling endpoints without authentication, which is a concern. Additionally, consider setting an IdleTimeout on the new HTTP server for better resource management.
| if pprofAddr := cmd.String("pprof-addr"); pprofAddr != "" { | ||
| pprofMux := http.NewServeMux() | ||
| pprofMux.HandleFunc("/debug/pprof/", netpprof.Index) | ||
| pprofMux.HandleFunc("/debug/pprof/cmdline", netpprof.Cmdline) | ||
| pprofMux.HandleFunc("/debug/pprof/profile", netpprof.Profile) | ||
| pprofMux.HandleFunc("/debug/pprof/symbol", netpprof.Symbol) | ||
| pprofMux.HandleFunc("/debug/pprof/trace", netpprof.Trace) | ||
|
|
||
| pprofServer := &http.Server{ | ||
| Addr: pprofAddr, | ||
| Handler: pprofMux, | ||
| ReadHeaderTimeout: 10 * time.Second, | ||
| } | ||
|
|
||
| go func() { | ||
| if err := pprofServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
| logger.Error().Err(err).Msg("pprof server error") | ||
| } | ||
| }() | ||
|
|
||
| registerShutdown("pprof", pprofServer.Shutdown) | ||
|
|
||
| logger.Info().Str("pprof_addr", pprofAddr).Msg("pprof profiling enabled") | ||
| } |
There was a problem hiding this comment.
The pprof server, when enabled via the pprof-addr flag, exposes sensitive profiling endpoints (e.g., /debug/pprof/heap, /debug/pprof/profile) without authentication, potentially leaking internal application state. While the default configuration binding to all network interfaces is acceptable for disabled-by-default features (as per repository rules), implementing authentication for these sensitive endpoints is still highly recommended. Additionally, for better resource management and server hardening, consider setting an IdleTimeout on the http.Server to prevent idle connections from lingering.
pprofServer := &http.Server{
Addr: pprofAddr,
Handler: pprofMux,
ReadHeaderTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
}References
- For optional debugging features (e.g., pprof) that are disabled by default, it is acceptable for the default configuration to bind to all network interfaces. The risk is considered low as the user must explicitly enable the feature.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## release-0.9 #1066 +/- ##
===========================================
Coverage 5.04% 5.04%
===========================================
Files 6 6
Lines 436 436
===========================================
Hits 22 22
Misses 410 410
Partials 4 4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Bot-based backport to
release-0.9, triggered by a label in #1063.