Skip to content

HYPERFLEET-1492 - feat: add WithStackTrace option for selective stack trace capture - #5

Open
kuudori wants to merge 2 commits into
openshift-hyperfleet:mainfrom
kuudori:HYPERFLEET-1492-configurable-stack-trace
Open

HYPERFLEET-1492 - feat: add WithStackTrace option for selective stack trace capture#5
kuudori wants to merge 2 commits into
openshift-hyperfleet:mainfrom
kuudori:HYPERFLEET-1492-configurable-stack-trace

Conversation

@kuudori

@kuudori kuudori commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds WithStackTrace(func(ctx context.Context, r slog.Record) bool) Option to NewHandler. The filter is consulted only at slog.LevelError or above; it decides whether that record gets a stack_trace field.
  • No implicit default: with no filter registered, no record ever gets a stack trace, regardless of level.
  • Fixes a real gap: the shared handler currently attaches a stack trace to every ERROR+ record unconditionally. That matches Sentinel's original (pre-extraction) logger, so it's not a regression for Sentinel - but it doesn't generalize. The Adapter's own logger classifies some errors as expected/routine (K8s NotFound/Conflict, network blips, HyperFleet API 4xx/5xx) and skips stack traces for those specifically, while still logging at ERROR level. That's a per-log-call decision, not a per-handler-instance one, so a simple on/off flag can't express it without forcing every such consumer to hand-write a full wrapping slog.Handler. A caller-supplied filter, consulted directly inside Handle(), solves this with no extra plumbing required from consumers.
  • Also adds the Makefile and pinned tools/go.mod this repo's hyperfleet-hooks pre-commit config (hyperfleet-gofmt, hyperfleet-golangci-lint, hyperfleet-go-vet) requires - both were missing, so the pre-commit hooks could never actually run in this repo.
  • Fixes a pre-existing errcheck lint violation in pool.Get(), surfaced once golangci-lint could actually run via make lint.

Test plan

  • go build ./...
  • go vet ./...
  • go test ./... -race -count=1 (40/40 passing)
  • make lint (0 issues via pinned tools/go.mod golangci-lint)
  • gofmt -l . clean
  • New tests cover: no-filter-never-captures, filter-returns-false, filter-returns-true, filter-not-consulted-below-error, filter-receives-record-for-classification, and a regression test proving the filter survives .With()/.WithGroup() handler chaining (verified this test actually fails without the corresponding fix in WithAttrs/WithGroup)

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added configurable, opt-in stack-trace capture for error-level logs through WithStackTrace.
    • Stack traces can be selectively captured based on logging context and record contents.
    • Configuration remains active when creating derived loggers.
  • Bug Fixes

    • Improved resilience when handling unexpected pooled values.
  • Documentation

    • Updated examples and API documentation to reflect opt-in stack-trace behavior.
  • Chores

    • Added development commands for formatting, linting, validation, race-enabled tests, and pre-commit setup.

Walkthrough

The handler captures error-level stack traces only when WithStackTrace is configured and accepts the record. Derived handlers preserve the filter. Pool retrieval creates a valid value after a type mismatch. Tests cover filtering, severity, record inspection, chaining, sanitization, and pool recovery. Documentation and Go tooling commands were updated.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 11
✅ Passed checks (11 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: adding the WithStackTrace option for selective stack-trace capture.
Description check ✅ Passed The description directly explains the new option, behavior, tests, tooling changes, and lint fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Sec-02: Secrets In Log Output ✅ Passed Eligible non-test/non-example files contain no log field or interpolated string for token, password, credential, or secret; no CWE-532 finding was detected.
No Hardcoded Secrets ✅ Passed No secret-shaped additions found: no credential URLs, private keys, or literal credential assignments; 976 long base64 values are go.sum h1 checksums. No CWE/CVE applies.
No Weak Cryptography ✅ Passed No crypto imports or banned primitives appear in the PR diff or tracked Go code; no secret/HMAC comparisons or custom crypto were found (CWE-327, CWE-208).
No Injection Vectors ✅ Passed No CWE-89/78/79/502 patterns were added: no SQL query construction, exec.Command, template.HTML, or yaml.Unmarshal calls. Existing fmt.Sprintf calls only format errors and stack frames.
No Privileged Containers ✅ Passed No Kubernetes/OpenShift manifests, Helm templates, or Dockerfiles are tracked, and no privileged-container settings were found in the complete tree or PR diff.
No Pii Or Sensitive Data In Logs ✅ Passed No new production logging calls or sensitive literals were found; WithStackTrace adds only filtered function/file/line frames and placeholder IDs. No CWE-532 exposure is evident.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ Simplify code
  • Create PR with simplified code

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@handler.go`:
- Around line 250-255: Update the generic pool method Get so a type mismatch
does not return the zero value when that value may be nil and later
dereferenced. For pcsPool callers such as captureStackTrace, allocate and return
a fresh valid pooled value of the expected type on mismatch, while preserving
the existing successful type-assertion path.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 5a288180-ff3e-4dd5-8de4-8cd2b4400cb5

📥 Commits

Reviewing files that changed from the base of the PR and between c3cad87 and 1aa0889.

⛔ Files ignored due to path filters (1)
  • tools/go.sum is excluded by !**/*.sum, !**/go.sum
📒 Files selected for processing (6)
  • Makefile
  • README.md
  • doc.go
  • handler.go
  • handler_test.go
  • tools/go.mod
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • openshift-hyperfleet/architecture (manual)
  • openshift-hyperfleet/hyperfleet-api (manual)
  • openshift-hyperfleet/hyperfleet-sentinel (manual)
  • openshift-hyperfleet/hyperfleet-adapter (manual)
  • openshift-hyperfleet/hyperfleet-broker (manual)

Comment thread handler.go Outdated
… trace capture

The shared handler previously attached a stack trace to every ERROR-level
record unconditionally, with no way to opt out. Consumers like the Adapter
classify some errors as expected (K8s NotFound/Conflict, network blips,
HyperFleet API 4xx/5xx) and want to skip stack traces for those specifically,
which is a per-log-call decision that a construction-time flag can't express.
WithStackTrace registers a filter, consulted only at slog.LevelError or
above, that decides whether a given record gets a stack trace - with no
implicit default, so each consumer opts in explicitly.

Also adds the Makefile and pinned tools/go.mod required by this repo's
hyperfleet-hooks pre-commit configuration (gofmt/lint/go-vet targets),
which were missing, and fixes a pre-existing errcheck lint violation in
pool.Get() surfaced once golangci-lint could actually run via make lint.
@kuudori
kuudori force-pushed the HYPERFLEET-1492-configurable-stack-trace branch from 1aa0889 to 1a6f127 Compare August 10, 2026 16:38
Comment thread Makefile
Comment thread handler_test.go Outdated
Comment thread handler.go Outdated
Comment thread handler_test.go
}

// alwaysCaptureStackTrace is a WithStackTrace filter that always returns true.
func alwaysCaptureStackTrace(context.Context, slog.Record) bool { return true }

@pnguyen44 pnguyen44 Aug 10, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

alwaysCaptureStackTrace is duplicated across 6+ tests in this file. Consider exporting it (e.g. AlwaysCaptureStackTrace) so it's reusable outside the package too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'd leave it unexported for now, since there's no package that would use it outside this one. It's just a helper for the stack trace tests here 🤔

Comment thread README.md
Comment thread handler.go Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Makefile`:
- Line 10: Fix the shell-injection risk in the help recipe around the awk
invocation and the GO-related recipes at the referenced lines: do not
interpolate MAKEFILE_LIST or other externally controllable variables unquoted
into shell commands, and prevent option injection by using a fixed trusted
makefile or safely escaped paths with an explicit awk option terminator. Keep GO
restricted to a validated executable path and invoke it as one separately quoted
argument.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 5d88fbc6-e853-47c2-93f0-d16319628eb6

📥 Commits

Reviewing files that changed from the base of the PR and between 1a6f127 and f4646f6.

📒 Files selected for processing (4)
  • Makefile
  • README.md
  • handler.go
  • handler_test.go
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • openshift-hyperfleet/architecture (manual)
  • openshift-hyperfleet/hyperfleet-api (manual)
  • openshift-hyperfleet/hyperfleet-sentinel (manual)
  • openshift-hyperfleet/hyperfleet-adapter (manual)
  • openshift-hyperfleet/hyperfleet-broker (manual)
🚧 Files skipped from review as they are similar to previous changes (3)
  • handler.go
  • handler_test.go
  • README.md

@kuudori
kuudori force-pushed the HYPERFLEET-1492-configurable-stack-trace branch from f4646f6 to d93eb5f Compare August 11, 2026 00:13

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Makefile`:
- Line 10: Update the help-target awk pattern to use the portable POSIX ERE form
`.*##` instead of `.*?##`, preserving the existing target matching and formatted
output behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 2815e247-0932-40d8-954d-cb69171e3aab

📥 Commits

Reviewing files that changed from the base of the PR and between f4646f6 and d93eb5f.

📒 Files selected for processing (1)
  • Makefile
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • openshift-hyperfleet/architecture (manual)
  • openshift-hyperfleet/hyperfleet-api (manual)
  • openshift-hyperfleet/hyperfleet-sentinel (manual)
  • openshift-hyperfleet/hyperfleet-adapter (manual)
  • openshift-hyperfleet/hyperfleet-broker (manual)

Comment thread Makefile Outdated
…akefile, tests, docs

Make pool[T].Get() call its constructor directly on a type-mismatch
fallback instead of risking a nil zero value, add a help target to the
Makefile, collapse duplicate stack-trace filter tests into a table,
export the shared test filter helper, and restore the stack_trace
example in the README.
@kuudori
kuudori force-pushed the HYPERFLEET-1492-configurable-stack-trace branch from d93eb5f to bcf3ebc Compare August 11, 2026 03:17

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Makefile`:
- Line 10: Update the help recipe’s awk invocation around MAKEFILE_LIST to
prevent shell interpretation of makefile paths: quote or otherwise safely
shell-escape each expanded path before passing it to awk, while preserving the
existing help-target filtering and output behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 29020d75-1504-4eb8-a13b-c074df037133

📥 Commits

Reviewing files that changed from the base of the PR and between d93eb5f and bcf3ebc.

📒 Files selected for processing (1)
  • Makefile
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • openshift-hyperfleet/architecture (manual)
  • openshift-hyperfleet/hyperfleet-api (manual)
  • openshift-hyperfleet/hyperfleet-sentinel (manual)
  • openshift-hyperfleet/hyperfleet-adapter (manual)
  • openshift-hyperfleet/hyperfleet-broker (manual)

Comment thread Makefile
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.

2 participants