ray.runtime_context.RuntimeContext.get_session_name#

RuntimeContext.get_session_name() str[source]#

Get the session name for the Ray cluster this process is connected to.

The session name uniquely identifies a Ray cluster instance. This is the same value that appears as the SessionName label in Ray metrics, making it useful for filtering metrics when multiple clusters run the same application name.

This can be called from within a driver, task, or actor.

Example

import ray

ray.init()
session_name = ray.get_runtime_context().get_session_name()
print(f"Session Name: {session_name}")

@ray.remote
def get_session_name():
    return ray.get_runtime_context().get_session_name()

# Session name is the same across all processes in the cluster
assert ray.get(get_session_name.remote()) == session_name

# Use SessionName label to filter metrics by cluster, e.g.:
# ray_serve_http_request_latency_ms_bucket{SessionName="<session_name>"}
Returns:

A session name string for the Ray cluster (e.g., “session_2025-01-01_00-00-00_000000_1234”).

Raises:

RuntimeError – If Ray has not been initialized.