reuse metrics response - #145
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughServer-scope collection now reuses the parsed ChangesServer metrics prefetch
Estimated code review effort: 3 (Moderate) | ~25 minutes Merge Risk: ⚪ Minimal · up to Server-mode collection reuses a single metrics response across agencies, reducing redundant requests while agency-mode fetching remains unchanged. No merge-blocking risk is currently identified. Sequence Diagram(s)sequenceDiagram
participant ServerScope
participant probeLiveAgencies
participant collectAgencyChecks
participant FetchObaAPIMetrics
ServerScope->>probeLiveAgencies: fetch metrics.json once
probeLiveAgencies-->>ServerScope: live agencies and parsed OBAMetrics
ServerScope->>collectAgencyChecks: pass prefetched OBAMetrics
collectAgencyChecks->>FetchObaAPIMetrics: process each live agency
FetchObaAPIMetrics-->>collectAgencyChecks: record agency metrics
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
style: gofmt import block
ac3a175 to
fd2540a
Compare
0xaboomar
left a comment
There was a problem hiding this comment.
Hey @ahmadmuhamadi, The prefetch optimization looks good, great work!
There are just a few leftover comments and docs from the old behavior that should be cleaned up:
-
internal/app/metrics_collector.go:226-246: Remove the entireTODO(server-mode dedup)block. It describes the old N+1 fetch problem and proposes the same solution that is now implemented. The existing loop comment around lines 218-225 is still accurate and can stay. -
internal/metrics/oba_rest_api_metrics.go:53-56: Update the stale N+1 documentation. It currently says that server mode performs the same/api/where/metrics.jsonrequest once per live agency and points to the TODO above. This is no longer true. Something like this would reflect the new behavior:// In server-mode this function is called once per live agency per tick, // but reuses the response prefetched by probeLiveAgencies instead of // issuing another HTTP request. Agency-mode callers pass nil and fetch // their own response.
-
internal/metrics/oba_rest_api_metrics.go:40-70: The function documentation also needs to be updated. It currently says the function always performs an HTTP GET and thatclientmust be non-nil, which is no longer always true. It should explain that whenprefetchis nil, the function fetches and decodes the endpoint itself; whenprefetchis provided, it uses that response and skips the HTTP request. Theprefetchparameter should also be documented, andclientshould be described as required only whenprefetch == nil. -
internal/app/metrics_collector.go:135: Small whitespace typo:// Treat as "no agencies live" for this tick; static bundles st ayshould be:
// Treat as "no agencies live" for this tick; static bundles stay -
internal/app/metrics_collector_test.go:126-127: The test comment is incomplete and still describes the old N+1 behavior. Update it to describe what the test now verifies:// Server mode should fetch /api/where/metrics.json once per tick. // The parsed response is reused by each live agency's // FetchObaAPIMetrics call.
-
internal/app/metrics_collector.go:270-271: This comment reads more like a commit note than a code comment:// nil prefetch: agency-mode has no /metrics.json probe ahead of this call, // so FetchObaAPIMetrics fetches the endpoint itself. Behavior unchanged.
I would either remove it or shorten it to:
// nil: agency-mode fetches its own response.
One optional improvement: prefetch works, but something like prefetchedMetrics or cachedMetrics would make its purpose clearer. If you decide to rename it, the relevant usages are in metrics_collector.go:133, metrics_collector.go:307, metrics_service.go:66, and oba_rest_api_metrics.go:74.
Moreover, the PR is conflicting with the main branch consider resolve this conflict.
|
Okay, Tmrw i will work on them |
Fixes #139
Problem
In server mode,
probeLiveAgenciesfetches/api/where/metrics.jsonat thetop of the tick and keeps only
entry.AgencyIDs. The per-agency loop thenreaches
FetchObaAPIMetrics, which fetches the same URL again for eachagency. The endpoint takes no agency parameter, so all N+1 responses are
identical.
Change
probeLiveAgenciesnow returns the parsed response asw . It's held incollectForServerScopeand threaded throughcollectAgencyChecksintoFetchObaAPIMetrics, where a newprefetch *OBAMetricsparameter skips thefetch when non-nil. Agency mode passes
niland is unchanged.Emission logic is untouched. Removes the
TODO(server-mode dedup)and thematching doc-comment paragraph.
Result:
/metrics.jsonrequests per server-mode tick go from N+1 to 1.Tests
TestServerScopeFetchesMetricsOncePerTickruns one server-mode tick over twolive agencies and asserts one request reaches the endpoint, and that both
agencies emit their own values from that response. The stub returns an empty
agencyIDsafter the first call, so any refetch would trip the guard and emitnothing. Verified failing on the old behaviour.
Summary by CodeRabbit
Performance
Bug Fixes
Tests