Skip to content

feat: implement a pprof server to help profile ncps - #1063

Merged
kalbasit merged 5 commits into
mainfrom
feat-implement-a-pprof-server-to
Mar 11, 2026
Merged

kalbasit merged 5 commits into
mainfrom
feat-implement-a-pprof-server-to

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

No description provided.

@kalbasit

kalbasit commented Mar 11, 2026 •

Copy link
Copy Markdown
Owner Author

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. enhancement New feature or request go Pull requests that update go code labels Mar 11, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 significant enhancement for debugging and performance analysis by integrating a pprof profiling server into the ncps application. This allows developers to easily collect and analyze runtime profiling data, such as CPU, memory, and goroutine usage, to identify and resolve performance bottlenecks.

Highlights

  • Pprof Server Implementation: A new HTTP server has been implemented to expose net/http/pprof endpoints, allowing for detailed profiling of ncps applications.
  • Configuration for Pprof Address: A new command-line flag, --pprof-addr, and corresponding environment variable, PPROF_ADDR, were added to specify the listening address for the pprof server. Leaving this flag empty disables the pprof server.
Changelog
  • pkg/ncps/serve.go
    • Imported the net/http/pprof package to enable profiling capabilities.
    • Added a new pprof-addr command-line flag for configuring the pprof server's listening address.
    • Implemented the setup and startup logic for the pprof HTTP server, registering standard pprof handlers.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@dosubot

dosubot Bot commented Mar 11, 2026 •

Copy link
Copy Markdown

Documentation Updates

1 document(s) were updated by changes in this PR:

OpenTelemetry Instrumentation and Metrics
View Changes
@@ -70,6 +70,49 @@
 
 The `/metrics` endpoint is available on the server's HTTP address (default `:8501`) and can be scraped by Prometheus or queried directly for metrics [[source]](https://github.com/kalbasit/ncps/blob/b7653d4303670bb88fd34b8bf82ed3aade09885d/cmd/serve.go#L255-L529).
 
+### pprof Profiling Support
+The pprof profiling server enables CPU and memory profiling for performance analysis and troubleshooting. Profiling is disabled by default for security and must be explicitly enabled using the `--pprof-addr` flag.
+
+When enabled, pprof exposes standard profiling endpoints on a separate HTTP server:
+
+- `/debug/pprof/` - Index page listing available profiles
+- `/debug/pprof/cmdline` - Command-line invocation of the program
+- `/debug/pprof/profile` - CPU profile (30-second sampling by default)
+- `/debug/pprof/symbol` - Symbol lookup for addresses
+- `/debug/pprof/trace` - Execution trace
+
+Example:
+
+```sh
+ncps serve --pprof-addr=:6060
+```
+
+The pprof endpoints are then available at `http://localhost:6060/debug/pprof/`. You can use Go's pprof tool to analyze profiles:
+
+```sh
+# CPU profile
+go tool pprof http://localhost:6060/debug/pprof/profile
+
+# Memory profile
+go tool pprof http://localhost:6060/debug/pprof/heap
+```
+
+#### Helm Chart Configuration
+When deploying with the Helm chart, configure pprof using:
+
+```yaml
+config:
+  observability:
+    pprof:
+      enabled: true
+      addr: ":6060"
+```
+
+#### Security Considerations
+**Important:** The pprof server should typically be disabled in production environments or only exposed on internal networks. Profiling endpoints can expose sensitive runtime information and consume significant resources during profile collection. The default configuration (empty `--pprof-addr` value) keeps pprof disabled for security.
+
+Consider restricting access to the pprof port using network policies or firewall rules, and only enable profiling temporarily when investigating performance issues.
+
 ### Metrics Collected for NAR and NarInfo File Serving
 A new observable gauge metric is available:
 
@@ -80,17 +123,18 @@
 ### Tracing Spans for NAR and NarInfo Operations
 In addition to tracing spans for NAR and NarInfo operations, lock operations are now instrumented with OpenTelemetry metrics. Both local and distributed (Redis-based) lock implementations record metrics for acquisition attempts, contention, failures (with detailed reasons), retry attempts, and lock hold durations. While lock operations do not create tracing spans, the emitted metrics provide fine-grained observability into lock behavior, including contention events and failure diagnostics for distributed systems.
 
-### Enabling and Accessing Metrics for Observability
-To enable tracing and metrics:
+### Enabling and Accessing Observability Features
+To enable tracing, metrics, and profiling:
 
 1. Start the server with `--otel-enabled` to activate OpenTelemetry tracing, metrics, and logging.
 2. Optionally, set `--otel-grpc-url` to export telemetry to a remote OTLP collector.
 3. Add `--prometheus-enabled` to expose Prometheus metrics at `/metrics`.
+4. Add `--pprof-addr=:6060` to enable pprof profiling endpoints (optional, disabled by default).
 
 Example:
 
 ```sh
-ncps serve --otel-enabled --prometheus-enabled
+ncps serve --otel-enabled --prometheus-enabled --pprof-addr=:6060
 ```
 
 To access metrics, configure Prometheus to scrape the `/metrics` endpoint (default `http://localhost:8501/metrics`). Example Prometheus scrape config:
@@ -109,5 +153,6 @@
 - Use the `--otel-grpc-url` flag to export telemetry to a centralized collector for production deployments.
 - Monitor the `result` and `status` attributes on NAR and NarInfo metrics to identify cache misses and errors.
 - Ensure Prometheus is configured to scrape the correct endpoint and port.
+- Keep pprof disabled in production unless actively troubleshooting performance issues, and restrict access to the pprof port to internal networks only.
 
 For further customization or troubleshooting, refer to the CLI help (`ncps --help`) and review logs for OpenTelemetry initialization messages.

How did I do? Any feedback?  Join Discord

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a pprof server to aid in profiling ncps. The implementation adds a --pprof-addr flag to enable the server on a specified address. The changes are well-contained and the feature is opt-in, which is good practice. I've suggested a simplification to the pprof handler registration to make the code more concise and maintainable.

Comment thread pkg/ncps/serve.go Resolved
@codecov

codecov Bot commented Mar 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 5.04%. Comparing base (6cdc72d) to head (4addc3e).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #1063   +/-   ##
=====================================
  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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Mar 11, 2026
@kalbasit
kalbasit force-pushed the feat-implement-a-pprof-server-to branch from 8ecf9af to d152e09 Compare March 11, 2026 06:06
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Mar 11, 2026
@kalbasit

Copy link
Copy Markdown
Owner Author

/gemini review

@kalbasit kalbasit added the backport release-0.9 Backport PR automatically label Mar 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a pprof server to ncps for performance profiling. However, the current implementation lacks authentication and uses an insecure default binding in the Helm chart, which could lead to unauthorized access to sensitive profiling data and potential Denial of Service attacks. Additionally, consider improving the documentation for port-forwarding to make it more robust for users with different Helm release names.

Comment thread pkg/ncps/serve.go Resolved
Comment thread charts/ncps/values.yaml Resolved
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

@kalbasit
kalbasit merged commit 1f9c6d8 into main Mar 11, 2026
11 checks passed
@kalbasit
kalbasit deleted the feat-implement-a-pprof-server-to branch March 11, 2026 06:22
kalbasit added a commit that referenced this pull request Mar 11, 2026
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
(cherry picked from commit 1f9c6d8)
@kalbasit

Copy link
Copy Markdown
Owner Author

Successfully created backport PR for release-0.9:

@github-actions

Copy link
Copy Markdown
Contributor

kalbasit added a commit that referenced this pull request Mar 11, 2026
…1066)

Bot-based backport to `release-0.9`, triggered by a label in #1063.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
kalbasit added a commit that referenced this pull request Jun 8, 2026
## Summary

The `[Unreleased]` section was populated in #1309 but skewed toward the
recent CDC-drain, in-flight-staging, and Ent-migration work, omitting
several user-facing features and fixes that also landed since v0.9.4.
This backfills the missing notable changes, each cross-referenced by PR.

**Added**
- `ncps fsck` command + Helm fsck CronJob (#975, #996, #998, #999,
#1000, #1004, #1006, #1176)
- Closure pinning HTTP endpoints `POST`/`DELETE /pin/{hash}.narinfo`,
`GET /pins` (#1108)
- pprof server via `--pprof-addr` (#1063)
- CDC lazy chunking as a feature + `--cache-cdc-lazy-cleanup-schedule` +
recovery cron (#1082, #1083, #1089, #1096)
- Helm: auto `GOMEMLIMIT` (#1061), configurable temp volume /
`volumeClaimTemplate` (#1074, #1077), SQLite `Recreate` strategy (#1012)
- Startup storage reporting + deprecated-narinfo warning (#959, #961)
- `docker-dev` image (#1097)

**Fixed**
- `store/tmp` no longer wiped on startup (#1313)
- `nix-hash` NAR verification (#1101)
- Prometheus counters primed at startup (#1337, #1343)
- 404 on un-reassemblable chunked NARs (#1319), purged narinfo no longer
500 (#1318), fail-fast tmp validation (#1086)
- Further CDC/cache reliability fixes (#1317, #1346, #1341, #1342,
#1331, #1347)

**Changed**
- CDC config now persisted in the DB config table (#971)

Dependency bumps, CI/test-only changes, and the internal Ent-conversion
PRs are intentionally excluded; the existing Ent-migration entry already
covers that work.

## Test plan

- [x] `nix fmt` reports no changes (docs-only edit)
- [x] CHANGELOG section structure intact
(Added/Fixed/Changed/Removed/Migration/CI)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport release-0.9 Backport PR automatically enhancement New feature or request go Pull requests that update go code size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant