feat: add request diagnostics - #11
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Adds structured request diagnostics across cache coalescing, upstream execution, and the health endpoint so operators can attribute retries/failures and understand degradation without logging write payloads.
Changes:
- Add upstream lifecycle diagnostics (identity, attempt/retry decisions, and disjoint timing fields) including explicit coalescing follower vs replacement-leader attribution.
- Track retry/recovery and degradation state in the Modbus client and expose those diagnostics via the health endpoint (while keeping availability semantics unchanged).
- Extend cache coalescing to return leader/follower status and wait attribution, with corresponding tests.
Show a summary per file
| File | Description |
|---|---|
| SPEC.md | Documents new diagnostics/health behavior and updates log examples. |
| README.md | Adds “Production Diagnostics” section describing structured logs and health fields. |
| internal/proxy/proxy.go | Uses coalescing status to log follower attribution and prevent stale fallback after context end. |
| internal/proxy/proxy_test.go | Adds tests for proxy health delegation and coalescing/stale-fallback logging behavior. |
| internal/modbus/server.go | Adds structured downstream failure logging with coalescing attribution and timing fields. |
| internal/modbus/server_test.go | Adds tests for server-side diagnostics fields, levels, and payload redaction. |
| internal/modbus/errors.go | Extends RequestError to carry queue/attempt/reconnect/total durations. |
| internal/modbus/client.go | Implements timing capture, retry diagnostics, degradation counters, and health reporting helpers. |
| internal/modbus/client_test.go | Adds tests for timing separation, log fields, payload redaction, and degradation state transitions. |
| internal/health/health.go | Extends health JSON to include optional diagnostic details and prefer atomic health snapshots. |
| internal/health/health_test.go | Adds tests for degraded/atomic report behavior and diagnostics inclusion on unhealthy. |
| internal/cache/cache.go | Introduces CoalesceWithStatus and attributed coalescing errors for leader/follower/wait attribution. |
| internal/cache/cache_test.go | Adds tests validating coalescing attribution for followers and replacement leaders. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 13/13 changed files
- Comments generated: 2
- Review effort level: Low
Add structured lifecycle timing, retry counters, coalescing attribution, and health degradation state without changing request safety behavior. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 97962790-10e4-4542-9e9e-02cadba19ef9
tma
force-pushed
the
improve-request-diagnostics
branch
from
July 30, 2026 19:02
a393a91 to
1a408e2
Compare
There was a problem hiding this comment.
Review details
Comments suppressed due to low confidence (3)
internal/modbus/client.go:598
waitForRequestSlotcomputes the delay usingtime.Until(c.nextRequestAt), which is tied to the process wall clock. Since this client now supports an injectedc.now()clock, the delay should be computed relative toc.now()to keep request pacing consistent with the same clock used for diagnostics andnextRequestAt.
func (c *Client) waitForRequestSlot(ctx context.Context) error {
delay := time.Until(c.nextRequestAt)
if delay <= 0 {
return contextError(ctx)
}
internal/modbus/client.go:537
responseCompletedAtusestime.Now()while the rest of the request timing/pacing logic is based on the injectedc.now()clock. This makes diagnostics andnextRequestAtinconsistent whenc.nowis overridden (tests/future determinism) and can skew pacing relative to the logged durations.
This issue also appears on line 594 of the same file.
attemptStart := c.now()
resp, err := c.executeRequest(ctx, req)
responseCompletedAt := time.Now()
timing.attempt += c.now().Sub(attemptStart)
internal/proxy/proxy.go:192
- The variable name
coalescedis acache.CoalesceResultstruct, not a boolean. Using a boolean-sounding name for a multi-field status object makes later checks likecoalesced.Coalescedharder to read and easy to misinterpret; consider renaming it tocoalesceResult/coalesceStatus(and updating uses in this function).
rangeKey := fmt.Sprintf("%d:%s", generation, cache.RangeKey(req.SlaveID, req.FunctionCode, req.Address, req.Quantity))
data, coalesced, err := p.cache.CoalesceWithStatus(ctx, rangeKey, func(ctx context.Context) ([]byte, error) {
data, err := p.client.Execute(ctx, req)
if err != nil {
- Files reviewed: 13/13 changed files
- Comments generated: 0 new
- Review effort level: Low
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This adds the request diagnostics intentionally left out of #10.
Request logs
Upstream lifecycle logs include:
A retryable first transport failure is logged at DEBUG with
will_retry=true. Successful retries are logged at DEBUG withattempts=2. Final failures and genuine upstream Modbus exceptions are logged at WARN.Coalesced followers report their own wait and result without pretending they made an independent upstream attempt.
Health diagnostics
The health response now includes:
One recovered retry sets
degraded=true, but health remains HTTP 200 with statusok. If no first-attempt success clears that state for one minute, status changes todegradedwhile remaining HTTP 200.A later first-attempt success clears the degradation state. Final upstream failures continue to return an unhealthy response.
Safety
This does not change the request-safety behavior from #10. End-to-end budgets, read-only retries, write ambiguity handling, exception mapping, request pacing, and cache generation remain unchanged.
Write payloads are never included in logs.
Testing
docker build --target test .