Ray

Learn how to import and use the Ray integration.

The Ray integration adds support for the Ray unified compute framework.

To get started, install sentry-sdk from PyPI.

Copied
pip install --upgrade sentry-sdk

Add RayIntegration() to your integrations list:

In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing. You can also collect and analyze performance profiles from real users with profiling.

Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.

Copied
import ray

import sentry_sdk
from sentry_sdk.integrations.ray import RayIntegration

def init_sentry():
    sentry_sdk.init(
        dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for tracing.
        traces_sample_rate=1.0,
        # Set profiles_sample_rate to 1.0 to profile 100%
        # of sampled transactions.
        # We recommend adjusting this value in production.
        profiles_sample_rate=1.0,
        integrations=[
            RayIntegration(),
        ],
    )

init_sentry()

ray.init(
    runtime_env={"worker_process_setup_hook": init_sentry},
)

Be sure to call sentry_sdk.init() before you call ray.init().

By setting the worker_process_setup_hook we make sure that sentry_sdk.init() is called inside Ray worker processes during startup. This allows Sentry to connect code running in the worker with the calling code.

Trigger an error in your code to verify that the integration is sending events to Sentry.

Copied
def init_sentry():
    sentry_sdk.init(...)  # same as above

init_sentry()

ray.init(
    runtime_env={"worker_process_setup_hook": init_sentry},
)

@ray.remote
def divide(a, b):
    return a/b

with sentry_sdk.start_transaction(name="ray-test"):
    futures = [
        divide.remote(10, 5),
        divide.remote(10, 0),
    ]
    print(ray.get(futures))

Running this will create an error event (ZeroDivisionError) that will be sent to sentry.io. Additionally, trace information will be created in the Performance section of sentry.io.

  • All unhandled exceptions will be captured and can be seen on sentry.io.
  • Performance data will be captured and available in the Performance section of sentry.io.
  • Performance data from Ray Tasks will be captured and linked to the calling code.
  • Note: Capturing performance data from Ray Actors is currently not supported. (As always, PRs are welcome)

  • Ray: 2.34+
  • Python: 3.8+
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").