Skip to content

Initialize debug logging at startup and isolate request settings - #721

Merged
raphael merged 2 commits into
mainfrom
fix/debug-initial-state
Sep 12, 2026
Merged

raphael merged 2 commits into
mainfrom
fix/debug-initial-state

Conversation

@raphael

@raphael raphael commented Sep 12, 2026

Copy link
Copy Markdown
Member

Clue can now start request debug logging enabled, without first making an HTTP request to its debug endpoint:

debug.MountDebugLogEnabler(mux, debug.WithInitialState(debugEnabled))

Previously the debug middleware always began with its separate process-wide switch disabled, even when the service's startup logger enabled debug. This option initializes that same switch before the servers start. Existing HTTP requests can still turn logging on or off. Mounting a handler without the option leaves the state unchanged; if several handlers explicitly initialize it, the last mounted value wins.

Request settings must not leak between calls

The existing switch was an unsynchronized Boolean. In addition, log.With created a logger with a separate lock but shared its mutable options. Applying a request's debug setting could therefore race with another request or change an already-running stream, contradicting the documented rule that toggles affect only subsequent streams.

The process-wide switch now uses atomic reads and writes. The HTTP and gRPC logging middleware own one logger per request or stream, including when request IDs are disabled. log.With copies the logging options and their slice storage; output writers and formatting functions remain shared. The debug middleware applies the selected state to that existing request logger, so the outer logging middleware can still flush buffered messages when a request fails.

This deliberately preserves log.Context's existing in-place configuration behavior. If callers supply a logger to debug middleware, it must belong to the request—normally by placing the corresponding Clue logging middleware first. Debug middleware without a supplied logger continues to create a default logger.

No endpoint paths, query values, response formats, payload limits, dependencies, or persisted data change. Existing callers need no migration; callers that want startup debug opt into the new option after updating Clue. Consumers release their usual binaries, with no coordinated service cutover required.

Verification

  • New isolation tests failed before the fix for HTTP, unary RPCs and streams; the race detector reported both the global switch race and shared logging-options races.
  • Tests cover startup on/off, multiple mounted handlers, existing request settings after toggling, concurrent toggles and requests, and default loggers when none is supplied.
  • Composed unary and stream tests verify that an untraced request with debug disabled still flushes its buffered informational messages when it fails. They caught and rejected an intermediate implementation that copied the logger inside debug middleware.
  • go test -race ./... passes.
  • golangci-lint run --timeout 3m reports zero issues.
  • Independent source review passed the complete staged diff.

@codecov

codecov Bot commented Sep 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.33%. Comparing base (3ac34ef) to head (7893dd8).
⚠️ Report is 15 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #721      +/-   ##
==========================================
+ Coverage   86.95%   87.33%   +0.38%     
==========================================
  Files          39       39              
  Lines        2008     2022      +14     
==========================================
+ Hits         1746     1766      +20     
+ Misses        238      232       -6     
  Partials       24       24              
Flag Coverage Δ
micro 87.33% <100.00%> (+0.38%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@raphael
raphael merged commit b52b35b into main Sep 12, 2026
7 checks passed
@raphael
raphael deleted the fix/debug-initial-state branch September 12, 2026 15:32
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.

1 participant