Skip to content

[core][observability] Flush OpenTelemetry metrics before process exit - #66180

Merged
rueian merged 6 commits into
ray-project:masterfrom
johntaylor-cell:serve-metrics-force-flush
Sep 19, 2026
Merged

rueian merged 6 commits into
ray-project:masterfrom
johntaylor-cell:serve-metrics-force-flush

Conversation

@johntaylor-cell

@johntaylor-cell johntaylor-cell commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

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 #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 #66178.

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]>
@johntaylor-cell
johntaylor-cell requested a review from a team as a code owner September 15, 2026 18:26
@johntaylor-cell johntaylor-cell self-assigned this Sep 15, 2026
@johntaylor-cell johntaylor-cell added core-api go add ONLY when ready to merge, run all tests labels Sep 15, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread python/ray/util/metrics.py Resolved
Comment thread src/ray/stats/metric.cc Resolved

@cursor cursor 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.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

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]>
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core observability Issues related to the Ray Dashboard, Logging, Metrics, Tracing, and/or Profiling labels Sep 15, 2026
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]>
@Yicheng-Lu-llll

Copy link
Copy Markdown
Member

@johntaylor-cell would it be possible to add it here?

opencensus::stats::StatsExporter::ExportNow();

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 Yicheng-Lu-llll left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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]>
@johntaylor-cell johntaylor-cell changed the title [core][observability] Add ray.util.metrics.force_flush() [core][observability] Flush OpenTelemetry metrics before process exit Sep 16, 2026
@johntaylor-cell

Copy link
Copy Markdown
Contributor Author

I am wondering if we still needs an new api after f789c6c

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]>
@rueian
rueian merged commit 2c6b81f into ray-project:master Sep 19, 2026
6 checks passed
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests observability Issues related to the Ray Dashboard, Logging, Metrics, Tracing, and/or Profiling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[core][observability] Expose a metrics force-flush so values recorded just before a force kill are not lost

3 participants