|
| 1 | +import json |
| 2 | +import os |
| 3 | +import pytest |
| 4 | + |
| 5 | +import ray |
| 6 | + |
| 7 | +import sentry_sdk |
| 8 | +from sentry_sdk.envelope import Envelope |
| 9 | +from sentry_sdk.integrations.ray import RayIntegration |
| 10 | +from tests.conftest import TestTransport |
| 11 | + |
| 12 | + |
| 13 | +class RayTestTransport(TestTransport): |
| 14 | + def __init__(self): |
| 15 | + self.envelopes = [] |
| 16 | + super().__init__() |
| 17 | + |
| 18 | + def capture_envelope(self, envelope: Envelope) -> None: |
| 19 | + self.envelopes.append(envelope) |
| 20 | + |
| 21 | + |
| 22 | +class RayLoggingTransport(TestTransport): |
| 23 | + def __init__(self): |
| 24 | + super().__init__() |
| 25 | + |
| 26 | + def capture_envelope(self, envelope: Envelope) -> None: |
| 27 | + print(envelope.serialize().decode("utf-8", "replace")) |
| 28 | + |
| 29 | + |
| 30 | +def setup_sentry_with_logging_transport(): |
| 31 | + setup_sentry(transport=RayLoggingTransport()) |
| 32 | + |
| 33 | + |
| 34 | +def setup_sentry(transport=None): |
| 35 | + sentry_sdk.init( |
| 36 | + integrations=[RayIntegration()], |
| 37 | + transport=RayTestTransport() if transport is None else transport, |
| 38 | + traces_sample_rate=1.0, |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.forked |
| 43 | +def test_ray_tracing(): |
| 44 | + setup_sentry() |
| 45 | + |
| 46 | + ray.init( |
| 47 | + runtime_env={ |
| 48 | + "worker_process_setup_hook": setup_sentry, |
| 49 | + "working_dir": "./", |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + @ray.remote |
| 54 | + def example_task(): |
| 55 | + with sentry_sdk.start_span(op="task", description="example task step"): |
| 56 | + ... |
| 57 | + |
| 58 | + return sentry_sdk.get_client().transport.envelopes |
| 59 | + |
| 60 | + with sentry_sdk.start_transaction(op="task", name="ray test transaction"): |
| 61 | + worker_envelopes = ray.get(example_task.remote()) |
| 62 | + |
| 63 | + client_envelope = sentry_sdk.get_client().transport.envelopes[0] |
| 64 | + client_transaction = client_envelope.get_transaction_event() |
| 65 | + worker_envelope = worker_envelopes[0] |
| 66 | + worker_transaction = worker_envelope.get_transaction_event() |
| 67 | + |
| 68 | + assert ( |
| 69 | + client_transaction["contexts"]["trace"]["trace_id"] |
| 70 | + == client_transaction["contexts"]["trace"]["trace_id"] |
| 71 | + ) |
| 72 | + |
| 73 | + for span in client_transaction["spans"]: |
| 74 | + assert ( |
| 75 | + span["trace_id"] |
| 76 | + == client_transaction["contexts"]["trace"]["trace_id"] |
| 77 | + == client_transaction["contexts"]["trace"]["trace_id"] |
| 78 | + ) |
| 79 | + |
| 80 | + for span in worker_transaction["spans"]: |
| 81 | + assert ( |
| 82 | + span["trace_id"] |
| 83 | + == client_transaction["contexts"]["trace"]["trace_id"] |
| 84 | + == client_transaction["contexts"]["trace"]["trace_id"] |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.forked |
| 89 | +def test_ray_spans(): |
| 90 | + setup_sentry() |
| 91 | + |
| 92 | + ray.init( |
| 93 | + runtime_env={ |
| 94 | + "worker_process_setup_hook": setup_sentry, |
| 95 | + "working_dir": "./", |
| 96 | + } |
| 97 | + ) |
| 98 | + |
| 99 | + @ray.remote |
| 100 | + def example_task(): |
| 101 | + return sentry_sdk.get_client().transport.envelopes |
| 102 | + |
| 103 | + with sentry_sdk.start_transaction(op="task", name="ray test transaction"): |
| 104 | + worker_envelopes = ray.get(example_task.remote()) |
| 105 | + |
| 106 | + client_envelope = sentry_sdk.get_client().transport.envelopes[0] |
| 107 | + client_transaction = client_envelope.get_transaction_event() |
| 108 | + worker_envelope = worker_envelopes[0] |
| 109 | + worker_transaction = worker_envelope.get_transaction_event() |
| 110 | + |
| 111 | + for span in client_transaction["spans"]: |
| 112 | + assert span["op"] == "queue.submit.ray" |
| 113 | + assert span["origin"] == "auto.queue.ray" |
| 114 | + |
| 115 | + for span in worker_transaction["spans"]: |
| 116 | + assert span["op"] == "queue.task.ray" |
| 117 | + assert span["origin"] == "auto.queue.ray" |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.forked |
| 121 | +def test_ray_errors(): |
| 122 | + setup_sentry_with_logging_transport() |
| 123 | + |
| 124 | + ray.init( |
| 125 | + runtime_env={ |
| 126 | + "worker_process_setup_hook": setup_sentry_with_logging_transport, |
| 127 | + "working_dir": "./", |
| 128 | + } |
| 129 | + ) |
| 130 | + |
| 131 | + @ray.remote |
| 132 | + def example_task(): |
| 133 | + 1 / 0 |
| 134 | + |
| 135 | + with sentry_sdk.start_transaction(op="task", name="ray test transaction"): |
| 136 | + with pytest.raises(ZeroDivisionError): |
| 137 | + future = example_task.remote() |
| 138 | + ray.get(future) |
| 139 | + |
| 140 | + job_id = future.job_id().hex() |
| 141 | + |
| 142 | + # Read the worker log output containing the error |
| 143 | + log_dir = "/tmp/ray/session_latest/logs/" |
| 144 | + log_file = [ |
| 145 | + f |
| 146 | + for f in os.listdir(log_dir) |
| 147 | + if "worker" in f and job_id in f and f.endswith(".out") |
| 148 | + ][0] |
| 149 | + with open(os.path.join(log_dir, log_file), "r") as file: |
| 150 | + lines = file.readlines() |
| 151 | + # parse error object from log line |
| 152 | + error = json.loads(lines[4][:-1]) |
| 153 | + |
| 154 | + assert error["level"] == "error" |
| 155 | + assert ( |
| 156 | + error["transaction"] |
| 157 | + == "tests.integrations.ray.test_ray.test_ray_errors.<locals>.example_task" |
| 158 | + ) # its in the worker, not the client thus not "ray test transaction" |
| 159 | + assert error["exception"]["values"][0]["mechanism"]["type"] == "ray" |
| 160 | + assert not error["exception"]["values"][0]["mechanism"]["handled"] |
| 161 | + |
| 162 | + |
| 163 | +@pytest.mark.forked |
| 164 | +def test_ray_actor(): |
| 165 | + setup_sentry() |
| 166 | + |
| 167 | + ray.init( |
| 168 | + runtime_env={ |
| 169 | + "worker_process_setup_hook": setup_sentry, |
| 170 | + "working_dir": "./", |
| 171 | + } |
| 172 | + ) |
| 173 | + |
| 174 | + @ray.remote |
| 175 | + class Counter(object): |
| 176 | + def __init__(self): |
| 177 | + self.n = 0 |
| 178 | + |
| 179 | + def increment(self): |
| 180 | + with sentry_sdk.start_span(op="task", description="example task step"): |
| 181 | + self.n += 1 |
| 182 | + |
| 183 | + return sentry_sdk.get_client().transport.envelopes |
| 184 | + |
| 185 | + with sentry_sdk.start_transaction(op="task", name="ray test transaction"): |
| 186 | + counter = Counter.remote() |
| 187 | + worker_envelopes = ray.get(counter.increment.remote()) |
| 188 | + |
| 189 | + # Currently no transactions/spans are captured in actors |
| 190 | + assert worker_envelopes == [] |
| 191 | + |
| 192 | + client_envelope = sentry_sdk.get_client().transport.envelopes[0] |
| 193 | + client_transaction = client_envelope.get_transaction_event() |
| 194 | + |
| 195 | + assert ( |
| 196 | + client_transaction["contexts"]["trace"]["trace_id"] |
| 197 | + == client_transaction["contexts"]["trace"]["trace_id"] |
| 198 | + ) |
| 199 | + |
| 200 | + for span in client_transaction["spans"]: |
| 201 | + assert ( |
| 202 | + span["trace_id"] |
| 203 | + == client_transaction["contexts"]["trace"]["trace_id"] |
| 204 | + == client_transaction["contexts"]["trace"]["trace_id"] |
| 205 | + ) |
0 commit comments