|
10 | 10 | from opentelemetry.metrics import Counter, Histogram, UpDownCounter |
11 | 11 | from opentelemetry.metrics import _Gauge as Gauge |
12 | 12 | from opentelemetry.sdk.trace import Span, TracerProvider |
| 13 | +from opentelemetry.trace import StatusCode |
13 | 14 |
|
14 | 15 | from docket import Docket, Worker |
15 | 16 | from docket.dependencies import Retry |
@@ -98,6 +99,97 @@ async def the_task(): |
98 | 99 | assert link.context.span_id == originating_span.context.span_id |
99 | 100 |
|
100 | 101 |
|
| 102 | +async def test_failed_task_span_has_error_status(docket: Docket, worker: Worker): |
| 103 | + """When a task fails, its span should have ERROR status.""" |
| 104 | + captured: list[Span] = [] |
| 105 | + |
| 106 | + async def the_failing_task(): |
| 107 | + span = trace.get_current_span() |
| 108 | + assert isinstance(span, Span) |
| 109 | + captured.append(span) |
| 110 | + raise ValueError("Task failed") |
| 111 | + |
| 112 | + await docket.add(the_failing_task)() |
| 113 | + await worker.run_until_finished() |
| 114 | + |
| 115 | + assert len(captured) == 1 |
| 116 | + (task_span,) = captured |
| 117 | + |
| 118 | + assert isinstance(task_span, Span) |
| 119 | + assert task_span.status is not None |
| 120 | + assert task_span.status.status_code == StatusCode.ERROR |
| 121 | + assert task_span.status.description is not None |
| 122 | + assert "Task failed" in task_span.status.description |
| 123 | + |
| 124 | + |
| 125 | +async def test_retried_task_spans_have_error_status(docket: Docket, worker: Worker): |
| 126 | + """When a task fails and is retried, each failed attempt's span should have ERROR status.""" |
| 127 | + captured: list[Span] = [] |
| 128 | + attempt_count = 0 |
| 129 | + |
| 130 | + async def the_retrying_task(retry: Retry = Retry(attempts=3)): |
| 131 | + nonlocal attempt_count |
| 132 | + attempt_count += 1 |
| 133 | + span = trace.get_current_span() |
| 134 | + assert isinstance(span, Span) |
| 135 | + captured.append(span) |
| 136 | + |
| 137 | + if attempt_count < 3: |
| 138 | + raise ValueError(f"Attempt {attempt_count} failed") |
| 139 | + # Third attempt succeeds |
| 140 | + |
| 141 | + await docket.add(the_retrying_task)() |
| 142 | + await worker.run_until_finished() |
| 143 | + |
| 144 | + assert len(captured) == 3 |
| 145 | + |
| 146 | + # First two attempts should have ERROR status |
| 147 | + for i in range(2): |
| 148 | + span = captured[i] |
| 149 | + assert isinstance(span, Span) |
| 150 | + assert span.status is not None |
| 151 | + assert span.status.status_code == StatusCode.ERROR |
| 152 | + assert span.status.description is not None |
| 153 | + assert f"Attempt {i + 1} failed" in span.status.description |
| 154 | + |
| 155 | + # Third attempt should have OK status (or no status set, which is treated as OK) |
| 156 | + success_span = captured[2] |
| 157 | + assert isinstance(success_span, Span) |
| 158 | + assert ( |
| 159 | + success_span.status is None or success_span.status.status_code == StatusCode.OK |
| 160 | + ) |
| 161 | + |
| 162 | + |
| 163 | +async def test_infinitely_retrying_task_spans_have_error_status( |
| 164 | + docket: Docket, worker: Worker |
| 165 | +): |
| 166 | + """When a task with infinite retries fails, each attempt's span should have ERROR status.""" |
| 167 | + captured: list[Span] = [] |
| 168 | + attempt_count = 0 |
| 169 | + |
| 170 | + async def the_infinite_retry_task(retry: Retry = Retry(attempts=None)): |
| 171 | + nonlocal attempt_count |
| 172 | + attempt_count += 1 |
| 173 | + span = trace.get_current_span() |
| 174 | + assert isinstance(span, Span) |
| 175 | + captured.append(span) |
| 176 | + raise ValueError(f"Attempt {attempt_count} failed") |
| 177 | + |
| 178 | + execution = await docket.add(the_infinite_retry_task)() |
| 179 | + |
| 180 | + # Run worker for only 3 task executions of this specific task |
| 181 | + await worker.run_at_most({execution.key: 3}) |
| 182 | + |
| 183 | + # All captured spans should have ERROR status |
| 184 | + assert len(captured) == 3 |
| 185 | + for i, span in enumerate(captured): |
| 186 | + assert isinstance(span, Span) |
| 187 | + assert span.status is not None |
| 188 | + assert span.status.status_code == StatusCode.ERROR |
| 189 | + assert span.status.description is not None |
| 190 | + assert f"Attempt {i + 1} failed" in span.status.description |
| 191 | + |
| 192 | + |
101 | 193 | async def test_message_getter_returns_none_for_missing_key(): |
102 | 194 | """Should return None when a key is not present in the message.""" |
103 | 195 |
|
|
0 commit comments