Repository navigation
[core][observability] Flush OpenTelemetry metrics before process exit - #66180
Conversation
A metric recorded shortly before a process is force-killed is lost. ray.kill routes to a force kill that ends in QuickExit (_Exit(1)), so no atexit handler runs, so ~CoreWorkerProcessImpl never reaches stats::Shutdown() and the only meter_provider_->ForceFlush() in the tree never fires. The periodic push is then the sole export path, and its cadence is floored at max(metrics_report_interval_ms, 1000), so whether a sample survives is a race against that timer. Expose the flush the recorder already performs on shutdown, without the teardown, as ray::stats::FlushMetrics and ray.util.metrics.force_flush(), so a caller that is about to kill itself can push its samples out first. Also floor the reader export deadline. It is derived as half the report interval, so a cluster that shortens the interval leaves too little time for the RPC a flush depends on, and the exporter uses delta temporality, so a timed-out export drops its samples instead of retrying them. Closes ray-project#66178 Signed-off-by: john.taylor <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces a force_flush mechanism for metrics, allowing users to manually trigger an export of recorded metrics before a process is terminated. This is particularly useful for ensuring that metrics recorded just before a force kill are not lost. The changes span across Python and C++ layers, including new API methods, updated metric recording logic, and a new test case to verify the functionality. The review feedback highlights the importance of adding defensive input validation for the timeout_ms parameter in both the Python wrapper and the C++ implementation to prevent potential runtime errors or undefined behavior.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit b87628f. Configure here.
Review feedback. A negative timeout reaches MeterContext::ForceFlush, which clamps its remaining time to the timeout, so the export gives up immediately and returns false with no explanation. Raise ValueError at the Python boundary instead. Signed-off-by: john.taylor <[email protected]>
The floor raised the periodic reader's per-export deadline to 5s without touching the export interval, so a cluster with a short report cadence got a deadline many times longer than its interval. That is the request queueing the original comment guarded against. On premerge build 73248 every test in test_metrics_2.py ran about 55s slower than on the previous build of the same shard, pushing the target past its 900s budget on both attempts, while targets that do not wait on metric export were unchanged. The floor is also not needed for the flush path. PeriodicExportingMetricReader::OnForceFlush takes the caller's timeout as its deadline and only slices its waits by the export interval, so force_flush() already gets its time from the timeout_ms handed to FlushMetrics. Co-Authored-By: Claude <[email protected]> Signed-off-by: john.taylor <[email protected]>
|
@johntaylor-cell would it be possible to add it here? That line is the OpenCensus‘s export before exit hook. OpenTelemetry is missing here. |
Review feedback from Yicheng-Lu-llll. DisconnectServices already exports before exit, but only for OpenCensus, which is off whenever enable_open_telemetry is on. That hook is also on the force path, since ExecuteForceShutdown calls DisconnectServices before QuickExit, so adding the OpenTelemetry flush there is what makes a metric recorded just before a ray.kill survive, with no change required of the caller. Bounded at 2s in a named constant: unlike ExportNow, which hands its batch to an async gRPC, ForceFlush blocks until the export completes, and this runs on every worker exit. Signed-off-by: john.taylor <[email protected]>
Yicheng-Lu-llll
left a comment
There was a problem hiding this comment.
I am wondering if we still needs an new api after f789c6c
Review feedback from Yicheng-Lu-llll. With the flush wired into the exit hook, the motivating case needs no caller changes at all: HandleKillActor with force_kill reaches ForceExit, ExecuteForceShutdown and DisconnectServices, so both a self-kill and a remote ray.kill flush on the way out, and Ray Serve needs no code of its own. That leaves ray.util.metrics.force_flush buying only a flush without an exit, which has no caller today, so the binding, the public function, its validation and the two tests that exercised it directly are removed. ray::stats::FlushMetrics and the recorder method stay, since the hook calls them. Signed-off-by: john.taylor <[email protected]>
I was worried about affecting every exit. Adopted recommendation. |
Review feedback from Yicheng-Lu-llll: the assertion passed whether or not ForceFlush was called, since it only checked that recording still worked afterwards. The suite shares one recorder and exports to a dead endpoint, so there is nothing non-vacuous left to assert there. The Python test carries the guard instead: with the report interval at 60s, the sample can only reach the endpoint via the flush. Co-Authored-By: Claude <[email protected]> Signed-off-by: john.taylor <[email protected]>
…ray-project#66180) ## Why `DisconnectServices` already exports before exit via `opencensus::stats::StatsExporter::ExportNow()`, but has no OpenTelemetry equivalent, and OpenCensus is off by default. So anything recorded since the last periodic push dies with the process. That hook is on the force path too, since `ExecuteForceShutdown` calls it before `QuickExit`, which is why Ray Serve lost `ray_serve_proxy_shutdown_duration_ms` in ray-project#66151 (roughly a 40% pass rate on the HAProxy shard). ## What - `OpenTelemetryMetricRecorder::ForceFlush(timeout)`, the flush `Shutdown()` already does, without the teardown. - `ray::stats::FlushMetrics(timeout_ms)`, false when stats are disabled or OpenTelemetry is off. - Call it in `DisconnectServices` beside `ExportNow()`, bounded at 500ms because `ForceFlush` blocks where `ExportNow` does not, and every worker exit takes this path. No caller changes needed, including in Serve. ## Testing A metric recorded before a force kill now reaches the endpoint, with the report interval set to 60s so only the flush can deliver it. C++ case covers `ForceFlush` leaving the recorder usable. `core_worker_lib`, `//:_raylet` and the stats and observability targets build clean; recorder test 5/5. Closes ray-project#66178. --------- Signed-off-by: john.taylor <[email protected]> Co-authored-by: Claude <[email protected]>

Why
DisconnectServicesalready exports before exit viaopencensus::stats::StatsExporter::ExportNow(), but has no OpenTelemetry equivalent, and OpenCensus is off by default. So anything recorded since the last periodic push dies with the process. That hook is on the force path too, sinceExecuteForceShutdowncalls it beforeQuickExit, which is why Ray Serve lostray_serve_proxy_shutdown_duration_msin #66151 (roughly a 40% pass rate on the HAProxy shard).What
OpenTelemetryMetricRecorder::ForceFlush(timeout), the flushShutdown()already does, without the teardown.ray::stats::FlushMetrics(timeout_ms), false when stats are disabled or OpenTelemetry is off.DisconnectServicesbesideExportNow(), bounded at 500ms becauseForceFlushblocks whereExportNowdoes not, and every worker exit takes this path.No caller changes needed, including in Serve.
Testing
A metric recorded before a force kill now reaches the endpoint, with the report interval set to 60s so only the flush can deliver it. C++ case covers
ForceFlushleaving the recorder usable.core_worker_lib,//:_rayletand the stats and observability targets build clean; recorder test 5/5.Closes #66178.