|
| 1 | +--- |
| 2 | +title: Ray |
| 3 | +description: "Learn how to import and use the Ray integration." |
| 4 | +--- |
| 5 | + |
| 6 | +The Ray integration adds support for the [Ray](https://www.ray.io/) unified compute framework. |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +To get started, install `sentry-sdk` from PyPI. |
| 11 | + |
| 12 | +```bash |
| 13 | +pip install --upgrade sentry-sdk |
| 14 | +``` |
| 15 | + |
| 16 | +## Configure |
| 17 | + |
| 18 | +Add `RayIntegration()` to your `integrations` list: |
| 19 | + |
| 20 | +<SignInNote /> |
| 21 | + |
| 22 | +In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/). You can also collect and analyze performance profiles from real users with [profiling](/product/explore/profiling/). |
| 23 | + |
| 24 | +Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below. |
| 25 | + |
| 26 | +<OnboardingOptionButtons |
| 27 | + options={["error-monitoring", "performance", "profiling"]} |
| 28 | +/> |
| 29 | + |
| 30 | +```python {"onboardingOptions": {"performance": "9-11", "profiling": "12-15"}} |
| 31 | +import ray |
| 32 | + |
| 33 | +import sentry_sdk |
| 34 | +from sentry_sdk.integrations.ray import RayIntegration |
| 35 | + |
| 36 | +def init_sentry(): |
| 37 | + sentry_sdk.init( |
| 38 | + dsn="___PUBLIC_DSN___", |
| 39 | + # Set traces_sample_rate to 1.0 to capture 100% |
| 40 | + # of transactions for tracing. |
| 41 | + traces_sample_rate=1.0, |
| 42 | + # Set profiles_sample_rate to 1.0 to profile 100% |
| 43 | + # of sampled transactions. |
| 44 | + # We recommend adjusting this value in production. |
| 45 | + profiles_sample_rate=1.0, |
| 46 | + integrations=[ |
| 47 | + RayIntegration(), |
| 48 | + ], |
| 49 | + ) |
| 50 | + |
| 51 | +init_sentry() |
| 52 | + |
| 53 | +ray.init( |
| 54 | + runtime_env={"worker_process_setup_hook": init_sentry}, |
| 55 | +) |
| 56 | +``` |
| 57 | + |
| 58 | +Be sure to call `sentry_sdk.init()` before you call `ray.init()`. |
| 59 | + |
| 60 | +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. |
| 61 | + |
| 62 | +## Verify |
| 63 | + |
| 64 | +Trigger an error in your code to verify that the integration is sending events to Sentry. |
| 65 | + |
| 66 | +```python |
| 67 | +def init_sentry(): |
| 68 | + sentry_sdk.init(...) # same as above |
| 69 | + |
| 70 | +init_sentry() |
| 71 | + |
| 72 | +ray.init( |
| 73 | + runtime_env={"worker_process_setup_hook": init_sentry}, |
| 74 | +) |
| 75 | + |
| 76 | +@ray.remote |
| 77 | +def divide(a, b): |
| 78 | + return a/b |
| 79 | + |
| 80 | +with sentry_sdk.start_transaction(name="ray-test"): |
| 81 | + futures = [ |
| 82 | + divide.remote(10, 5), |
| 83 | + divide.remote(10, 0), |
| 84 | + ] |
| 85 | + print(ray.get(futures)) |
| 86 | +``` |
| 87 | + |
| 88 | +Running this will create an error event (`ZeroDivisionError`) that will be sent to [sentry.io](https://sentry.io). Additionally, trace information will be created in the Performance section of [sentry.io](https://sentry.io). |
| 89 | + |
| 90 | +## Behavior |
| 91 | + |
| 92 | +- All unhandled exceptions will be captured and can be seen on [sentry.io](https://sentry.io). |
| 93 | +- Performance data will be captured and available in the Performance section of [sentry.io](https://sentry.io). |
| 94 | +- Performance data from [Ray Tasks](https://docs.ray.io/en/latest/ray-core/tasks.html) will be captured and linked to the calling code. |
| 95 | +- **Note**: Capturing performance data from [Ray Actors](https://docs.ray.io/en/latest/ray-core/actors.html) is currently **not supported**. (As always, [PRs are welcome](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md)) |
| 96 | + |
| 97 | +## Supported Versions |
| 98 | + |
| 99 | +- Ray: 2.34+ |
| 100 | +- Python: 3.8+ |
| 101 | + |
0 commit comments